我是这个网站的新手,也是编程的新手。我目前正在通过销售点创建库存系统。它使用模态和非模态形式。我的问题是,我正在处理change password
对话框,该对话框必须连接到数据库才能覆盖密码字段。我使用的数据库是microsoft sql server management studio express。以下是我到目前为止所提出的必要评论。请注意,在“设计”表单中,我有一个与数据库绑定的组合框。我哪里做错了?
private void ChangePwdButton_Click(object sender, EventArgs e)
{
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Gerald- dean Martin\Documents\SQL Server Management Studio Express\Projects\BodyMates.mdf;Integrated Security=True;User Instance=True";
sqlconn.Open();
string oldpwd = txtOldPwd.Text;
string newpwd = txtNewPwd.Text;
string confirmNewPwd = txtConfirmNewPwd.Text;
string sqlquery = "UPDATE [Employee] SET Pwd=@newpass where EmployeeCode=@empcode";
SqlCommand cmd = new SqlCommand(sqlquery, sqlconn);
cmd.Parameters.AddWithValue("@newpass", txtConfirmNewPwd.Text);
cmd.Parameters.AddWithValue("@empcode", comboEmpCode.SelectedValue);
//cmd.Parameters.AddWithValue("@pwd", txtNewPwd.Text);
cmd.Connection = sqlconn;
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if(txtOldPwd.Text == dr["pwd"].ToString() && (txtNewPwd.Text == txtConfirmNewPwd.Text))
{
if (comboEmpCode.SelectedIndex == 0)
{
string query = "UPDATE [Employee] SET Pwd = '" + txtConfirmNewPwd.Text + "'";
}
}
// if ((txtNewPwd.Text == dr["newpwd"].ToString()) & (txtConfirmNewPwd.Text == (dr["confirmNewPwd"].ToString()))) { }
}
// MessageBox.Show("Password was changed Successfully!", "Password Change", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
答案 0 :(得分:1)
您可以使用cmd.ExecuteNonQuery();
,例如int
,它会返回int i = cmd.ExecuteNonQuery();
值。像这样使用它;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
而且ExecuteNonQuery
也是这样的;
reader[0]
您可以阅读返回数据的列。与第一列reader[1]
,第二列{{1}}等相同。
但在所有这些信息之前,如果您不熟悉编程,可以在Stackoverflow上找到大量的书籍提案和有用的信息。检查这些文章;