3
我正在使用VS2008,V3.5将VB6 Windows应用程序转换为C#。我有一个用于数据存储和检索的SQL Server 2000数据库。一个表保存一个记录是int类型,用于生成报价数量,即123456在VB6我做了以下内容:C#等效于adodb记录集
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
在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();
不幸的是,我发现现在没有游标锁定“adLockPessimistic”,当多人同时击中记录时,同一索引号的多个实例可以显示出来。如果任何人都可以解释如何在C#中使用ADODB.Recordset用于此特定目的,或者使用记录锁定以便在需要时锁定数据库记录,或者使用.Net框架和C#主体中的更好方法来完成相同的事情,我会很感激。提前谢谢了。
诺埃尔
你看过ADO.NET吗? – 2011-12-15 14:14:58