如何检查c#中sql中是否存在用户名和密码 [英] How to check whether user name and password exists in sql in c#

查看:128
本文介绍了如何检查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,看看它返回什么,如果有数据,两者都存在,如果没有......它们不在表中



< a href =http://www.w3schools.com/sql/sql_where.asp> 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[^]


这篇关于如何检查c#中sql中是否存在用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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