Getting data from a Datagrid back into SQL Server Database

家住魔仙堡 提交于 2019-12-25 08:25:15

问题


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.

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.

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;

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?


回答1:


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); 

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

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

Some references for you:
Update database from DataGridView
How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual C# .NET
DbDataAdapter.Update Method




回答2:


You should implement insert/update/delete commands in your adapter.

myAdapter.InsertCommand = new SqlCommand("INSERT INTO yourTable VALUES(@param1, @param2)", conn);
myAdapter.InsertCommand.Parameters.Add("@param1",...);

Just do the same to the Update and Delete commands. Then, fill your DataSet using the adapter and make it as a datasource of your datagrid.

DataSet ds = new DataSet();
myAdapter.Fill(ds);
dataGrid1.DataSource = ds;

When you want to send your changes to the database you can just call the myAdapter.Update method.

myAdapter.Update(ds);

A nice article here: Add, Edit, and Delete in DataGridView




回答3:


Here's a pretty basic example on how to do updates from a datagrid

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.updatecommand.aspx

Basically you need a

    OnEditCommand="ItemsGrid_Edit"

Then in your

    Sub ItemsGrid_Update(sender As Object, e As DataGridCommandEventArgs) 

Method you need to cast the items back into textboxes to get their values

    Dim qtyText As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)

Then you can use those values in your SQL UPDATE

Oh and you probably should use stored procedures or Linq rather than straight SQL if this is ever going to be used by many users.

Also the code above is VB.net, you should use tags in your question to say what language you're using



来源:https://stackoverflow.com/questions/10140709/getting-data-from-a-datagrid-back-into-sql-server-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!