using System;
// use ADO.NET namespace
using System.Data;
// use OLE DB .NET Data Provider
using System.Data.OleDb;
class DataAddRowExample
{
public static void Main()
{
// connect to NWIND MS Access example with OLE DB
OleDbConnection thisConnection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/TMP/NWIND.MDB");
// open connection
thisConnection.Open();
// create DataAdapter object for update and other operations
OleDbDataAdapter thisAdapter = new OleDbDataAdapter(
"SELECT CustomerID, CompanyName FROM Customers", thisConnection);
// create CommandBuilder object to build SQL commands
OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);
// create DataSet to contain related data tables, rows, and columns
DataSet thisDataSet = new DataSet();
// fill DataSet using query defined previously for DataAdapter
thisAdapter.Fill(thisDataSet, "Customers");
Console.WriteLine("# rows before change: {0}",
thisDataSet.Tables["Customers"].Rows.Count);
DataRow thisRow = thisDataSet.Tables["Customers"].NewRow();
thisRow["CustomerID"] = "ZACZI";
thisRow["CompanyName"] = "Zachary Zithers Ltd.";
thisDataSet.Tables["Customers"].Rows.Add(thisRow);
Console.WriteLine("# rows after change: {0}",
thisDataSet.Tables["Customers"].Rows.Count);
thisAdapter.Update(thisDataSet, "Customers");
Console.WriteLine("name after change: {0}",
thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);
thisConnection.Close();
}
}
dataset更新例子
最新推荐文章于 2023-04-04 17:47:15 发布