通过DataSet 对象更新数据库
来源:互联网 发布:渭南市广电网络 编辑:程序博客网 时间:2024/05/16 03:13
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
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();
}
}
}
(comeform MSDN)
如何通过 DataSet 对象更新数据库
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();
}
}
}
(comeform MSDN)
- 通过DataSet 对象更新数据库
- 通过DataSet更新数据库
- 通过DataSet更新、插入数据库
- 从 DataSet 对象更新数据库
- 如何使用 Visual C# 2005 或 Visual C# .NET 通过 DataSet 对象更新数据库
- 通过 SqlDataAdapter.update 方法通过DataSet更新数据库
- 如何使用 Visual C# .NET 从 DataSet 对象更新数据库
- 利用DataSet更新数据库
- DataSet更新数据库
- DataSet 更新到数据库
- dataset更新数据库
- DataSet更新到数据库
- 用 DataSet 对象更新数据
- 用 DataSet 对象更新数据
- 用DataSet对象更新数据
- 用 DataSet 对象更新数据
- DataSet更新到数据库总结
- DataSet更新到数据库总结
- PHP - 下载 - AppServ 2.5.9
- asp.net 2.0中的几个小技巧
- 我要学习的书
- 把ACCESS数据库转换SQL数据库
- 从.NET CLI(第3版)看.NET Framework 2.0
- 通过DataSet 对象更新数据库
- Vb.net用Luhn's Algorthim辨别信用卡真假
- OpenLDAP学习笔记
- OpenLDAP快速指南
- OpenLDAP 操作備忘
- jquery 1.2 高级应用--ajax
- 在Ofbiz的base程序库中使用到的一些java开源技术
- samba和openldap结合实战
- 如何设置一个基本的OpenLDAP Server