[英]C# Equivalent of adodb recordsets
I am converting a VB6 windows application to C# using VS2008, V3.5. 我正在使用VS2008,V3.5将VB6 windows应用程序转换为C#。 I have a SQL Server 2000 database I use for data storage and retrieval.
我有一个SQL Server 2000数据库,用于数据存储和检索。 One table holds a single record that is of type Int and is used for generating a quote number, ie, 123456. In VB6 I do the following:
一个表包含一个Int类型的记录,用于生成报价编号,即123456.在VB6中,我执行以下操作:
OpenDBCon
Set rst = New ADODB.Recordset
rst.Open "SELECT idx From tblQuoteIdx", cn, adOpenDynamic, adLockPessimistic
Select Case rst.BOF
Case False
rst.MoveFirst
Me.txtRef = rst!idx
tID = rst!idx + 1
OKToContinue = True
Case False
'Do something
End Select
If OKToContinue = True Then
Set rst = New ADODB.Recordset
rst.Open "update tblQuoteIdx set idx = '" & tID & "' ", cn, adOpenDynamic,
adLockPessimistic
End If
CloseDBCon
In C# I currently am doing this: 在C#中,我目前正在这样做:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = Vars.connstring;
conn.Open();
SqlCommand sqlComm = new SqlCommand("Select idx from tblQuoteIdx", conn);
Int32 tt = (Int32)sqlComm.ExecuteScalar();
Int32 tt2 = tt++;
SqlCommand sqlComm2 = new SqlCommand("Update tblQuoteIdx set " +
"idx = " + tt2 + "", conn);
sqlComm.ExecuteNonQuery();
conn.Close();
Unfortunately I find now without a cursor lock of "adLockPessimistic" when several people hit the record at the same time multiple instances of the same index number can show up. 不幸的是,当几个人同时点击同一索引号的多个实例时,我发现没有“adLockPessimistic”的游标锁定。 If anyone could either explain how to use an ADODB.Recordset in C# for this specific purpose and or use record locks so as to lock the db record when needed, OR a better way within the .Net framework and C# principals to accomplish the same thing, I would be greatful.
如果有人可以解释如何在C#中使用ADODB.Recordset用于此特定目的,或者使用记录锁以便在需要时锁定db记录,或者在.Net框架和C#主体内更好地完成同样的事情。 ,我会很高兴。 Many thanks in advance.
提前谢谢了。
Noel 诺埃尔
I believe you will find this article useful: Pessimistic locking in ado.net Basically you will need to wrap everything in a transaction that applies a lock on the records as soon as editing starts. 我相信你会发现这篇文章很有用: ado.net中的悲观锁定基本上你需要将一切都包装在一个事务中,一旦编辑开始就对记录施加锁定。 This way no other method will be able to edit the values until the lock is released.
这样,在释放锁之前,没有其他方法可以编辑值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.