内联更新语句将字符串替换为变量

时间:2022-03-08 23:10:58

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.

我正在尝试使用更新语句来更新某个记录,但是我遇到了困难。我想用变量值替换'user'和'timestamp'但是当我提供内联变量时我收到错误,有人可以帮我实现这个功能。我使用sql server作为我的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

2 个解决方案

#1


2  

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));

#2


1  

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

Try that. concatenate the data into the string.

试试吧。将数据连接到字符串中。

#1


2  

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));

#2


1  

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

Try that. concatenate the data into the string.

试试吧。将数据连接到字符串中。