[专
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
]OleDbDataAdapter 插入记录
OleDbDataAdapter 插入记录
DataSet 中所作的更改发回数据源。
当 OleDbDataAdapter 填充 DataSet 时,它将为返回的数据创建适当的
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
和列(如果它们尚不存在)。但是,除非 MissingSchemaAction 属性设置为 AddWithKey,否则这个隐式创建的架构中不包括主键信息。也可以使用 FillSchema,让 OleDbDataAdapter 创建 DataSet 的架构,并在用数据填充它之前就将主键信息包括进去。有关更多信息,请参见关于C#调用API向 DataSet 添加现有约束。
请注意,c#操作ACCESS数据库包括 MSDataShape 提供程序在内的某些 OLE DB 提供程序并不返回基表或主键信息。因此,OleDbDataAdapter 无法对任何已创建的 DataTable 正确设置 PrimaryKey 属性。在这些情况下,应该为 DataSet 中的表显式地指定主键。
OleDbDataAdapter 还包括 SelectCommand、InsertCommand、DeleteCommand、UpdateCommand 和 TableMappings 属性,c#连接MDB,SQL数据库代码以便于数据的加载和更新。
当创建 OleDbDataAdapter 的实例时,属性都设置为其初始值。有关这些值的列表,请参见 OleDbDataAdapter 构造函数。
OleDbDataAdapter 充当 DataSet 和数据源之间的桥梁,用于检索和保存数据。OleDbDataAdapter 通过以下方法提供这个桥接器:使用 Fill 将数据从数据源加载到 DataSet 中,并使用 Update 将 DataSet 中所作的更改发回数据源。
当 OleDbDataAdapter 填充 DataSet 时,它将为返回的数据创
建适当的表和列(如果它们尚不存在)。但是,除非
MissingSchemaAction 属性设置为 AddWithKey,否则这个隐式创建的架构中不包括主键信息。也可以使用 FillSchema,让
OleDbDataAdapter 创建 DataSet 的架构,并在用数据填充它之前就将主键信息包括进去。
请注意,包括 MSDataShape 提供程序在内的某些 OLE DB 提供程序并不返回基表或主键信息。因此,OleDbDataAdapter 无法对任何已创建的 DataTable 正确设置 PrimaryKey 属性。在这些情况下,应该为 DataSet 中的表显式地指定主键。
OleDbDataAdapter 还包括 SelectCommand、InsertCommand、DeleteCommand、UpdateCommand 和 TableMappings 属性,以便于数据的加载和更新。
public static OleDbDataAdapter CreateDataAdapter(string
selectCommand,
OleDbConnection connection)
{
OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the Insert, Update and Delete commands.
adapter.InsertCommand = new OleDbCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (?, ?)");
adapter.UpdateCommand = new OleDbCommand(
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
"WHERE CustomerID = ?");
adapter.DeleteCommand = new OleDbCommand(
"DELETE FROM Customers WHERE CustomerID = ?");
// Create the parameters.
adapter.InsertCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add("@CompanyName",
OleDbType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID");
adapter.UpdateCommand.Parameters.Add("@CompanyName",
OleDbType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
OleDbType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
return adapter;
}
using System;
using System.Data;
using System.Data.OleDb;
class Class1
{
static void Main()
{
}
public static OleDbDataAdapter CreateDataAdapter(string
selectCommand,
OleDbConnection connection)
{
OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the Insert, Update and Delete commands.
adapter.InsertCommand = new OleDbCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (?, ?)");
adapter.UpdateCommand = new OleDbCommand(
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
"WHERE CustomerID = ?");
adapter.DeleteCommand = new OleDbCommand(
"DELETE FROM Customers WHERE CustomerID = ?");
// Create the parameters.
adapter.InsertCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add("@CompanyName",
OleDbType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID");
adapter.UpdateCommand.Parameters.Add("@CompanyName",
OleDbType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
OleDbType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
return adapter;
}