代碼如下
?
private void btn_login_Click(object sender, EventArgs e){SqlConnection sqlconnection = new SqlConnection();sqlconnection.ConnectionString = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;SqlCommand sqlcommand = new SqlCommand();sqlcommand.Connection = sqlconnection;sqlcommand.CommandText = "select COUNT(1) from tb_stuinfo where name=@name and pwd=@pwd "; //'"+txt_number.Text+"'and pwd= HASHBYTES('md5','"+txt_pwd.Text+"')";sqlcommand.Parameters.AddWithValue("@name",txt_number.Text.Trim());sqlcommand.Parameters.AddWithValue("@pwd", txt_pwd.Text.Trim());sqlconnection.Open();int rowCount = (int)sqlcommand.ExecuteScalar();sqlconnection.Close();if (rowCount == 1){MessageBox.Show("登陸成功");}else{MessageBox.Show("錯誤");this.txt_pwd.Focus();this.txt_pwd.SelectAll();}}
? ? ? ?
?
?
?
?
問題1
?
?
?
?
?
?在登錄窗體的代碼中 若sql語句中使用了”+this.txt_number+”會導致數據類型不符合,登陸失敗;改為”+txt_number.text+”,就能解決
?
注冊
private void btn_regest_Click(object sender, EventArgs e){if (txt_number.Text == "" && txt_pwd.Text == ""){MessageBox.Show("用戶名或密碼不能為空");}SqlConnection sqlconnection = new SqlConnection();sqlconnection.ConnectionString = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;SqlCommand sqlcommand = new SqlCommand();sqlcommand.Connection = sqlconnection;sqlcommand.CommandText = "insert tb_stuinfo (name,pwd) values(@name,HASHBYTES('MD5',@pwd))";sqlcommand.Parameters.AddWithValue("@name", txt_number.Text.Trim());sqlcommand.Parameters.AddWithValue("@pwd", txt_pwd.Text.Trim());sqlcommand.Parameters["@pwd"].SqlDbType=SqlDbType.VarChar;sqlconnection.Open();int rowAffected = sqlcommand.ExecuteNonQuery();sqlconnection.Close();if (rowAffected == 1){MessageBox.Show("ok");//frm_login lg = new frm_login();//lg.ShowDialog();}else{MessageBox.Show("wrong");}}
?
?