本任务的内容
概要数据集对象是 Microsoft .NET 框架中数据访问的关键部分,是可保存表、视图和关系的内存中对象。本文介绍了如何获取包含数据(从数据库加载)的数据集、如何修改此数据,以及接着如何再将其发回数据库以更新原始数据源。返回页首 要求下表概括了推荐使用的硬件、软件、网络结构以及所需的服务包:
如何从数据集对象更新数据库本节演示如何使用数据集对象更新数据库中的数据。还可以使用 SqlCommand 对象直接在数据库中插入、更新和删除数据,记住这一点很重要。如想更好地理解本文,请单击下面的文章编号以查看 Microsoft 知识库中的文章:
加载数据集后,就可以修改数据了。 数据集将跟踪这些更改。 可将数据集对象视为从数据库检索出的缓存于内存中的数据。 数据集对象由一个包含表、关系和约束的集合构成。 若要更新数据集并将这些更新发回数据库,请按照下列步骤操作:
完整代码列表using System;
using System.Data;
using System.Data.SqlClient;
namespace PopulateDataSet
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
string sConnectionString;
// Modify the following string to correctly connect to your SQL Server.
sConnectionString = "Password=;User ID=sa;"
+ "Initial Catalog=pubs;"
+ "Data Source=(local)";
SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();
// Create an instance of a DataAdapter.
SqlDataAdapter daAuthors
= new SqlDataAdapter("Select * From Authors", objConn);
// Create an instance of a DataSet, and retrieve
// data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");
//****************
// BEGIN ADD CODE
// Create a new instance of a DataTable.
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["Authors"];
DataRow drCurrent;
// Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow();
// Set the DataRow field values as necessary.
drCurrent["au_id"] = "993-21-3427";
drCurrent["au_fname"] = "George";
drCurrent["au_lname"] = "Johnson";
drCurrent["phone"] = "800 226-0752";
drCurrent["address"] = "1956 Arlington Pl.";
drCurrent["city"] = "Winnipeg";
drCurrent["state"] = "MB";
drCurrent["contract"] = 1;
// Pass that new object into the Add method of the DataTable.
tblAuthors.Rows.Add(drCurrent);
Console.WriteLine("Add was successful, Click any key to continue!!");
Console.ReadLine();
// END ADD CODE
//*****************
// BEGIN EDIT CODE
drCurrent = tblAuthors.Rows.Find("213-46-8915");
drCurrent.BeginEdit();
drCurrent["phone"] = "342" + drCurrent["phone"].ToString().Substring(3);
drCurrent.EndEdit();
Console.WriteLine("Record edited successfully, Click any key to continue!!");
Console.ReadLine();
// END EDIT CODE
//*****************
// BEGIN SEND CHANGES TO SQL SERVER
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
// END SEND CHANGES TO SQL SERVER
//*****************
//BEGIN DELETE CODE
drCurrent = tblAuthors.Rows.Find("993-21-3427");
drCurrent.Delete();
Console.WriteLine("SRecord deleted successfully, Click any key to continue!!");
Console.ReadLine();
//END DELETE CODE
//*****************
// CLEAN UP SQL SERVER
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
}
}
}返回页首 参考有关使用 ADO.NET、数据集对象和 SQL 的更多信息,请访问以下 Microsoft Web 站点:深入了解数据访问(MSDN 之音专栏) 这篇文章中的信息适用于:
| |||
| |||
使用 Visual C# .NET 从数据集对象更新数据库
最新推荐文章于 2022-01-28 18:36:58 发布
博客围绕.NET和C#展开,涉及SQL Server数据库相关内容,如使用Dataset进行数据库操作等,聚焦于信息技术领域的后端开发与数据库应用。
6875

被折叠的 条评论
为什么被折叠?



