简体   繁体   English

如何将 GridView 中的数据保存和更新到 ASP.Net C# 中的数据库?

[英]how to Save and Update data from GridView to database in ASP.Net C#?

I developed a web application using ASP.Net C#.我使用 ASP.Net C# 开发了一个 web 应用程序。

In this application there is a GridView that I fill it with data using ODBCDataAdapter as in the following code:-在这个应用程序中,有一个 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();

}

This code works fine and loads the data from dataset to the GridView.此代码工作正常,并将数据从数据集加载到 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.我的问题是我在 GridView 中做了一些更改,我想使用 DataSet 将这些更改保存在真实数据库中,这些更改应根据按钮单击事件或特定条件而更改。

I try using the following but it did not work althogh it gives 0 as the result.我尝试使用以下方法,但它没有工作,尽管它给出了 0 作为结果。

OdbcDataAdapter1.UpdateCommand = new OdbcCommand("UPDATE TableName", OdbcConnection1); 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..因为每次我检查数据库时我都没有发现任何变化,尽管变化出现在 GridView..

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.我需要阅读 Microsoft 和 MSDN 以获取有关使用 C#、Z9E0DA8438E1E38A1C30F4B76741B0B5A2Z 操作(SELECT - INSERT - UPDATE - DELETE)的更多示例和代码示例。 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. ODBCDataAdapter 应该是在使用 Disconnected 模式的情况下处理该问题的好方法,或者 ODBCDataReader 将是使用 Online 模式的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net如何从gridview C#更新数据库 - ASP.Net How to Update a database from a gridview C# 如何在ASP.NET C#中将多个gridview行数据保存到数据库 - How to save multiple gridview row data to database in asp.net c# 将gridview的所有行保存到数据库asp.net C# - save all rows of gridview to database asp.net c# 如何从数据库驱动的数据在C#和asp.net中设置GridView的字段宽度 - How do i set the field widths of a GridView in C# and asp.net from database driven data 更新gridview asp.net中的复选框更改数据库c# - update database on checkbox change in gridview asp.net c# 在Asp.Net C#中更新gridview - Update gridview in Asp.Net c# ASP.NET C#将图像从数据库绑定到GridView - ASP.NET C# Binding Images to GridView from Database 如何使用asp.net C#使用GridView值更新数据库中的另一个表 - how to use gridview values to update another table in database using asp.net c# 如何使用asp.net C#中的绑定功能在GridView中编辑删除数据行,其中数据来自数据库 - how to edit delete data row in gridview where data come from database using binding function in asp.net c# 如何通过数据库中的 DataList/RepeaterConrol 或 Gridview 在 asp.net C# 中以水平格式显示我的数据? - How can I display my data in horizontal format in asp.net C# through DataList/RepeaterConrol or Gridview from database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM