0

The following parameterized code does not return any rows though the concatenated query works.

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 ";

This is the command instance

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);

This version works

   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 ;
Chakra
  • 2,525
  • 8
  • 43
  • 82
  • Take a look here: http://stackoverflow.com/questions/2322274/c-sharp-mysqlparameter-problem The problem was solved by replacing @ with ? – CShark Nov 26 '15 at 20:53
  • @CShark I did try with ? too but get the same result. – Chakra Nov 26 '15 at 22:56
  • I made only the 'confidenceThreshold' as a parameter. The rest of the concatenated strings were left alone since they are not directly entered into the UI. Now it works. Thanks. – Chakra Nov 26 '15 at 23:33

0 Answers0