从C#中经过编辑的Datagrid更新数据库 [英] Update Database From an edited Datagrid in C#

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

问题描述

我只想通过单击按钮来从已编辑的DataGrid更新sql数据库.
编辑后的单元格值应在数据库中更新.

I want to update sql database from a editted DataGrid only by clicking a Button.
The editted cell values should be updated in Database.

推荐答案

只需使用:

simply use:

SqlCommandBuilder build = new SqlCommandBuilder(adapter);



用于构建更新查询并在保存记录按钮上单击事件,请写以下行:



for building the update query and on save record button click event write the below line:

adapter.Update(dt);



在这里,适配器是SqlDataAdapter的对象,而dt是DataTable的对象.并确保您的表包含主键

请参阅此以了解更多信息:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx?ppud = 4 [



here adapter is a object of SqlDataAdapter and dt is object of DataTable. And make sure your table contains Primary key

refer this to learn more:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx?ppud=4[^]

hope this helps :)

for further queries comment here!!


SqlDataAdapter adapter = null;

form_load()
{
SqlConnection connection =新的SqlConnection(某些连接字符串.");
adapter = new SqlDataAdapter(从someTable中选择*",连接,);
adapter.Fill(dataTable);
}

btnUpdate()
{
SqlCommandBuilder commandBuilder =新的SqlCommandBuilder(adapter);
adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
adapter.Update(dataTable);
}
SqlDataAdapter adapter = null;

form_load()
{
SqlConnection connection = new SqlConnection("some connection string.");
adapter = new SqlDataAdapter("Select * from someTable",connection,);
adapter.Fill(dataTable);
}

btnUpdate()
{
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
adapter.Update(dataTable);
}


这篇关于从C#中经过编辑的Datagrid更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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