Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I have an INSERT statement that looks like this:

 

Dim strSQL As String = "INSERT INTO ULogRec ( ULDate, ULTime, ULName ) " & _
                              "VALUES (@Dat, @Tim, @UName)"

 

This works fine with an executenonquery statement.

 

But, I want to use @@IDENTITY with an ExecuteScalar statement to retrieve the value of the row that I just inserted into the database and assign this value to an integer variable.

 

The primary key of the table is an autonumber type.

 

Any ideas?

 

Thanks,

Chester

Edited by cpopham

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Posted (edited)

Got it:

 

myCom.CommandText = "SELECT @@Identity"
myCom.CommandType = CommandType.Text
intCLogKey = CInt(myCom.ExecuteScalar())

Edited by cpopham

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Posted

instead of sending 2 queries to the server, u can save a round trip by combinding the 2 sql queries and invoking ExecuteScalar.

 

e.x.:

myCom.CommandText = "INSERT INTO ULogRec ( ULDate, ULTime, ULName ) " & _

"VALUES (@Dat, @Tim, @UName);" &_ "SELECT @@Identity"

myCom.CommandType = CommandType.Text

intCLogKey = CInt(myCom.ExecuteScalar())

Posted

Thanks, yeah that will cut down on database trips. I like your idea of combining both commands into one execution.

Thank you for the help :)

 

Chester

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Posted

When I combine both statements into one, I get an error: Here is my code:

 

      Dim strSQL As String = "INSERT INTO ULogRec ( ULDate, ULTime, ULName ) " & _
                              "VALUES (@Dat, @Tim, @UName) " & _
                              "SELECT @@IDENTITY;"

 

Now that code throws the error:

Missing semicolon (;) at end of SQL statement

 

       Dim strSQL As String = "INSERT INTO ULogRec ( ULDate, ULTime, ULName ) " & _
                              "VALUES (@Dat, @Tim, @UName); " & _
                              "SELECT @@IDENTITY "

 

The error for that statement reads:

Characters found after end of SQL statement.

 

My execution code looks like this:

myCom.CommandType = CommandType.Text
       Try
           intULogKey = CInt(myCom.ExecuteScalar())
       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try

 

My backend is an Access database. Anyideas as to why I can not combine both statements into one?

 

Chester

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Posted

Yeah I thought using stored procedures it might support it. I am going to switch this to MySQL later and it should support multiple statements :)

 

Thanksm Chester

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Posted
well, it depends which mysql driver/api you're using. I created and use an ado.net client/driver using the mysql c api and even though mysql 4.1 and above support multiple queries in a single statement, it only applies to unparameterized queries. The official .net provider uses the undocumented network protocol, so I think the multiple queries will probably work. I also think the odbc client is outdated should you choose to use that.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...