gpt4 book ai didi

mysql - 向数据库插入一条sql语句

转载 作者:行者123 更新时间:2023-11-30 00:53:28 25 4
gpt4 key购买 nike

我正在尝试向数据库中插入一条 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 是在方法中作为参数给出的值(这一切都在 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

最佳答案

    <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 库可以使用参数来完成它的工作。您可以获得将要执行的准备好的语句/命令文本,然后将其作为要执行的实际命令中的参数值传递。

关于mysql - 向数据库插入一条sql语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20780555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com