0

这可能是最简单的解决方法,但我需要获取自动编号并将其存储到一个公共变量中,该变量将用于识别用户所在的会话。当用户注销以关闭会话时使用此 ID。大胆代码在 ACCESS 中严格使用,但我现在已将表移至 SQL,现在此代码不起作用。因此,需要修改下面的代码以适应此代码的其余部分。我需要 Recordsource 是 dbo.tTbl_LoginSessions。LngLoginID 稍后使用。如果有人可以帮助我,我将不胜感激。我了解到的是存储过程不起作用,@@IDENTITY、SCOPE_IDENTITY 和 IDENT_CURRENT 是类似的功能,但我听说这些可能是可疑的。这封电子邮件让我看起来比看起来更聪明,但相信我,我不是。因此,我需要婴儿步骤。

    Function CreateSession()
'This closes the open session
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim strSQL As String
Dim WhoAmI As Long

Dim StrLoginName As String, StrComputerName As String

'passing variables
StrMSID = StrLoginName
StrComputerName = FindComputerName

'Declaring what table you are passing the variables to
strSQL = "Insert into dbo.tTbl_LoginSessions(fldUserName, fldLoginEvent, fldComputerName) Values ('" & StrMSID & "','" & Now() & "','" & StrComputerName & "')"

'connect to SQL Server
Set con = New ADODB.Connection
With con
.ConnectionString = cSQLConn
.Open
End With
'write back
Set cmd = New ADODB.Command
With CMD
.ActiveConnection = con
.CommandText = strSQL
.CommandType = adCmdText
.Execute
End With

'/Next get the autonumber and store it to a public variable that will be used to
'/identify this session.

'/This id is used when user logs off to close this session.
**Rs.MoveFirst**    
**DoEvents**
**Rs.MoveLast**
**LngLoginId = Rs(0)** 
Debug.Print strSQL
'close connections
con.Close
Set cmd = Nothing
Set con = Nothing



End Function 

这是旧代码,在我转换之前。除自动编号外的所有功能均有效

 Function CreateSession(WhoAmi As Long)
    '/This function records the details regarding the login details of the person

    Dim Rs As DAO.Recordset


Set Rs = CurrentDb.OpenRecordset("Tbl_LoginSessions")
    Rs.AddNew
    Rs.Fields("fldUserName").Value = StrLoginName
    Rs.Fields("fldComputerName").Value = StrComputerName
    Rs.Fields("fldLoginEvent").Value = Now()
    Rs.Update
    '/Next get the autonumber and store it to a public variable that will be used to
    '/identify this session.
    '/This id is used when user logs off to close this session.
    Rs.MoveFirst
    DoEvents
    Rs.MoveLast
    LngLoginId = Rs(0)

    Rs.Close
Set Rs = Nothing

End Function
4

1 回答 1

0

好的,你想要这样的东西:(请记住,我没有安装 VB 来检查这个,所以它可能不是 100%)

' Create command
Set Cmd1 = New ADODB.Command

Cmd1.ActiveConnection = Conn1 ' Whatever your open conn object is - you've got a conn already by the looks of things

' Point the command at a sproc
Cmd1.CommandText = "sp_YourSprocName"
Cmd1.CommandType = adCmdStoredProc

' Add parameters
Cm1.Parameters.Append Cmd1.CreateParameter ("fldLoginName", adVarChar, adParamInput, fldLoginName) 
Cm1.Parameters.Append Cmd1.CreateParameter ("fldLoginEvent", adVarChar, adParamInput, fldLoginEvent) 
Cm1.Parameters.Append Cmd1.CreateParameter ("fldComputerName", adVarChar, adParamInput, fldComputerName) 
Cm1.Parameters.Append Cmd1.CreateParameter ("ReturnValue", adInteger, adParamReturnValue) 

' Run the command
Set Rs1 = Cmd1.Execute()

Cmd1.Parameters("ReturnValue").Value ' Contains the return value of the sproc

我不确定这样做有多容易,因为您已经在使用事务而不是存储过程来执行此操作,因为使用事务使整个过程原子化是可行的(然后您可以同时发出 INSERT 和 SCOPE_IDENTITY() 调用去)

...虽然老实说取决于项目规模,如果您正在考虑进一步发展,我会考虑重新实施新技术。VB 有点长了(显然,如果您编写的应用程序运行良好并且您不需要重新编写,那么 VB6 没有任何问题)

实体框架中的相同数据访问代码将是几行真正富有表现力的代码,例如

using(var context = new DatabaseContext())
{
    var newSession = new LoginSession();

    newSession.UserName = "Fred";
    newSession.LoginEvent = "?";
    newSession.ComputerName = "Some Computer Name";

    ctx.LoginSessions.Add(newSession);

    ctx.SaveChanges();

    // newSession.SessionId would contain the new ID
}
于 2013-10-31T22:23:02.663 回答