Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to modify GetSelectStatement to get inner join select statement ?
as below :
SQL
select FooterTable.ItemCode,FooterTable.Quantity,FooterTable.UniPrice from

MasterTable inner join FooterTable on MasterTable.Serial=FooterTable.Serial,MasterTable.BranchCode=FooterTable.BranchCode,MasterTable.Year=FooterTable.Year

where MasterTable.Serial=10 AND MasterTable.Year=2019 AND MasterTable.BranchCode=1


json string i generate sql from it
{ 
   "Details":{ 
      "table":[ 
         "MasterTable",
         "FooterTable"
      ],
      "fields":{ 
         "ItemCode":"string",
         "Quantity":"int",
         "Price":"decimal"
         
      },
      "keys":{ 
         "BranchCode":1,
         "Year":2019,
         "Serial":2
      }
   }
}

How to do that please by modify code above

What I have tried:

C#
public string GetSelectStatement(string JsonDataForSelect)
{
var root = (JObject)JsonConvert.DeserializeObject(JsonDataForSelect);
var query = "";
var items = root.SelectToken("Details").Children().OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
foreach (var item in items)
{
if (item.Key == "table")
{
var tableName = item.Value;
query = string.Format("select from table {0} inner join table{1} where", tableName);
}
else if (item.Key == "keys")
{
var key = item.Value.SelectToken("").OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
var count = 0;
foreach (var id in key)
{
count++;
if (count == key.Count())
{
query += string.Format("{0} = {1}", id.Key, id.Value);
}
else
{
query += string.Format("{0} = {1} and ", id.Key, id.Value);
}
}

}
}
return query;
}
Posted
Updated 21-Sep-19 11:21am
v3
Comments
MadMyche 21-Sep-19 18:16pm    
And what does this generate for a SQL statement? What happens if you run it? Do you see a problem, and if so what is it?
ahmed_sa 21-Sep-19 22:13pm    
the problem on statement above not return select statement inner join another table
but if i run with one table it will work
can you help me to get select statement
select FooterTable.ItemCode,FooterTable.Quantity,FooterTable.UniPrice from

MasterTable inner join FooterTable on MasterTable.Serial=FooterTable.Serial,MasterTable.BranchCode=FooterTable.BranchCode,MasterTable.Year=FooterTable.Year

where MasterTable.Serial=10 AND MasterTable.Year=2019 AND MasterTable.BranchCode=1
from csharp function getselectstatment
[no name] 22-Sep-19 9:04am    
Firstly deserialise your json to a .Net object with JavaScriptSerializer or JsonConvert.DeserializeObject

Then loop through your json object (now in .Net format), and with those values, construct the query statements to do what you want with the values.
Richard Deeming 23-Sep-19 9:37am    
By putting the WHERE condition values in the JSON, and returning the query as a string, you are writing code which will be vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900