我需要能夠驗證一個用戶名和密碼對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