如何更新数据库中的所有记录? [英] How can I update all records in the database?

查看:91
本文介绍了如何更新数据库中的所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网格视图外面有一个网格视图和一个更新按钮。当用户对网格视图内的记录进行一些更改时,数据库中的记录也将更新。

这是我的代码:

  protected   void  Button2_Click( object  sender,EventArgs e)
{
string connectionString = Server = 192.168.100.252; Database = AMICASSA_Checklist; Trusted_Connection = true;

SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
SqlCommand cmd = new SqlCommand( 更新示例,myConnection);
SqlDataReader red = cmd.ExecuteReader();
red.Close();
myConnection.Close();
BindData();

}





有人可以帮助我吗?

解决方案

请阅读我对这个问题的评论。



看看这里:使用DataAdapters更新数据源 [ ^ ]

如果你想用sql更新数据声明,您应该使用更新 [ ^ ] statement。


SqlCommand cmd = new SqlCommand(更新示例,myConnection;



您的命令文本不完整。您究竟需要更新什么?你需要使用这样的命令文本





 SqlConnection myConnection = new SqlConnection(connectionString) ; 
myConnection。打开();
SqlCommand cmd = new SqlCommand( UPDATE示例集
samplecode =
+ yourvalue,myConnection);

cmd.CommandType = CommandType。文字;


批量更新使用Sql Merge将对您有用

尝试使用它

upsert to sql [ ^ ]

合并Sql [ ^ ]

批量转发到sql server [ ^ ]

I have a grid view and an update button outside the grid view. When the user make some changes to the records inside the grid view,the records in the database will also be updated.
Here's my code:

protected void Button2_Click(object sender, EventArgs e)
   {
       string connectionString = "Server=192.168.100.252;Database=AMICASSA_Checklist;Trusted_Connection=true";

       SqlConnection myConnection = new SqlConnection(connectionString);
       myConnection.Open();
       SqlCommand cmd = new SqlCommand("UPDATE Sample ", myConnection);
       SqlDataReader red = cmd.ExecuteReader();
       red.Close();
       myConnection.Close();
       BindData();

   }



Can Anyone help me?

解决方案

Please, read my comment to the question.

Have a look here: Updating Data Sources with DataAdapters[^]
If you would like to update data using sql statement, you should use UPDATE[^] statement.


SqlCommand cmd = new SqlCommand("UPDATE Sample ", myConnection);

Your command text is incomplete. What exactly you need to update? You need to use the command text something like this


SqlConnection myConnection = new SqlConnection(connectionString);
        myConnection.Open();
        SqlCommand cmd = new SqlCommand("UPDATE Sample set    
                                samplecode="+yourvalue , myConnection);

        cmd.CommandType = CommandType.Text;


Bulk Update using Sql Merge will be useful for you
Try with it
upsert to sql[^]
Merge in Sql[^]
bulk upsert to sql server[^]


这篇关于如何更新数据库中的所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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