Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hence OLE DB .NET Provider does not support named parameters, is there any better or faster way to search multiple columns? like just using single parameter?

cmd = new OleDbCommand("SELECT * FROM Table1 WHERE Column1 LIKE ? OR Column2 LIKE ? OR Column3 LIKE ? OR Column4 LIKE ? OR Column5 LIKE ? OR Column6 LIKE ? OR Column7 LIKE ? OR Column8 LIKE ? OR Column9 LIKE ? OR Column10 LIKE ? ", con);

cmd.Parameters.AddWithValue("@Column1", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column2", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column3", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column4", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column5", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column6", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column7", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column8", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column9", "%" + txtSearch.Text + "%");
cmd.Parameters.AddWithValue("@Column10", "%" + txtSearch.Text + "%");


What I have tried:

If i use only single parameter, the value for second parameter is not found
Posted
Updated 14-Jul-19 23:16pm
v2

No, there is no generic "column specifier" you can use. You could do it by concatenating all the column values together and checking that:
SQL
WHERE Column1 + '|' + Column2  + '|' + Column3 + '|' + Column4 + '|' + Column5 + '|' + Column6 + '|' + Column7 + '|' + Column8 + '|' + Column9 + '|' + Column10 LIKE ? "
Then you only need to provide the parameter once - but I'd strongly suggest you don't as it effectively opens your DB up to SQL Injection if any column contains data a user can enter as well as other problems.

I'd wonder why your DB is organised like that in the first place, instead of storing each column value in a separate row of a different table accessed via a foreign key.
 
Share this answer
 
using cross apply is working.

// proper indentation makes for a more readable code
var sql = @"SELECT * 
    FROM Table1 
    CROSS APPLY 
    (
        SELECT '%'+ ? +'%' As searchParam
    ) As s
    WHERE Column1 LIKE searchParam 
    OR Column2 LIKE searchParam 
    OR Column3 LIKE searchParam 
    OR Column4 LIKE searchParam 
    OR Column5 LIKE searchParam 
    OR Column6 LIKE searchParam 
    OR Column7 LIKE searchParam 
    OR Column8 LIKE searchParam 
    OR Column9 LIKE searchParam 
    OR Column10 LIKE searchParam";

// OleDbCommand implements the IDisposable interface...
using (var cmd = new OleDbCommand(sql, con))
{
    // use Add instead of AddWithValue
    cmd.Parameters.Add("@searchParam", OleDbType.VarWChar).Value = txtSearch.Text;
    con.Open();
    // Execute command here
}
 
Share this answer
 

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