@@IDENTIFIER and Insert

  • Thread starter Thread starter Gansoinat
  • Start date Start date
G

Gansoinat

Hi,

During an insert command, i'm using @@IDENTIFIER to
obtain the key (auto increment colonne).
4 users insert record in the same time.
result :
user1 : Key=1
user2 : Key=2
user3 : Key=2
user4 : Key=3
I've problem because the insert command is good but i've
only 3 new records ....
Could you help me please ?
..
 
Hi,

If you are using Sql server then more adequeate method would be
SCOPE_IDENTITY.
What does your code look like?
 
Thank you for you reply,

My data base is access (very small trafic)

I think that problem is in my server session allocation time. When i'm
starting 4 user very quickly (on the server for exemple), 2 on this have
the same session.sessionID
One question please : how to create a good trace session (time, userID
etc...?)

For this moment, here is my code :

Web.config
---------------
<trace enabled="false" requestLimit="10" pageOutput="false"
traceMode="SortByTime" localOnly="true" />
<sessionState
mode= "InProc"
stateConnectionString="tcpip=localhost:42424"
sqlConnectionString="data
source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="1"
/>
MyPage.aspx
----------------
Dim strConn As String = "Provider=Microsoft.Jet.OleDb.4.0;Data
Source=""C:\Inetpub\wwwroot\FG2\BD0.mdb"""
Dim cn As New OleDbConnection(strConn)
Dim da As OleDbDataAdapter
Dim cmdRefresh As OleDbCommand

'this proc is on the page_load event but later, in the session_start
event......

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
cn.Open()
da = CreateStandardDataAdapter()
cmdRefresh = CreateRefreshCommand()
AddHandler da.RowUpdated, AddressOf HandleRowUpdated
Response.Write(RunTest())
Response.Write("<BR>" & Application("SessCount"))
RemoveHandler da.RowUpdated, AddressOf HandleRowUpdated
End Sub

Private Function CreateStandardDataAdapter() As OleDbDataAdapter
Dim da As OleDbDataAdapter
Dim cmd As OleDbCommand
Dim strSQL As String

strSQL = "SELECT ID,SessionID,SessionStart FROM Sessions ORDER
BY ID"
da = New OleDbDataAdapter(strSQL, cn)

strSQL = "INSERT INTO Sessions (SessionID,SessionStart) VALUES
(?,?)"
cmd = New OleDbCommand(strSQL, cn)
da.InsertCommand = cmd
cmd.Parameters.Add("@SessionID", OleDbType.VarChar, 32,
"SessionID")
cmd.Parameters.Add("@SessionStart", OleDbType.VarChar, 32,
"SessionStart")
Return da
End Function
Private Function CreateRefreshCommand() As OleDbCommand
Return New OleDbCommand("SELECT @@IDENTITY", cn)
End Function
Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As
OleDbRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue AndAlso _
(e.StatementType = StatementType.Insert) Then
e.Row("ID") = CInt(cmdRefresh.ExecuteScalar)
e.Row.AcceptChanges()
End If
End Sub
Private Function RunTest() As String
Dim strResults As New IO.StringWriter
Dim tbl As DataTable = CreateDataTable()
Dim row As DataRow
row = tbl.NewRow
row("SessionID") = Session.SessionID
row("SessionStart") = DateTime.Now.ToLongTimeString.ToString
tbl.Rows.Add(row)
da.Update(tbl)
For Each row In tbl.Rows
strResults.WriteLine("<BR>" & row("ID") & "<BR>" &
row("SessionID") & "<BR>" & row("SessionStart"))
Next row
Return strResults.ToString
End Function
Private Function CreateDataTable() As DataTable
Dim tbl As New DataTable
Dim col As DataColumn
col = tbl.Columns.Add("ID", GetType(Integer))
col.AutoIncrement = True
col.AutoIncrementSeed = -1
col.AutoIncrementStep = -1
tbl.Columns.Add("SessionID", GetType(String))
tbl.Columns.Add("SessionStart", GetType(String))
tbl.PrimaryKey = New DataColumn() {col}
Return tbl
End Function
 
Back
Top