在C#中对mySQL的这个参数化查询有什么问题

时间:2015-11-26 18:55:15

标签: c# mysql ado.net mysql.data

以下参数化代码不会返回任何行,但连接查询仍可正常工作。

string middleOfString = "'," + serviceCode.ToString() + ",'";
string startOfString = "'[" + serviceCode.ToString() + ",'";
string endOfString = "'," + serviceCode.ToString() + "]'";

string queryString = "SELECT * FROM associationrules2 ";
queryString += "WHERE locate( @middleOfString, ItemsInInvoice)>0 AND Confidence >= @confidenceThreshold ";
queryString += "UNION ";
queryString += "SELECT * FROM associationrules2 ";
queryString += "WHERE locate( @startOfString, ItemsInInvoice)>0 AND Confidence >= @confidenceThreshold ";
queryString += "UNION ";
queryString += "SELECT * FROM associationrules2 ";
queryString += "WHERE locate( @endOfString, ItemsInInvoice)>0 AND Confidence >= @confidenceThreshold ";

这是命令实例

var cmd = new MySqlCommand(queryString, conn) {CommandType = CommandType.Text};
var param = new MySqlParameter
            {
                ParameterName = "@middleOfString",
                Value = middleOfString
            };

cmd.Parameters.Add(param);

param = new MySqlParameter
        {
            ParameterName = "@startOfString",
            Value = startOfString
        };

cmd.Parameters.Add(param);

param = new MySqlParameter
        {
            ParameterName = "@endOfString",
            Value = endOfString
        };

cmd.Parameters.Add(param);

param = new MySqlParameter
        {
            ParameterName = "@confidenceThreshold",
            Value = confidenceThreshold
        };

cmd.Parameters.Add(param);

此版本有效

   string queryString = "SELECT * FROM associationrules2 ";   
   queryString +="WHERE locate(" + middleOfString + ",ItemsInInvoice)>0 AND Confidence >=" + confidenceThreshold + " ";
   queryString +="UNION ";
   queryString +="SELECT * FROM associationrules2 ";
   queryString += "WHERE locate(" + startOfString + ",ItemsInInvoice)>0 AND Confidence >=" + confidenceThreshold + " ";
   queryString +="UNION ";
   queryString +="SELECT * FROM associationrules2 ";
   queryString += "WHERE locate(" + endOfString + ",ItemsInInvoice)>0 AND Confidence >=" + confidenceThreshold ;

0 个答案:

没有答案