具有多个条件的SQL select语句 [英] SQL select statement with multiple conditions

查看:159
本文介绍了具有多个条件的SQL select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我只需要编写一个满足3个条件的select语句即可.我的表包含3个字段,如userid,password和usertype.

我需要在前端传递一条select语句作为命令文本,以满足3个条件,如

textbox1值应等于表中的userid字段.IIy
textbox2的值应等于密码.
textbox3的值应等于usertype

我不知道如何在选择语句中传递多个条件,例如如何检查userid = textbox1值,password = textbox2值和usertype = textbox3值

任何人都可以帮助我.........

Hi,

i just need to write a select statement which satisfies 3 conditions.my table consist of 3 fields like userid,password and usertype.

i need to pass a select statement as command text in the front end which satisfies 3 conditions like

textbox1 value should be equal to userid field in the table.IIy
textbox2 value should be equal to password.
textbox3 value should be equal to usertype

i dont know how to pass multiple conditions in select statement like how to check userid=textbox1 value,password=textbox2 value and usertype =textbox3 value

can anyone plss help me..................

推荐答案

您可以使用任何带有WHERE的sql语句中的AND关键字,

You can use the AND keyword in any sql statement with WHERE like this

query = "SELECT userid, password, usertype FROM users WHERE userid = @userid AND password = @password AND usertype = @usertype";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@userid", textbox1.Text);
cmd.Parameters.AddWithValue("@password", textbox2.Text);
cmd.Parameters.AddWithValue("@usertype", textbox3.Text);



请注意我如何使用参数化查询而不是直接串联文本框值.



take note how I used a parameterised query instead of directly concatenating the text boxes values.


Here[^] is a good example for you (select command with parameters) that should help you get started.


这篇关于具有多个条件的SQL select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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