在SQL中使用like和参数 [英] use of like with parameters in sql

查看:353
本文介绍了在SQL中使用like和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从表中选择数据,如果它们的名称像文本框tex一样,为此我编写此代码

i want to select datas from table if they name like a text box tex , for this i write this code

string str = "Data Source=C:\\Documents and Settings\\almas\\Desktop\\BazarKhodro\\khodro.sdf";
Qconnection.ConnectionString = str;
Qcommand.Connection = Qconnection;


string commandText = "select id,name,lname,phone,mobile,address from moshtari where name like '% @name %' and lname like '% @lname %'";
Qcommand.Parameters.AddWithValue("@name", textBox3.Text);
Qcommand.Parameters.AddWithValue("@lname", textBox4.Text);
Qcommand.CommandText = commandText;
Qcommand.CommandType = CommandType.Text;
SqlCeDataAdapter a = new SqlCeDataAdapter();
a.SelectCommand = Qcommand;
a.Fill(k);
Qconnection.Open();
Qconnection.Close();
return k;



但也无法正常工作,我也用sql编写查询,没有错误,但不显示结果,请帮我



but dont work correct also i write the query in sql and dont have error but dont show the result pleas help me

推荐答案



删除%和@lname 之间的空格后,您可以尝试吗?

I.E

Hi

Can you try after removing the blank space between % and @lname ?

I.E

string commandText = "select id,name,lname,phone,mobile,address from moshtari where name like ''% @name %'' and lname like ''%@lname%''";


您可以尝试:

You can try:

select id,name,lname,phone,mobile,address from moshtari where name like ''%'' + @name + '' %'' and lname like ''% '' + @lname + ''%''


这篇关于在SQL中使用like和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆