|
I'm using the following code to add a record to a SQL database.
When the record is inserted how can I get the primary key value the database assigns to a field in that record when it is inserted.
Thanks
Dim DBCommand1 As SqlCommand
NonQueryText = "insert into mytable(field1)values(@field1)
DBCommand1 = New SqlCommand(NonQueryText, Connection1)
DBCommand1.Parameters.Add("@field1", SqlDbType.Char,10, "field1").Value = "abc"
Connection1.Open()
DBCommand1.ExecuteNonQuery()
|
|
|
|
|
Use the query "select @@identity" to get it.
@@identity contains the last identity created with the same connection.
---
b { font-weight: normal; }
|
|
|
|
|
I believe @@IDENTITY is for Access DB, if you are using SQL 2000 use SCOPE_IDENTITY()
|
|
|
|
|
The @@identity property works in Access also.
You can use scope_identity() or @@identity in SQL Server. They work very similar, the only time there is any difference between them is if you are using triggers that also insert records.
---
b { font-weight: normal; }
|
|
|
|
|
Using the following sql throws an error
"select @@contactdataid from contactdata"
What else must I do? Thanks.
|
|
|
|
|
Just use the query "select @@identity", as I said.
---
b { font-weight: normal; }
|
|
|
|
|
Sorry to ask elementary questions here but @@identity does not have any value. After I execute the query "select @@identity" how can I get this value into a varable? How does it know which field in the table to use?
|
|
|
|
|
The query only returns one field in one record. Use ExecuteScalar to run the query and get the single value.
---
b { font-weight: normal; }
|
|
|
|
|
I am doing a project that requires me to send the users and alert message to their cellphone as soon as any new changes made to the service.. I am presently using vb.net to develop this online application.. how do i go about sending alert message to the person's cell phone. if anyone have the coding for it.. please help..
Nab
|
|
|
|
|
Not sure if cellphones have this, but when we wanted to send system outage messages to our pagers we would send an email to the email gateway provided by the pager company with an address of the beeper number.
EX. 805551212@cellphone.com, you need to check with the cellp[hone provider to see if they support this.
Matthew Hazlett
|
|
|
|
|
now days every E-commerce project required alert message to cell phone
this is very useful, if any body have code or guidance please post.
|
|
|
|
|
Hi all,
In Visual Basic 6 I'm trying to insert data into a db that is retrieved from a file. When I execute the insert statement ("Connection.Execute") I receive some errors because there are records that have apostrophes. How can I escape this character?
Thanks in advance,
Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
|
|
|
|
|
Let me gues, you''re concatenting SQL statements together like this:
Dim sql As String
sql = "INSERT INTO table field1, field2, field3 VALUES ('" & textbox.text & "', '" & textbox2.text & "' " & textbox3.text & "')"
This is completely the wrong way to do it and you'll run into escaping problems, like you've discovered, and you also open yourself up to SQL Injection attacks.
Use parameterized queries and you won't have this problem at all.
See this[^] article by Collin Angus Mackay for more information.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi Dave, now after reading the article I'm more concerned about the escaping the apostrophes and I've parameterised the query.
Thanks,
Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
|
|
|
|
|
Yeah, the Parameter class takes care of escaping any characters that need it itself, as well as checking for certain types of injection attacks.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
You escape apostrophes with apostrophes.
For example, this string...
"Bob's Data"
...becomes this...
"Bob''s Data"
|
|
|
|
|
Ok, but is there a function to do it or do I have parse the string to look for the apostrophe?
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
|
|
|
|
|
There's no function to convert it for you. But it's really easy to do it yourself. Try using this...
Public Function StringToSql(sBuf As String) As String
StringToSql = Replace(sBuf, "'", "''")
End Function
|
|
|
|
|
Let me reiterate that unless you take Dave's advice, you'll solve the problem today, but have more grief in the future, because you're choosing to do things the wrong way.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thanks Chris, your persistence have convinced me to take the rigth way.
... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits
|
|
|
|
|
thnx 4 da help actually i required that when i add data into a dataset it is automatically sorted and then saved into a dataset during run time (if i can get the code for help it'l b a gr8 help)
FRAZ SOOMRO
|
|
|
|
|
i want a sample tutorial (or code) in vb.net that demonstrate on parallel port proghramming for doing simple program?????
|
|
|
|
|
That's rather vauge. What are you doing with the parallel port?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
i want to connect a small motor (simple) to the parallel port and control it by using a vb.net program??
i want a sample program and howto connect it??
|
|
|
|
|
Well, since this is a very specific, and relatively rare, project, you're probably going to have to do your own research. The "perfect" example, with everything you want, probably doesn't exist in one place.
I suggest Googling for "parallel port motor" for any information. You'll probably have to piece together your project from various sources.
You can start with this[^] one, right here on Code Project.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|