我用vs2012設計一個winform的注冊登錄視窗,鏈接的表列名是“用戶名”“密碼”。以下是注冊按鈕的代碼:
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
MessageBox.Show("用戶名或密碼不得為空", "警告");
else
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=log;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from log where 用戶名='" + textBox1.Text.Trim() + "'and 密碼='" + textBox2.Text.Trim() + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
MessageBox.Show("該用戶名已存在", "警告");
else
{
sdr.Close();
string myInsert = "insert into log(用戶名,密碼) values('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand myCom = new SqlCommand(myInsert, conn);
myCom.ExecuteNonQuery();
conn.Close();
conn.Dispose();
MessageBox.Show("注冊成功");
}
}
運行結果是只有用戶名和密碼完全和之前的相同時才會報告重復,如果密碼不同就可以隨意注冊,請問該怎么改代碼?還請大佬解答,先謝謝了
uj5u.com熱心網友回復:
注冊不用驗證密碼,查詢用戶名是否存在就可以了轉載請註明出處,本文鏈接:https://www.uj5u.com/net/261560.html
標籤:C#
上一篇:WPF界面設計
