SqlBulkCopy WriteToServer示例。我究竟做错了什么?

c# sql sqlbulkcopy sql-server

这可能很长,但我想解释一下我的例子

我得到了这个代码:

#region [parent table]
DataTable dtParent = new DataTable();
DataColumn dc;

dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "Id";
dc.Unique = true;
dc.AutoIncrement = true;
dtParent.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Title";
dc.Unique = false;
dtParent.Columns.Add(dc);

dtParent.TableName = "aTestSw";
dtParent.PrimaryKey = new DataColumn[] { dtParent.Columns[0] };
#endregion

#region [child table]
DataTable dtChild = new DataTable();

dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "Id";
dc.Unique = true;
dc.AutoIncrement = true;
dtChild.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "ParentId";
dc.Unique = false;
dtChild.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Description";
dc.Unique = false;
dtChild.Columns.Add(dc);

dtChild.TableName = "aTestSwChild";
dtChild.PrimaryKey = new DataColumn[] { dtChild.Columns[0] };
#endregion

DataSet DataSet1 = new DataSet();
DataSet1.Tables.Add(dtParent);
DataSet1.Tables.Add(dtChild);

#region [fk]
DataColumn parentColumn = dtParent.Columns["Id"];
DataColumn childColumn = dtChild.Columns["ParentId"];
DataRelation relParentChild = new DataRelation("ParentChild", 
    parentColumn, childColumn);
DataSet1.Relations.Add(relParentChild); 
#endregion

#region [fill parent]
DataRow dr2saveIn = dtParent.NewRow();
dr2saveIn["Title"] = "a";
dtParent.Rows.Add(dr2saveIn);

dr2saveIn = dtParent.NewRow();
dr2saveIn["Title"] = "b";
dtParent.Rows.Add(dr2saveIn);
#endregion

#region [fill child]
dr2saveIn = dtChild.NewRow();
dr2saveIn["Description"] = "c";
dr2saveIn["ParentId"] = dtParent.Rows[0]["Id"];
dtChild.Rows.Add(dr2saveIn);

dr2saveIn = dtChild.NewRow();
dr2saveIn["Description"] = "d";
dr2saveIn["ParentId"] = dtParent.Rows[1]["Id"];
dtChild.Rows.Add(dr2saveIn);   
#endregion

SqlBulkCopy bulkCopy = new SqlBulkCopy(
    "server=aaa;database=bbb;uid=ccc;password=ddd", 
    SqlBulkCopyOptions.TableLock);
bulkCopy.DestinationTableName = "dbo.aTestSw";
bulkCopy.WriteToServer(dtParent);
bulkCopy.DestinationTableName = "dbo.aTestSwChild";
bulkCopy.WriteToServer(dtChild);

当我运行它时,我在子表的FK列中获得了“看起来”正确的数据。 (0和1)事情是,保存后,数据进入数据库,但它不是很好。我没有从父表中获取FK id,而是从C#代码获得了0和1。注意:DB中的表有对它们的FK约束。你不能手动添加那些东西,但SqlBulkCopy WriteToServer没有问题,反正写错了数据(就像它没有检查约束)

查看表格:

CREATE TABLE [dbo].[aTestSw](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Title] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_aTestSw] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[aTestSwChild](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [ParentId] [int] NOT NULL,
 [Description] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_aTestSwChild] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[aTestSwChild]  WITH NOCHECK ADD  CONSTRAINT [FK_aTestSwChild_aTestSw] FOREIGN KEY([ParentId])
REFERENCES [dbo].[aTestSw] ([Id])

ALTER TABLE [dbo].[aTestSwChild] CHECK CONSTRAINT [FK_aTestSwChild_aTestSw]

结果如下所示:

select * from aTestSw

Id          Title
16          a
17          b

select * from aTestSwChild

Id          ParentId    Description
12          0           c
13          1           d

我究竟做错了什么?可能我不应该在C#中得到这样的Id。但是怎么样?或者甚至可能吗?我的意思是我的第二个玩法怎么会知道SQL !!!!插入ID?

一般承认的答案

使用此选项:

SqlBulkCopyOptions.KeepIdentity

在批量复制操作之前,将数据库的CheckConstraints设置为OFF。在此之后使用批量复制插入数据。完成所有工作后,将CheckConstraints设置为ON。

上行选项强制您的数据库保持您的身份值。


热门答案

默认情况下,SqlBulkCopy会将您的外键变为nocheck外键。您需要添加选项SqlBulkCopyOptions.CheckConstraints以保持外键检查所有现有数据和所有新数据。




许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因