如何使用SQL 2008创建C#登录表单

问题描述:

大家好,





我设计了一个登录表单,我希望它能像这样运行,



用户将输入他/她的用户名和密码到texboxes。

正确的详细信息必须来自数据库。

如果用户输入错误的详细信息并且必须限制用户。



你可以在做代码时评论,因为我不想做疯狂的东西。





感谢您的帮助。

Hi Everyone,


I have designed a login form and i want it to function like this,

User will enter his/her username and password to texboxes.
The correct details must come from the database.
If the user enters the wrong details and must restrict the user.

Can you please comment when doing the codes because i dont want to do craming stuff.


Thanks for your help.

参见如何创建登录页面 [ ^ ]



干杯,

Edo
See How to create Login page[^]

Cheers,
Edo


如果它是sql server数据库实例的名称和密码,你可以以编程方式生成一个连接字符串。


或者如果用户信息存储在表格中,你可以设置查询。

这样的事情:

you can generate a connectionstring programarically if its usre name and password of sql server database instance.

or if user information is stored inside a table you can set up a query.
some thing like this:
if    exists
(
   Select   UserName,Password   From Users    Where UserName = @UserName 
   And Password  = @Password
) return 1
else
  return 0


试试这个我已经实现了这个

Try this I''ve already implemented this
public bool login(string id, string entered_password)
    {
 public SqlConnection con = new SqlConnection("Data Source=[Server Name];Initial Catalog=[Database Name];User ID=[User];Password=[Password]");
        con.Open();
        string query = "SELECT [userpass] FROM userlogin WHERE [userid]=@id";
        SqlCommand cmdChk = new SqlCommand(query, con);
        cmdChk.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
        SqlDataReader dr = cmdChk.ExecuteReader();
        int count = 0;
        string password = "";
        while (dr.Read())
        {
            count++;
            password= dr[0].ToString();
        }

        dr.Close();
        con.Close();
        if (count < 1)
        {
            return false;

        }
        else
        {
            if (password == entered_password)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }