使用SqlCommand时出现错误

时间:2018-06-20 00:40:12

标签: c# sql-server

在下面的代码中使用SqlCommand时出现错误,请告知。

try
{
    sqlConnection sqlConnection1 = new sqlConnection("Server=.\\SQLEXPRESS;Database=Bazy;Integrated Security=true ");

    SqlCommand cmd1 = new SqlCommand("select pass from Users where user1='" + user + "'", sqlConnection1);
    SqlDataReader sdr;

    sqlConnection1.open();

    sdr = cmd1.ExecuteReader();

    if (sdr.GetString(0) == tBox_oldPassword.Text)
    {
        cmd1.CommandText = "update Users set Pass='" + tBox_newPassword.Text + "' where User1='" + tBox_userName.Text + "'";
        cmd1.ExecuteNonQuery();
    }
    else
    {
        MessageBox.Show("ادخل كلمة السر الحالية.");
    }

}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

enter image description here

1 个答案:

答案 0 :(得分:1)

似乎您将SqlConnection的输入错误键入为sqlConnection,然后让Visual Studio(或者可能是ReSharper,因为它倾向于使用throw new NotImplementedException();作为其生成的方法主体,并且我不确定这是否是VS的默认行为)使用构造函数或open方法自动为您实现该类,并抛出NotImplementedException,因为您尚未实现。

更改

sqlConnection sqlConnection1 = new sqlConnection("Server=.\\SQLEXPRESS;Database=Bazy;Integrated Security=true ");

SqlConnection sqlConnection1 = new SqlConnection("Server=.\\SQLEXPRESS;Database=Bazy;Integrated Security=true ");

并考虑删除您错误添加的sqlConnection类。