我在使用HTTP POST請求綁定到asp.net web API的HTML表中創建過濾器,並使用此jquery querybuilder創建過濾器。動態構建來自c#對象的SQL WHERE子句
{
"condition": "AND",
"rules": [
{
"id": "price",
"field": "price",
"type": "double",
"input": "text",
"operator": "less",
"value": "10.25"
},
{
"condition": "OR",
"rules": [
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": "2"
},
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": "1"
}]
}]
}
有沒有什麼辦法可以轉換在此JSON對象,其中使用C#或可在SQL接收HTTP發佈對象和過濾數據的任何其它NuGet包的條件?
C#
public Products GetProductById(object filterObj)
{
SqlDataReader reader = null;
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server=.\SQLSERVER2008R2;Database=DBCompany;User ID=sa;[email protected];";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "SELECT * FROM MYTABLE WHERE PRICE < 10.25 AND (CATEGORY = 2 OR CATEGORY = 1);
sqlCmd.Connection = myConnection;
myConnection.Open();
reader = sqlCmd.ExecuteReader();
Products prod = null;
while (reader.Read())
{
prod = new Products();
prod.Id = Convert.ToInt32(reader.GetValue(0));
prod.Name = reader.GetValue(1).ToString();
prod.CategoryId = Convert.ToInt32(reader.GetValue(2));
}
return prod ;
}
所有數據都存在,所以可以將其轉換爲SQL查詢。如果你想在c#中做到這一點,你可以創建自己的方法來爲你解碼。 – Neil
你使用的是mvc web api嗎? – InferOn
是的我在後端使用mvc web api和SQL服務器 –