这可能是最简单的修复,但我需要获取自动编号并将其存储到一个公共变量中,该变量将用于标识用户所在的会话。当用户注销关闭会话时使用此ID 。 Bolded 代码在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
答案 0 :(得分: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
我不确定执行此操作是多么容易,因为您已经使用事务而不是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
}