ASP.NET 2.0----DataSet
来源:互联网 发布:淘宝卖家第三层级 编辑:程序博客网 时间:2024/05/16 11:09
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.(建立一个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();
}
}
}
- ASP.NET 2.0----DataSet
- Asp.net操作DataSet
- Asp.net操作DataSet
- ASP.NET遍历DataSet
- asp.net Dataset导出excel
- ASP.NET 2.0-选用DataSet或DataReader
- DataSet导出CSV格式(ASP.NET,C#)
- DataSet导出CSV格式(ASP.NET,C#)
- DataSet导出CSV格式(ASP.NET,C#)
- DataSet导出CSV格式(ASP.NET,C#)
- Asp.Net学习笔记:使用DataSet
- ASP.NET 数据库操作类---DataSet类
- DataSet导出CSV格式(ASP.NET,C#)
- asp.net DataSet添加新数据
- (C#)asp.net(DataSet)导出Excel文件
- DataSet导出CSV格式(ASP.NET,C#)
- ASP.NET 绑定DataSet中的多个表
- asp.net中用DataSet对象更新数据
- ASP.NET2.0---GridView控件学习
- ASP.NET Interview Questions (ASP.NET 面试题)
- .NET牛人应该知道些什么?
- 超详细的SQL语句语法
- SQL语句学习手册实例版
- ASP.NET 2.0----DataSet
- ASP.NET 2.0---Repeater控件
- SQL Express 2005里面没有SQL Server Management Studio管理器的解决办法
- 一个ASP.NET读写XML文件的示例
- 使用PageDataSource给Repeater、DataList增加分页
- 使用PagedDataSource类实现Repeater分页显示功能
- FCKeditor在.net(asp.net)中的使用方法
- ASP.NET中对字符串加密
- AS3操作XML添加节点