jamHud
jamHud

Reputation: 103

Inline update statement replace string with variables

I am trying to use an update statement to update a certain record, however I am having difficulty. I would like to replace ‘user’ and ‘timestamp’ with variable values however When I provide inline variables I receive an error, can someone help me to achieve this functionality. I an using sql server as my dbms.

Variables:

Dim timeStamp As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")

Dim user As String = GetUserName()

Update Statement:

strsql = strsql & " Update Activity Set userName = 'user', timeDate = 'timeStamp' Where noteKey = " & lngNoteKey

Upvotes: 0

Views: 353

Answers (2)

Abe Miessler
Abe Miessler

Reputation: 85096

You will want to use parameterized queries to do this.

strsql = strsql & " Update Activity Set userName = @user, timeDate = @date Where noteKey = " & lngNoteKey

and then add the parameters to the command when you execute it:

yourQuery.Parameters.Add(new ObjectParameter("user", user));
yourQuery.Parameters.Add(new ObjectParameter("date", timeStamp));

Upvotes: 2

Thomas
Thomas

Reputation: 1401

strsql = strsql & " Update Activity Set userName = '" & user & "', timeDate = '" & timeStamp & "' Where noteKey = " & lngNoteKey

Try that. concatenate the data into the string.

Upvotes: 1

Related Questions