2013-10-31 78 views
0

這可能是最簡單的修復方法,但我需要獲取自動編號並將其存儲到公用變量中,該變量將用於標識用戶所處的會話。此用戶名在用戶註銷時使用關閉會議。 Bolded代碼在ACCESS中嚴格使用,但現在我已將表移至SQL,現在此代碼不起作用。因此,下面的代碼需要修改以適應其餘的代碼。我需要Recordsource是dbo.tTbl_LoginSessions。 LngLoginID稍後使用。如果有人能幫助我,我將非常感激。我所學到的是一個存儲過程不起作用,@@ IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT是類似的功能,但我聽說這些可能是可疑的。這封電子郵件讓我看起來比我看起來更聰明,但相信我,我不是。因此,我需要寶寶的步驟。MoveFirst DoEvents MoveLast

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

讓我不寒而慄看着VB6 ......但無論如何,範圍身份將正常工作假設你是直接操作 – Charleh

+0

後抓住它會是什麼,我需要更換Rs.MoveFirst行,DOEVENTS ,Rs.MoveLast,LngLoginId = Rs(0),因爲我放在那裏的所有東西似乎都出錯了,似乎對我沒有用處?如果某人在同一時間正確登錄,會發生什麼? –

+0

我現在在我的手機上,但不久就要回家了,當我回來的時候生病會幫助你! – Charleh

回答

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 

我m不知道這樣做有多容易,因爲你已經在使用事務而不是使用事務處理器來完成它,因爲使用事務處理原子的整個過程是可行的(然後你可以一次性發出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 
}