我正在尝试在数据库中插入一个sql语句,因为其中一个变量包含一个sql语句。但是,此语句具有参数,它在数据库中显示为@parm而不是实际值。有什么想法吗?
这是声明:
Dim cmd As New SqlCommand("insert into MyTableOne (ID, QueryText) values (3,'insert into MyTableTwo (Name,Image,ClientId,Taskcode,TaskYear,Notes,ImgDT,Flag) values (@name,@img,@clientid,@taskcode,@taskyear,@notes,@imgdt,@flag)')", con)
这就是我如何填写价值观 cmd.Parameters.AddWithValue(“@ name”,Name)和Name是方法中作为parm给出的值 (这是一个Web服务方法,然后从提供值的应用程序调用)
新:
Dim cmd As New SqlCommand("insert into FTWebUploadQueue2_GVT (ID, QueryText) values (4,'insert into IMAGES_GVT (Name,Image,ClientId,Taskcode,TaskYear,Notes,ImgDT,Flag) values ('+ @name + ',' + @img + ',' + @clientid + ',' + @taskcode + ',' + @taskyear + ',' + @notes + ',' + @imgdt + ',' + @flag +')')", con)
<WebMethod()> _
Public Function SaveImageInBackoffice(ByVal imageData As Byte(), ByVal Name As String, ByVal ClientId As String, ByVal Taskcode As Integer, ByVal TaskYear As Integer, ByVal Notes As String, ByVal ImgDT As DateTime, ByVal Flag As Integer) As Boolean
Dim con As New SqlConnection("Server=****;uid=**;pwd=****;database=ImagesTrial")
If con.State = ConnectionState.Closed Then con.Open()
Dim cmd As New SqlCommand("insert into FTWebUploadQueue3_GVT (ID, QueryText) values (4,'insert into IMAGES_GVT (Name,Image,ClientId,Taskcode,TaskYear,Notes,ImgDT,Flag) values ('+ CAST(@name AS VARCHAR) + ',' + CAST(@img AS VARCHAR) + ',' + CAST(@clientid AS VARCHAR) + ',' + CAST(@taskcode AS VARCHAR) + ',' + CAST(@taskyear AS VARCHAR) + ',' + CAST(@notes AS VARCHAR) + ',' + CAST(@imgdt AS VARCHAR) + ',' + CAST(@flag AS VARCHAR) +')')", con)
cmd.Parameters.AddWithValue("@img", imageData)
cmd.Parameters.AddWithValue("@name", Name)
cmd.Parameters.AddWithValue("@clientid", ClientId)
cmd.Parameters.AddWithValue("@taskcode", Taskcode)
cmd.Parameters.AddWithValue("@taskyear", TaskYear)
cmd.Parameters.AddWithValue("@notes", Notes)
cmd.Parameters.AddWithValue("@imgdt", ImgDT)
cmd.Parameters.AddWithValue("@flag", Flag)
cmd.ExecuteNonQuery()
con.Close()
Return 0
End Function
答案 0 :(得分:0)
<WebMethod()> _
Public Function SaveImageInBackoffice(ByVal imageData As Byte(), ByVal Name As String, ByVal ClientId As String, ByVal Taskcode As Integer, ByVal TaskYear As Integer, ByVal Notes As String, ByVal ImgDT As DateTime, ByVal Flag As Integer) As Boolean
Dim con As New SqlConnection("Server=****;uid=**;pwd=****;database=ImagesTrial")
If con.State = ConnectionState.Closed Then con.Open()
Dim qry As New SqlCommand("insert into IMAGES_GVT (Name,Image,ClientId,Taskcode,TaskYear,Notes,ImgDT,Flag) values (@name,@img,@clientid,@taskcode,@taskyear,@notes,@imgdt,@flag", con)
qry.Parameters.AddWithValue("@img", imageData)
qry.Parameters.AddWithValue("@name", Name)
qry.Parameters.AddWithValue("@clientid", ClientId)
qry.Parameters.AddWithValue("@taskcode", Taskcode)
qry.Parameters.AddWithValue("@taskyear", TaskYear)
qry.Parameters.AddWithValue("@notes", Notes)
qry.Parameters.AddWithValue("@imgdt", ImgDT)
qry.Parameters.AddWithValue("@flag", Flag)
Dim query as String = qry.CommandText
Dim cmd As New SqlCommand("insert into FTWebUploadQueue3_GVT (ID, QueryText) values (4,@query", con)
cmd.Parameters.AddWithValue("@query", query)
cmd.ExecuteNonQuery()
con.Close()
Return 0
End Function
这让SqlClient库可以通过参数完成它的工作。您可以获取将执行的预准备语句/命令文本,然后将其作为参数值传递给您要执行的实际命令。