插入多行数据时对表的锁定

来源:互联网 发布:定静安虑得 知乎 编辑:程序博客网 时间:2024/05/16 15:12

表结构如下:

CREATE TABLE Sync_Bas_Customer(
 [fSyncID] [int] IDENTITY (1, 1) NOT NULL ,
 [fChangeType] [smallint] NOT NULL ,
 [fOrgID] [int] NOT NULL ,
 [fSyncLogID] [uniqueidentifier] NULL ,
 [fSync_fCustID] [int] NOT NULL
) ON [PRIMARY]

//测试代码

int i = 0;
while (i++ < 10)
{
    SqlHelper.ExecuteNonQuery(trans, CommandType.Text,
        "Insert into Sync_Bas_Customer with(tablock)(fChangeType,fOrgID,fSync_fCustID)" +
        "Values(0,1,"+this.textBox1.Text+")");
    System.Threading.Thread.Sleep(500);
}

//测试结论

如果不使用with(tablock)则并发执行时将不会按顺序生成数据