TheChos
asked on
Update or Insert SQL Server 2005 using VB.net
I'd like to update or insert a new record into a SQL Server 2005 database using VB.net.
Knowing that my values will contain single quotes, I'd prefer to update the fields within my tables using the individual field names rather than a blanket INSERT command.
For example, in VB6, I could write something as follows to accomplish the above:
sSql = "SELECT * FROM MyTable"
objMyRst.Open sSql, cnn, adOpenKeyset, adLockOptimistic, adCmdText
If objMyRst.EOF Then
If objMyRst.State = 1 Then objMyRst.Close
sSql = "MyTable"
objMyRst.Open sSql, cnn, adOpenKeyset, adLockOptimistic, adCmdTable
objMyRst.AddNew
End If
objMyRst!Testfield = Now()
objMyRst.Update
Any help in performing both the insert and update would be appreciated.
Knowing that my values will contain single quotes, I'd prefer to update the fields within my tables using the individual field names rather than a blanket INSERT command.
For example, in VB6, I could write something as follows to accomplish the above:
sSql = "SELECT * FROM MyTable"
objMyRst.Open sSql, cnn, adOpenKeyset, adLockOptimistic, adCmdText
If objMyRst.EOF Then
If objMyRst.State = 1 Then objMyRst.Close
sSql = "MyTable"
objMyRst.Open sSql, cnn, adOpenKeyset, adLockOptimistic, adCmdTable
objMyRst.AddNew
End If
objMyRst!Testfield = Now()
objMyRst.Update
Any help in performing both the insert and update would be appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can either use named parameters or escape any potential single quotes in your values by replacing single quote characters with two single quote characters.
--
Chris
--
Chris
ASKER
Got it. thank you
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for TheChos's comment #a39751583
for the following reason:
Very quick response and easy to follow directions.
Accepted answer: 0 points for TheChos's comment #a39751583
for the following reason:
Very quick response and easy to follow directions.
If you are happy with the feedback you got, then assign points.
If you need additional help, please ask.
And if you want to delete the questions and keep your points, then indicate so as well.
If you need additional help, please ask.
And if you want to delete the questions and keep your points, then indicate so as well.
ASKER
Very responsive and easy to follow directions. Thank you.
You are very welcome and thanks for the points.
ASKER