如何将 GridView 中的数据保存到数据库中?
我正在开发一个 ASP.Net C# Web 应用程序,其中包含一个 GridView 来显示数据库中某个表的记录,我使用 ODBC 连接连接到它,并使用一个 DataSet 在其中保存数据并编辑它,然后我应该保存数据使用数据集中所做的更改到数据库。
我可以使用 OdbcDataAdapter 的 fill() 方法成功访问数据库,并且可以进行数据绑定,以便在 GridView 中查看数据。
我的问题是,当完成任何更新或更改时,如何将 gridview 保存到数据集,然后保存到数据库 [反之亦然,之前完成的操作]?
我在 Web 表单类中使用的示例代码如下:-
private void SelectFromDatabase()
{
string OdbcConnectionString1 = getConnectionString();
OdbcConnection OdbcConnection1 = new OdbcConnection(OdbcConnectionString1);
string OdbcSelectText1 = "SELECT * FROM table";
OdbcCommand OdbcSelectCommand1 = new OdbcCommand(OdbcSelectText1, OdbcConnection1);
OdbcDataAdapter OdbcDataAdapter1 = new OdbcDataAdapter();
try
{
OdbcConnection1.Open();
OdbcDataAdapter1.SelectCommand = OdbcSelectCommand1;
OdbcDataAdapter1.AcceptChangesDuringFill = true;
int FillResult = OdbcDataAdapter1.Fill(myDataSet, TableName);
myDataSet.AcceptChanges();
fillGridViewbyDataset(myGridView, myDataSet, TableName);
Response.Write("<br/>SelectFromDatabase() Fill Result: " + FillResult);
}
catch (Exception Exception1)
{
Response.Write("<br/> SelectFromDatabase() Exception: " + Exception1.Message);
}
finally
{
OdbcConnection1.Close();
}
}
private void fillGridViewbyDataset(GridView gv, DataSet ds, string dt)
{
gv.DataSource = ds;
gv.DataMember = dt;
gv.DataBind();
}
what I need is something like:-
how to save Gridview to the DataSet then save the DataSet to the database as i got the gridview updates but the database still without any updates !!
如果我有一个名为 myDs 的数据集,并且我通过直接访问循环来编辑其中的字段,如下所示:-
for (int i = 0; i < myDS.Tables[TableName].Rows.Count; i++)
{
//some function or web method to get the id value of the record being updated
int n = getNewNumber();
//updating the dataset record according to some condition
if (n == 0)
{
myDS.Tables[TableName].Rows[i]["id"] = n;
myDS.Tables[TableName].Rows[i]["description"] = "some data";
}
else
{
myDS.Tables[TableName].Rows[i]["id"] = n;
myDS.Tables[TableName].Rows[i]["description"] = "new data";
}
}
如何在数据库中完成这些更改当我执行 databind() 时,我可以在 GridView 中看到它,但数据库不受影响,我尝试使用 fill & OdbcDataAdapter 和 OdbcCommandBuilder 的更新方法?
请这是紧急的,因为我在开发一个重要的应用程序时需要它。
提前感谢您的回复和解答......
I am developing an ASP.Net C# Web Application that contains a GridView to display the records of a certain table from my database which I use ODBC Connection to connect to it and a DataSet to save data in it and edit it then I should save data to the database using the changes made in the DataSet.
I could access the database succefully using the fill() method of the OdbcDataAdapter and I could do databinding so that the data is viewed in the GridView.
My question is how I can save the gridview to the dataset then to the database when any updates or changes done [the vice versa of the operation done before]?
My sample code that is used inside a web form class is as follow:-
private void SelectFromDatabase()
{
string OdbcConnectionString1 = getConnectionString();
OdbcConnection OdbcConnection1 = new OdbcConnection(OdbcConnectionString1);
string OdbcSelectText1 = "SELECT * FROM table";
OdbcCommand OdbcSelectCommand1 = new OdbcCommand(OdbcSelectText1, OdbcConnection1);
OdbcDataAdapter OdbcDataAdapter1 = new OdbcDataAdapter();
try
{
OdbcConnection1.Open();
OdbcDataAdapter1.SelectCommand = OdbcSelectCommand1;
OdbcDataAdapter1.AcceptChangesDuringFill = true;
int FillResult = OdbcDataAdapter1.Fill(myDataSet, TableName);
myDataSet.AcceptChanges();
fillGridViewbyDataset(myGridView, myDataSet, TableName);
Response.Write("<br/>SelectFromDatabase() Fill Result: " + FillResult);
}
catch (Exception Exception1)
{
Response.Write("<br/> SelectFromDatabase() Exception: " + Exception1.Message);
}
finally
{
OdbcConnection1.Close();
}
}
private void fillGridViewbyDataset(GridView gv, DataSet ds, string dt)
{
gv.DataSource = ds;
gv.DataMember = dt;
gv.DataBind();
}
what I need is something like:-
how to save Gridview to the DataSet then save the DataSet to the database as i got the gridview updates but the database still without any updates !!
if I have a DataSet called myDs and I edit a field in it by direct access in a loop like the following:-
for (int i = 0; i < myDS.Tables[TableName].Rows.Count; i++)
{
//some function or web method to get the id value of the record being updated
int n = getNewNumber();
//updating the dataset record according to some condition
if (n == 0)
{
myDS.Tables[TableName].Rows[i]["id"] = n;
myDS.Tables[TableName].Rows[i]["description"] = "some data";
}
else
{
myDS.Tables[TableName].Rows[i]["id"] = n;
myDS.Tables[TableName].Rows[i]["description"] = "new data";
}
}
How I make these changes done in the database as I could see it in the GridView when I do databind() but the database is not affected and I try using the fill & update methods of OdbcDataAdapter and OdbcCommandBuilder ??
Please this is urgent as I need it in developing an important application..
Thanks in advance for your replies and answers .....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有关从 GridView 保存到数据集和数据库所需了解的所有信息,请参见 这篇文章。
希望这有帮助!
Everything you need to know about saving from the GridView to the DataSet and to the DB is explained is this article.
Hope this helps!
如果它是“重要的应用程序”,我建议使用存储过程并仅向包上的数据库用户授予 EXECUTE 权限。如果用户拥有完整的 DML 权限,您的数据可能更容易受到攻击。
这是关于调用存储过程的基本教程
如果您有时间,我'还要查看 Microsoft Enterprise图书馆。
If it's "an important application", I'd recommend using Stored Procedures and grant only the EXECUTE privilege to the database user on the package. If the user has full DML privileges, your data might be more vulnerable.
Here's a basic tutorial on calling stored procedures
If you have time, I'd also look at the Microsoft Enterprise Library.