如何在 ASP.Net C# 中将数据从 GridView 保存和更新到数据库?
我使用 ASP.Net C# 开发了一个 Web 应用程序。
在此应用程序中,有一个 GridView,我使用 ODBCDataAdapter 向其中填充数据,如以下代码所示: -
protected void Page_Load(object sender, EventArgs e)
{
ConnectionString1 = "DSN=DataSourceName;SRVR=Server;DB=Database;UID=User;PWD=Password;";
OdbcConnection1 = new OdbcConnection(ConnectionString1);
try
{
OdbcConnection1.Open();
CommandText1 = "SELECT * FROM TableName";
DataSet1 = new DataSet();
OdbcDataAdapter1 = new OdbcDataAdapter(CommandText1, OdbcConnection1);
OdbcCommandBuilder1 = new OdbcCommandBuilder(OdbcDataAdapter1);
OdbcDataAdapter1.Fill(DataSet1, "TableName");
DataSet1.AcceptChanges();
myGridView.DataSource = DataSet1;
myGridView.DataMember = "TableName";
myGridView.DataBind();
}
catch (Exception Exception1)
{
Response.Write("<br/>Exception1 Message: " + Exception1.Message);
}
OdbcConnection1.Close();
}
此代码工作正常,并将数据从数据集加载到 GridView。
我的问题是,我在 GridView 中做了一些更改,并且我想使用 DataSet 将这些更改保存在真实数据库中,该 DataSet 应根据按钮单击事件或特定条件进行更改。
我尝试使用以下命令,但它不起作用,尽管它给出 0 作为结果。
OdbcDataAdapter1.UpdateCommand = new OdbcCommand("UPDATE TableName", OdbcConnection1);
OdbcDataAdapter1.Fill(DataSet1,"TableName");
int g = OdbcDataAdapter1.Update(DataSet1,"TableName");
Response.Write("g: " + g);
我还尝试了以下操作:-
OdbcCommandBuilder1 = new OdbcCommandBuilder(OdbcDataAdapter1);
try
{
int k = OdbcDataAdapter1.Update(DataSet1, "TableName");
DataSet1.AcceptChanges();
Response.Write("k: " + k);
}
catch (Exception Except)
{
Response.Write("Except: " + Except.Message);
}
每次检查数据库时,我都没有发现任何更改,尽管更改出现在 GridView 中。
I developed a web application using ASP.Net C#.
In this application there is a GridView that I fill it with data using ODBCDataAdapter as in the following code:-
protected void Page_Load(object sender, EventArgs e)
{
ConnectionString1 = "DSN=DataSourceName;SRVR=Server;DB=Database;UID=User;PWD=Password;";
OdbcConnection1 = new OdbcConnection(ConnectionString1);
try
{
OdbcConnection1.Open();
CommandText1 = "SELECT * FROM TableName";
DataSet1 = new DataSet();
OdbcDataAdapter1 = new OdbcDataAdapter(CommandText1, OdbcConnection1);
OdbcCommandBuilder1 = new OdbcCommandBuilder(OdbcDataAdapter1);
OdbcDataAdapter1.Fill(DataSet1, "TableName");
DataSet1.AcceptChanges();
myGridView.DataSource = DataSet1;
myGridView.DataMember = "TableName";
myGridView.DataBind();
}
catch (Exception Exception1)
{
Response.Write("<br/>Exception1 Message: " + Exception1.Message);
}
OdbcConnection1.Close();
}
This code works fine and loads the data from dataset to the GridView.
My problem is that I do some changes in that GridView and I would like to save these changes in the real database using the DataSet that should change according to an event of a button click or a specific condition.
I try using the following but it did not work althogh it gives 0 as the result.
OdbcDataAdapter1.UpdateCommand = new OdbcCommand("UPDATE TableName", OdbcConnection1);
OdbcDataAdapter1.Fill(DataSet1,"TableName");
int g = OdbcDataAdapter1.Update(DataSet1,"TableName");
Response.Write("g: " + g);
I tried also the following:-
OdbcCommandBuilder1 = new OdbcCommandBuilder(OdbcDataAdapter1);
try
{
int k = OdbcDataAdapter1.Update(DataSet1, "TableName");
DataSet1.AcceptChanges();
Response.Write("k: " + k);
}
catch (Exception Except)
{
Response.Write("Except: " + Except.Message);
}
as every time I check the database I find no changes although the changes appears in the GridView..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要阅读 Microsoft 和 MSDN,以获取有关使用 C#、ASP.NET 和 ADO.Net 执行基本数据库 SQL 操作(SELECT - INSERT - UPDATE - DELETE)的更多示例和代码示例。
ODBCDataAdapter 应该是处理使用断开模式的好方法,或者 ODBCDataReader 应该是使用在线模式的好方法。
I need to read the Microsoft and MSDN for more examples and code samples about doing the basic database SQL operations (SELECT - INSERT - UPDATE - DELETE) using C#, ASP.NET, and ADO.Net.
The ODBCDataAdapter should be the good way to handle that in case of using Disconnected mode or the ODBCDataReader will be the good way in case of using Online mode.