如何检查c#中sql中是否存在用户名和密码
问题描述:
Hai,
我在c#web application中有用户名文本框和密码trxt框。我有一个sql表登录其中包含用户名和密码。如何验证SQL表中是否存在用户名或密码。
提前致谢
Hai,
I am having username textbox and password trxt box in c# web application.I am having a sql table "Login" which contains username and password.How to validate whether username or password exists in SQL table.
Thanks in advance
答
首先看一下:密码存储:怎么做它。 [ ^ ] - 它不包括存储或检索来自teh dtabase的数据,但这是微不足道的:
Start by looking at this: Password Storage: How to do it.[^] - it doesn't cover storing or retrieving the data from teh dtabase, but that is the trivial bit:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT password FROM myTable WHERE username=@UN", con))
{
cmd.Parameters.AddWithValue("@UN", txtUserName.Text);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
byte[] psw = (byte[])reader["password"];
...Check as in the Tip
}
}
}
}
你也可以查看这个(当然除了格里夫已经给出的答案)
check-if-username-已经存在于数据库中 [ ^ ]
You can check this also(off course apart from the answer Griff already gave)
checking-if-username-already-exists-within-the-databasae[^]
如何进行查询?你可以结合WHERE做一个SELECT,看看它返回什么,如果有数据,两者都存在,如果没有......它们不在表中
http://www.w3schools.com/sql/sql_where.asp [ ^ ]
How about doing a query? You can do a SELECT combined with WHERE and see what it returns, if there is data, both exists, if not... they are not in the table
http://www.w3schools.com/sql/sql_where.asp[^]