使用C#进行SQL数据库更新 [英] SQL Database Update using C#

查看:790
本文介绍了使用C#进行SQL数据库更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Windows应用程序更新数据库,我已经创建了连接字符串,

I''m trying to update my database using windows application i have already created connection string,

i want only update selected rows


我的更新代码
//da作为数据适配器


My update codes
// da is as data adapter

da.UpdateCommand = new SqlCommand("UPDATE complain SET Name = @NAME,Email = @EMAIL,Message= @MESSAGE", con);
           da.UpdateCommand.Parameters.Add("@NAME", SqlDbType.NVarChar).Value = nameTextBox.Text;
           da.UpdateCommand.Parameters.Add("@EMAIL", SqlDbType.NVarChar).Value = emailTextBox.Text;
           da.UpdateCommand.Parameters.Add("@MESSAGE", SqlDbType.NVarChar).Value = messageTextBox.Text;

           con.Open();
           da.UpdateCommand.ExecuteNonQuery();
           con.Close();



它确实会更新,但会影响整个数据库,因此如果我输入错误,请帮我纠正
谢谢



it does update but it effect to whole database so help me please correct me if I''m wrong
Thank you

推荐答案

您没有在查询中提供任何 Where子句.
如果您不提供任何 Where子句,它将更新整个表.
You have not provided any Where clause in your query.
If you don''t provide any Where clause, it will update whole table.


您的更新语句将更新所有记录...
您还需要添加一个where子句...类似..

更新投诉SET名称= @NAME,电子邮件= @EMAIL,消息= @MESSAGE WHERE primary_key = ????"
your update statement will update all records...
you also need to add a where clause... something like..

"UPDATE complain SET Name = @NAME,Email = @EMAIL,Message= @MESSAGE WHERE primary_key= ????"


HI,
您只是在更新语句中错过了WHERE子句.
我的另一个建议是在类文件上使用存储过程而不是直接sql查询.

you just missed WHERE clause in your update statement.
and my another suggestion is that use stored procedure rather than direct sql query on class file.


这篇关于使用C#进行SQL数据库更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆