using System;
using System.Data;
using System.Data.OleDb;
namespace ConsoleDataSet
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class DataSetClass
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataBase.mdb";
// 定义连接
OleDbConnection MyCon = new OleDbConnection(strCon);
try
{
// 打开连接
MyCon.Open();
string strSQL = @"select * from ADODemo";
OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter();
MyDataAdapter.SelectCommand = new OleDbCommand(strSQL, MyCon);
// 将数据填充到DataSet中
DataSet MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet, "ADODemo");
// 从DataSet中获取DataTable
DataTable MyDataTable = MyDataSet.Tables["ADODemo"];
// 设置“姓名”字段不能为空
MyDataTable.Columns["姓名"].AllowDBNull = false;
// 修改第一行的姓名
MyDataTable.Rows[0]["姓名"] = "赵六";
MyDataTable.Rows[0]["姓名"] = "张三";
// 添加一条新记录
DataRow NewRow = MyDataTable.NewRow();
NewRow["姓名"] = "赵六";
NewRow["年龄"] = 22;
MyDataTable.Rows.Add(NewRow);
foreach (DataRow row in MyDataTable.Rows)
{
Console.WriteLine("姓名:{0} 年龄:{1}", row["姓名"].ToString(), row["年龄"].ToString());
}
// 将DataSet中的数据更新到数据库中
OleDbCommandBuilder MyCommandBuilder = new OleDbCommandBuilder(MyDataAdapter);
MyDataAdapter.Update(MyDataSet, "ADODemo");
}
catch (Exception ex)
{
Console.WriteLine("{0}", ex.ToString());
}
finally
{
MyCon.Close();
}
Console.ReadLine();
}
}
}