在具有GUID主键和默认newsequentialid()的表上使用sqlBulkCopy时
例如
CREATE TABLE [dbo].[MyTable]( [MyPrimaryKey] [uniqueidentifier] NOT NULL CONSTRAINT [MyConstraint] DEFAULT (newsequentialid()),[Status] [int] NULL,[Priority] [int] NULL,CONSTRAINT [PK_MyTable] PRIMARY KEY NONCLUSTERED ( [MyPrimaryKey] ASC )WITH (PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
与C#代码
tran = connection.BeginTransaction(); sqlBulkCopy sqlCopy = new sqlBulkCopy(connection,sqlBulkCopyOptions.Default,tran); sqlCopy.DestinationTableName = "MyTable"; sqlCopy.WriteToServer(dataTable);
给你一个错误……
列’MyPrimaryKey’不允许DBNull.Value
我试过摆弄sqlBulkCopyOptions.唯一有效的方法是将MyPrimaryKey字段设置为允许空值并删除主键.