CodeProject - Inserting relational data using DataSet and DataAdapter(下)

博客介绍了在SQL Server 2000中创建数据库结构和插入数据的方法。创建结构时,使用代码替代之前方式,借助SQL存储过程更新数据;插入数据时,过程与处理Access数据库类似,使用特定方法维持关系,确保列值正确更新。
作者: Mauricio Ritter       翻译:小新0574

原文(代码)链接: http://www.codeproject.com/cs/database/relationaladonet.asp

Creating the structure (SQL Server 2000)

We'll start by creating the same structure that we used for Access 2000, but instead of creating the DataAdapter commands using the CommandBuilder, we'll create them by code, since we're going to use a SQL Server stored procedure to update the data.

我们将开始于创建我们为Access2000使用的相同的结构,但是与前面使用CommandBuilder创建DataAdapter命令不同,我们使用代码创建它们,因为我们要用一个SQL储存过程来更新数据。

None.gif //  Create the DataSet object
None.gif
DataSet oDS  =   new  DataSet();
None.gifSqlConnection conn 
=   new  SqlConnection( " Data Source=.;
None.gif
        Initial Catalog = Orders;Integrated Security = SSPI " );
None.gif
conn.Open();
None.gif
None.gif
//  Create the DataTable "Orders" in the Dataset and the OrdersDataAdapter
None.gif
SqlDataAdapter oOrdersDataAdapter  =   new
None.gif    SqlDataAdapter(
new  SqlCommand( " SELECT * FROM Orders " , conn));
None.gif
None.gifoOrdersDataAdapter.InsertCommand 
=   new  
None.gif    SqlCommand(
" proc_InsertOrder " , conn);
None.gifSqlCommand cmdInsert 
=  oOrdersDataAdapter.InsertCommand;
None.gifcmdInsert.CommandType 
=  CommandType.StoredProcedure;
None.gif
None.gifcmdInsert.Parameters.Add(
new  SqlParameter( " @OrderId " , SqlDbType.Int));
None.gifcmdInsert.Parameters[
" @OrderId " ].Direction  =  ParameterDirection.Output;
None.gifcmdInsert.Parameters[
" @OrderId " ].SourceColumn  =   " OrderId " ;
None.gif
None.gifcmdInsert.Parameters.Add(
new  SqlParameter( " @CustomerName "
None.gif                             SqlDbType.VarChar,
50 , " CustomerName " ));
None.gif                             
None.gifcmdInsert.Parameters.Add(
new  SqlParameter( " @ShippingAddress " ,
None.gif                             SqlDbType.VarChar,
50 , " ShippingAddress " ));
None.gif
None.gifoOrdersDataAdapter.FillSchema(oDS, SchemaType.Source);
None.gif
None.gifDataTable pTable 
=  oDS.Tables[ " Table " ];
None.gifpTable.TableName 
=   " Orders " ;
None.gif
None.gif
//  Create the DataTable "OrderDetails" in the
None.gif
//  Dataset and the OrderDetailsDataAdapter
None.gif

None.gifSqlDataAdapter oOrderDetailsDataAdapter 
=   new
None.gif      SqlDataAdapter(
new  SqlCommand( " SELECT * FROM OrderDetails " , conn));
None.gif
None.gifoOrderDetailsDataAdapter.InsertCommand 
=   new  
None.gif      SqlCommand(
" proc_InsertOrderDetails " , conn);
None.gif      
None.gifcmdInsert 
=  oOrderDetailsDataAdapter.InsertCommand;
None.gifcmdInsert.CommandType 
=  CommandType.StoredProcedure;
None.gifcmdInsert.Parameters.Add(
new  SqlParameter( " @OrderId " , SqlDbType.Int));
None.gifcmdInsert.Parameters[
" @OrderId " ].SourceColumn  =   " OrderId " ;
None.gifcmdInsert.Parameters.Add(
new  SqlParameter( " @ProductId " , SqlDbType.Int));
None.gifcmdInsert.Parameters[
" @ProductId " ].SourceColumn  =   " ProductId " ;
None.gifcmdInsert.Parameters.Add(
new  
None.gif   SqlParameter(
" @ProductName " , SqlDbType.VarChar, 50 , " ProductName " ));
None.gifcmdInsert.Parameters.Add(
new  
None.gif   SqlParameter(
" @UnitPrice " , SqlDbType.Decimal));
None.gifcmdInsert.Parameters[
" @UnitPrice " ].SourceColumn  =   " UnitPrice " ;
None.gifcmdInsert.Parameters.Add(
new  SqlParameter( " @Quantity " , SqlDbType.Int ));
None.gifcmdInsert.Parameters[
" @Quantity " ].SourceColumn  =   " Quantity " ;
None.gif
None.gifoOrderDetailsDataAdapter.FillSchema(oDS, SchemaType.Source);
None.gif
None.gifpTable 
=  oDS.Tables[ " Table " ];
None.gifpTable.TableName 
=   " OrderDetails " ;
None.gif
None.gif
//  Create the relationship between the two tables
None.gif
oDS.Relations.Add( new  DataRelation( " ParentChild " ,
None.gif    oDS.Tables[
" Orders " ].Columns[ " OrderId " ],
None.gif    oDS.Tables[
" OrderDetails " ].Columns[ " OrderId " ]));
None.gif

In this piece of code we're manually creating a SqlCommand to do all the inserts in the database table through the DataAdapter. Each SqlCommand calls a stored procedure in the database that has the parameters structure equal to the table structure.

在这段代码里我们手工创建了一个SqlCommand通过DataAdapter来实现数据表的所有插入。每个SqlCommand调用数据库里的一个储存过程,这个数据库有等同于表结构的参数结构。

The most important thing here is the OrderId parameter of the first DataAdapter's command. This parameter has a different direction than the others. The parameter has an output direction and a source column mapped to the OrderId column of the DataTable. With this structure, after each execution, the stored procedure will return the value to this parameter, that will be copied to the OrderId source column. The OrderId parameter receives the @@IDENTITY inside the procedure, like the one below.

在这最重要的东西就是在第一个DataAdapter命令里的OrderId 参数。这个参数跟其他的相比有一个不同的direction这个参数有一个output direction和一个映射到DataTableOrderId列的源列。有了这个结构,在每次执行以后,储存过程就会为这个参数返回一个值,这样这个值就会被拷贝到源OrderId列。OrderId参数在储存过程里接受@@IDENTITY,就像下面的这个:

None.gif CREATE PROCEDURE proc_InsertOrder
None.gif(@OrderId 
int  output,
None.gif @CustomerName varchar(
50 ),
None.gif @ShippingAddress varchar(
50 )
None.gif)
None.gif AS
None.gif
None.gifINSERT INTO Orders (CustomerName, ShippingAddress)
None.gifVALUES
None.gif(@CustomerName, @ShippingAddress)
None.gif
None.gifSELECT @OrderId
= @@IDENTITY
None.gif

Inserting the Data

Now that we set the entire structure, its time to insert the data. The process is exactly the same as we have done with the Access database, using the SetParentRow method to maintain the relationship and guarantee that the IDENTITY column will be copied to the child table (OrderDetails).

现在我们设置了整个结构,是时候来插入数据了。过程就跟我们处理Access数据库一样,使用SetParentRow 方法维持关系,保证IDENTITY列被拷贝到子表里(OrderDetails)

None.gif //  Insert the Data
None.gif
DataRow oOrderRow  =  oDS.Tables[ " Orders " ].NewRow();
None.gifoOrderRow[
" CustomerName " =   " Customer ABC " ;
None.gifoOrderRow[
" ShippingAddress " =   " ABC street, 12345 " ;
None.gifoDS.Tables[
" Orders " ].Rows.Add(oOrderRow);
None.gif
None.gifDataRow oDetailsRow 
=  oDS.Tables[ " OrderDetails " ].NewRow();
None.gifoDetailsRow[
" ProductId " =   1 ;
None.gifoDetailsRow[
" ProductName " =   " Product 1 " ;
None.gifoDetailsRow[
" UnitPrice " =   1 ;
None.gifoDetailsRow[
" Quantity " =   2 ;
None.gif
None.gifoDetailsRow.SetParentRow(oOrderRow);
None.gifoDS.Tables[
" OrderDetails " ].Rows.Add(oDetailsRow);
None.gif
None.gifoOrdersDataAdapter.Update(oDS, 
" Orders " );
None.gifoOrderDetailsDataAdapter.Update(oDS, 
" OrderDetails " );
None.gif
None.gifconn.Close();
None.gif

If you check the database you'll see that the OrderId column is updated with the correct IDENTITY column value.

如果你检查数据库你就会发现OrderId列更新了正确的IDENTITY 列值。

转载于:https://www.cnblogs.com/wdxinren/archive/2005/04/05/132538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值