我試圖做批量插入到數據庫,我以前的嘗試是用短小精悍的批量插入,但它是可怕的,所以我試圖用乾淨的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