0

I am inserting data to a data list in asp.net. I need to modify the Select Command according to my needs. I did something like this,

string query = "SELECT [movieName], [sDate], [eDate], [IMDb], [imageUrl] FROM [movieDrama] WHERE ([category]='Drama' AND [movieName] like '%@key%') ORDER BY [movieName]";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@key", key);
SqlDataSource1.SelectCommand = query;

But this is not working. I think I did something wrong when defining '@key'. How to do it in correct way? Thanks in advance...

4

3 回答 3

6

像这样使用它;

LIKE '%' + @key + '%'

代替

LIKE '%@key%'

完整查询;

string query = "SELECT [movieName], [sDate], [eDate], [IMDb], [imageUrl] FROM [movieDrama] WHERE ([category]='Drama' AND [movieName] LIKE '%' + @key + '%') ORDER BY [movieName]";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@key", key);

实际上,您不需要查询的每一列都使用方括号,您只需要在想要使用一些保留关键字作为列名时使用。

于 2013-08-30T15:54:17.853 回答
2

Just this:

string query = "SELECT [movieName], [sDate], [eDate], [IMDb], [imageUrl] FROM [movieDrama] WHERE ([category]='Drama' AND [movieName] like @key) ORDER BY [movieName]";

then

cmd.Parameters.AddWithValue("@key", "%"+ key + "%");
于 2013-08-30T15:53:55.680 回答
0

尝试为您的 sqlCommand 提供连接参数:

    SqlCommand cmd = new SqlCommand(query,YOURCONNECTIONSTRING);
    cmd.Parameters.AddWithValue("key", key)
于 2013-09-02T11:03:17.990 回答