首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MoveFirst DoEvents MoveLast

MoveFirst DoEvents MoveLast
EN

Stack Overflow用户
提问于 2013-10-31 19:49:26
回答 1查看 263关注 0票数 0

这可能是最简单的修复方法,但我需要获得自动编号,并将其存储到一个公共变量中,该变量将用于标识用户所在的会话。当用户注销以关闭会话时,将使用此ID。粗体的代码在ACCESS中被严格使用,但是我现在已经将表移到了SQL中,现在这段代码不能工作了。因此,需要对下面的代码进行修改,以适应该代码的其余部分。我需要记录源成为dbo.tTbl_LoginSessions。稍后将使用LngLoginID。如果有人能帮我做这件事,我将非常感激。我所了解到的是,存储过程不能工作,@@IDENTITY、SCOPE_IDENTITY和IDENT_CURRENT是类似的函数,但我听说这些函数可能是可疑的。这封电子邮件让我看起来比我看起来聪明,但相信我,我不是。因此,我需要婴儿的步子。

代码语言:javascript
复制
    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 

在我转换它之前,这是旧代码。除了自动编号之外,一切都能工作。

代码语言:javascript
复制
 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
EN

回答 1

Stack Overflow用户

发布于 2013-10-31 22:23:02

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

代码语言:javascript
复制
' 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中编写的应用程序运行良好且不需要重写,那么它没有什么问题)。

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

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19715387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档