将数据从Datagrid取回SQL Server数据库 [英] Getting data from a Datagrid back into SQL Server Database

查看:75
本文介绍了将数据从Datagrid取回SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向您道歉,因为我知道对于大多数人来说这是一个非常基本的问题,但是我真的很难找到我需要的相关信息,并且想知道是否有人可以向我发送正确的信息。

I apologise in advance because I know this is a very basic question for most of you, but I'm really struggling to find the relevant information I need and wondered if someone could send me in the right direction.

我有一个winforms应用程序和一个SQLserver数据库。我知道如何用数据库中的数据填充各种控件(包括数据网格)。但是,我正在苦苦挣扎的是如何使用户对datagrid中的数据进行操作,然后返回到sql server数据库。

I have a winforms app, and a SQLserver database. I know how to poulate various controls (including the datagrid) with data from my database. However, what I am struggling with, is how to get user's manipulation of data in the datagrid, back to the sql server database.

我也知道如何从中获取数据说一个文本框,然后使用TSQL将其发布回数据库中,但是datagrid却在暗示着我。我知道它与数据绑定有关,但是我并没有取得太大进展。

Also I know how to take data from say a text box and use TSQL to post it back into the database, but the datagrid alludes me. I know its something to do with databinding, but I'm not making much progress.

这是我目前填充数据网格的方式:

Here is how I am populating the datagrid at the moment:

string sqlText = "SELECT * FROM tblCaseTypes;";
string conStr = cConnectionString.BuildConnectionString();
SqlConnection linkToDB = new SqlConnection(conStr);
SqlCommand sqlCom = new SqlCommand(sqlText, linkToDB);
SqlDataAdapter myAdapter = new SqlDataAdapter(sqlCom);
myAdapter.Fill(tsh);
dataTimesheet.DataSource = tsh;

我搜索了各种资源,但没有找到非常有用的东西。谁能指出我的方向(关于如何从数据源发回sqlserver数据库的datagrid中获取用户输入数据的文章)(初学者)?

I've searched various resources but haven't found anything terribly usedful. Can anyone point me in the direction of a good (beginners) article on how to get user input data in a datagrid posted back to the sqlserver database from whence it came?

推荐答案

我首先假设 tsh var是DataSet或DataTable。

如果为真,则更新可以通过使用与
相同的SqlCommand创建一个新的SqlDataAdapter来完成,但是可以使用Update方法,而不是使用Fill方法,而应使用从网格数据源提取的DataTable或DataSet的Update方法。在执行Update方法之前,将DataAdapter的SelectCommand属性设置为用于检索记录的SqlCommand

I start from the assumption that the tsh var is a DataSet or a DataTable.
If this is true, then the update could be done creating a new SqlDataAdapter with the same SqlCommand but, instead of using the Fill method, you use the Update method passing the DataTable or DataSet extracted from the DataSource of the grid. Before the execution of the Update method you set the property SelectCommand of the DataAdapter to the same SqlCommand used to retrieve records

DataTable dt = dataTimesheet.DataSource as DataTable;
string sqlText = "SELECT * FROM tblCaseTypes;"; 
string conStr = cConnectionString.BuildConnectionString(); 
SqlConnection linkToDB = new SqlConnection(conStr); 
SqlDataAdapter myAdapter = new SqlDataAdapter(); 
myAdapter.SelectCommand = new SqlCommand(sqlText, linkToDB);
myAdapter.Update(dt); 

这样,您将负担传递给SqlDataAdapter来构建所需的适当INSERT,DELETE和UPDATE SqlCommand。

In this way you pass to the SqlDataAdapter the burden to build the appropriate INSERT, DELETE and UPDATE SqlCommand required to update your database.

但是应该注意,内部SqlDataAdapter必须执行SelectCommand才能构造INSERT,UPDATE和DELETE SQL命令。结果,这会妨碍性能。为了获得最佳性能,请指定INSERT,DELETE和UPDATE命令,将它们显式分配给SqlDataAdapter的InsertCommand,DeleteCommand和UpdateCommand属性

However should be noted that internally the SqlDataAdapter must execute the SelectCommand in order to be able to construct the INSERT, UPDATE, and DELETE SQL commands. As a result, this can hinder performance. To achieve optimal performance, specify your INSERT, DELETE, and UPDATE commands explicitly assigning them to InsertCommand, DeleteCommand and UpdateCommand properties of the SqlDataAdapter

一些参考资料:

从DataGridView更新数据库

如何在Visual C#.NET中使用SqlDataAdapter对象更新SQL Server数据库

DbDataAdapter.Update方法

这篇关于将数据从Datagrid取回SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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