我试图做批量插入到数据库,我以前的尝试是用短小精悍的批量插入,但它是可怕的,所以我试图用干净的ADO.NET 方法为什么DataRow中的时间戳不是自动生成的?
我生成的数据表,并从列表填充它项目:这里
using (SqlConnection cn = new SqlConnection(VoucherConnectionManager.connectionString))
{
cn.Open();
var cmd = new SqlCommand("SET FMTONLY ON; SELECT * FROM Voucher; SET FMTONLY OFF;", cn);
var dt = new DataTable();
dt.Load(cmd.ExecuteReader());
foreach (var itm in voucherList)
{
DataRow row = dt.NewRow();
row["VoucherUid"] = itm.Uid;
row["ClientUid"] = itm.ClientUid;
row["BatchUid"] = itm.BatchUid;
row["CardNo"] = itm.CardNo;
row["Origin"] = itm.Origin;
row["VoucherCreateDate"] = itm.VoucherCreateDate;
row["State"] = itm.State;
//row["LastTimeStamp"] = DateTime.Now;
dt.Rows.Add(row);
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(cn))
{
bulkCopy.DestinationTableName = "dbo.Voucher";
bulkCopy.WriteToServer(dt);
}
cn.Dispose();
}
问题是,我收到以下错误:
An unhandled exception of type 'System.Data.NoNullAllowedException' occurred in MasterRepositorys.dll Additional information: Column 'LastTimeStamp' does not allow nulls.
LastTimeStamp是一个时间戳列,为什么不自动生成?我做错了什么?
编辑 表DEF:与时间戳数据类型的SQL表列产生
CREATE TABLE [dbo].[Voucher](
[VoucherUid] [uniqueidentifier] NOT NULL,
[ClientUid] [uniqueidentifier] NOT NULL,
[BatchUid] [uniqueidentifier] NOT NULL,
[CardNo] [nvarchar](13) NOT NULL,
[CycleUid] [uniqueidentifier] NULL,
[CycleCreateDate] [datetime] NULL,
[VoucherCreateDate] [datetime] NOT NULL,
[LastTimeStamp] [timestamp] NOT NULL,
[Type] [int] NULL,
[Origin] [int] NOT NULL,
[OrderRef] [nvarchar](36) NULL,
[State] [int] NOT NULL,
CONSTRAINT [PK_Voucher] PRIMARY KEY CLUSTERED
(
[VoucherUid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
它真的是时间戳(又名rowversion)字段,而不仅仅是日期时间?如果是日期时间,则需要为其设置默认值,或者在插入子句中提供 –
您可以将实际的表定义显示为“CREATE TABLE”语句吗?请注意,SQL Server中的'timestamp'具有与时间无关的特定含义。 –
添加表定义 – Timsen