MySQL databases are used, but when I use SQL statements with parameters for Fuzzy queries, I find that MySQL does not recognize the content in my parameters. After many experiments, I finally found the answer and shared it with you. I will not talk about it much more. The details are as follows:
Public DataTable GetUserList (string strParam1, string strParam2, string strParam3, string strParam4)
{
StringBuilder sqlContent = new StringBuilder ();
ArrayList paramList = new ArrayList ();
SqlContent. Append ("SELECT ");
SqlContent. Append ("column1 ");
SqlContent. Append (", column2 ");
SqlContent. Append (", column3 ");
SqlContent. Append (", column4 ");
SqlContent. Append ("FROM ");
SqlContent. Append ("tab_temp ");
SqlContent. Append ("WHERE 1 = 1 ");
// Determine whether the parameter is null or ""
If (! String. IsNullOrEmpty (strParam1 ))
{
SqlContent. Append ("AND column1 LIKE @ param1 ");
// Add Parameters
ParamList. Add (new MySqlParameter ("@ param1", "%" + strParam1 + "% "));
}
If (! String. IsNullOrEmpty (strParam2 ))
{
SqlContent. Append ("AND column2 LIKE @ param2 ");
ParamList. Add (new MySqlParameter ("@ param2", "%" + strParam2 + "% "));
}
If (! String. IsNullOrEmpty (strParam3 ))
{
SqlContent. Append ("AND column3 LIKE @ param3 ");
ParamList. Add (new MySqlParameter ("@ param3", "%" + strParam3 + "% "));
}
If (! String. IsNullOrEmpty (strParam4 ))
{
SqlContent. Append ("AND column4 LIKE @ param4 ");
ParamList. Add (new MySqlParameter ("@ param4", "%" + strParam4 + "% "));
}
Try
{
// Obtain the database link
DbConn. getConnection ();
ObjDT = new DataTable ();
// Call the query method in DBUtil
ObjDT = dbConn.exe cuteQuery (sqlContent. ToString (), paramList );
}
Catch (Exception e)
{
Throw e;
}
Finally
{
// Close the DB Link
DbConn. closeConnection ();
}
Return objDT;
}
Correct syntax:
SqlContent. Append ("AND column1 LIKE @ param1 ");
// Add Parameters
ParamList. Add (new MySqlParameter ("@ param1", "%" + strParam1 + "% "));
Incorrect syntax:
SqlContent. Append ("AND column1 LIKE '% @ param1 % '");
// Add Parameters
ParamList. Add (new MySqlParameter ("@ param1", strParam1 ));