更新查询语法正确吗? [英] is the update query syntax correct?
问题描述
我是新手,所以不要对我的问题生气,我的英语也很弱
我已经编辑了插入查询之前的查询,并且已经编辑成为uodate查询,但是最后一部分不正确,因为数据更新成功了,但是如果更新成功,我想显示一个消息框,那么它将是成功的,如果更新失败,然后将显示一个消息框,其中包含错误消息.
这是我疯狂的代码
i am newbie so dont angry on my question also i am weak in english
i have edited my query which was before an insert query and i have edited to become a uodate query but the last portion is not correct as data is updated successfull but i want to show a messagebox if updation was success then it will be suceess and if updation was failed then a message box will show which will contain message of error.
here is my crazy code
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
try
{
conn.Open();
}
catch (Exception)
{
MessageBox.Show("Error with the databse connection");
}
string qry2 = "UPDATE Table1 SET Password =@Password WHERE UserName=@username";
SqlCommand com = new SqlCommand(qry2, conn);
com.Parameters.AddWithValue("@username", this.textBox1.Text);
com.Parameters.AddWithValue("@Password", this.textBox2.Text);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true)
{
MessageBox.Show("updated sucesfull");
}
}
if (dr.HasRows == false)
{
MessageBox.Show("updation failed ");
}
推荐答案
阿迪尔,
请按以下方式更新您的代码.
Hi Adil,
Please update your code as below.
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source = .\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
try
{
conn.Open();
string qry2 = "UPDATE Table1 SET Password =@Password WHERE UserName=@username";
SqlCommand com = new SqlCommand(qry2, conn);
com.Parameters.AddWithValue("@username", this.textBox1.Text);
com.Parameters.AddWithValue("@Password", this.textBox2.Text);
int result = com.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("updated sucesfull");
}
else
MessageBox.Show("updated failed");
}
catch (Exception)
{
MessageBox.Show("Error with the databse connection");
}
希望对您有所帮助.
Hope this will help you.
请尝试以下操作:
Try the following:
int x = cmd.ExecuteNonQuery();
if (x == -2)
{
//fail and show the message
}
请检查以下示例:
http://stackoverflow.com/Questions/2963703/how-to-know-a-sql-update-statement-exected-successfully-or-failed [
please check the following for example:
http://stackoverflow.com/questions/2963703/how-to-know-whether-a-sql-update-statement-executed-successfully-or-failed[^]
这篇关于更新查询语法正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!