我需要能够验证一个用户名和密码对sql服务器,我需要一个C#表单应用程序的代码。如何将SQL身份验证添加到C#表单应用程序?
我有2个文本框(1个用户和1通)设置,然后我有一个登录按钮。
SqlConnection UGIcon = new SqlConnection();
UGIcon.ConnectionString = "Data Source=HP-PC//localhost;Initial Catalog=UGI;Integrated Security=True";
UGIcon.Open();
string userText = textBox11.Text;
string passText = textBox12.Text;
SqlCommand cmd = new SqlCommand("SELECT stUsername,stPassword FROM LoginDetails WHERE stUsername='" + textBox11.Text + "' and stPassword='" + textBox12.Text + "'", UGIcon);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Login Success!!");
cmd = new SqlCommand("SELECT stRole from LoginDetails where [email protected]", UGIcon);
cmd.Parameters.AddWithValue("@stUsername",userText);
string role = cmd.ExecuteScalar().ToString();
MessageBox.Show(role);
UGIcon.Close();
}
else
{
MessageBox.Show("Access Denied!!");
UGIcon.Close();
}
的onclick作出的SqlConnection和一个SqlCommand和查询您的数据库,并得到一个SqlDataReader可以读取和验证用户 – f2lollpll 2012-03-02 05:35:45
你尝试过什么?在SO上说“_I需要代码... ...”通常是不可接受的。我们在这里帮助你,而不是为你做你的工作。 – 2012-03-02 05:37:19
只是将它添加到操作。 – 2012-03-02 05:41:31