gpt4 book ai didi

asp.net - SqlCommand 占位符参数 : "Incorrect syntax near ' ?'" and "Must declare the scalar variable @param"?

转载 作者:行者123 更新时间:2023-12-02 05:40:40 25 4
gpt4 key购买 nike

我正在为我们公司开发网络 map 应用程序内部网。我正在 map 上创建一个“添加多边形”功能。我使用 AspMap、VB.NET 和 SQL Server。当用户单击按钮以从 Web 表单输入数据属性添加新记录时,发生错误Incorrect syntax near '?'

我的代码是:

Private Sub AddNewShape(ByVal checklist_id As String, ByVal type As String, ByVal shape As AspMap.Shape, ByVal address_area As String, ByVal dmz As String, ByVal customerid As String, ByVal source As String, ByVal area As String, ByVal instalatur As String, ByVal developer As String, ByVal data_received As DateTime, ByVal doc_data As DateTime, ByVal datereport As DateTime, ByVal remark As String)
Dim tableName As String

Select Case shape.ShapeType
Case AspMap.ShapeType.Line
tableName = "lines"
Case AspMap.ShapeType.Polygon
tableName = "sambungan_baru"
Case Else
Return
End Select

Dim conn As SqlConnection = GetDbConnection()
Dim sql As String = "INSERT INTO " & tableName & " (CHECKLIST_ID, TYPE, SHAPEDATA, ADDRESS_AREA, DMZ, CUSTOMERID, SOURCE, AREA, INSTALATUR, DEVELOPER, DATA_RECEIVED, DOC_DATA, DATA_SENT, REMARK) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

Dim cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("CHECKLIST_ID", checklist_id)
cmd.Parameters.AddWithValue("TYPE", type)
cmd.Parameters.AddWithValue("SHAPEDATA", shape.ShapeData)
cmd.Parameters.AddWithValue("ADDRESS_AREA", address_area)
cmd.Parameters.AddWithValue("DMZ", dmz)
cmd.Parameters.AddWithValue("CUSTOMERID", customerid)
cmd.Parameters.AddWithValue("SOURCE", source)
cmd.Parameters.AddWithValue("AREA", area)
cmd.Parameters.AddWithValue("INSTALATUR", instalatur)
cmd.Parameters.AddWithValue("DEVELOPER", developer)
cmd.Parameters.AddWithValue("DATA_RECEIVED", data_received)
cmd.Parameters.AddWithValue("DOC_DATA", doc_data)
cmd.Parameters.AddWithValue("DATA_SENT", datereport)
cmd.Parameters.AddWithValue("REMARK", remark)

cmd.ExecuteNonQuery()
conn.Close()

ReloadShapesDatabase()
End Sub

我改变了这个:

Dim sql As String = "INSERT INTO " & tableName & " (CHECKLIST_ID, TYPE, SHAPEDATA, ADDRESS_AREA, DMZ, CUSTOMERID, SOURCE, AREA, INSTALATUR, DEVELOPER, DATA_RECEIVED, DOC_DATA, DATA_SENT, REMARK) VALUES *(?,?,?,?,?,?,?,?,?,?,?,?,?,?)

为此:

Dim sql As String = "INSERT INTO " & tableName & " (CHECKLIST_ID, TYPE, SHAPEDATA, ADDRESS_AREA, MZ, CUSTOMERID, SOURCE, AREA, INSTALATUR, DEVELOPER, DATA_RECEIVED, DOC_DATA, DATA_SENT, REMARK)(@checklist_id, @type, @shapedata, @address_area, @dmz, @conection, @source, @area, @instalatur, @developer, @data_received, @doc_data, data_sent, @remark)"

然后我遇到了警告:必须声明标量变量“@conection”。有人能帮我吗?

最佳答案

您混淆了 SQL 查询中两种不同样式的参数。 ODBC 语法使用简单的占位符“?”每个参数的字符,并按照您将它们添加到参数集合的相同顺序替换参数。对于 OdbcCommand,您为参数指定的名称将被忽略,只有它们的顺序很重要。

对于SqlCommand,参数名是有意义的;当命令执行时,它将通过一个 SQL 存储过程运行,该过程获取参数名称和值的列表并将它们替换到 T-SQL 查询中。在这种情况下,您向查询添加参数的顺序并不重要,但您需要确保名称正确(包括“@”前缀。)

使用带参数的 SqlCommand 的正确方法如下:

// The SQL Query: Note the use of named parameters of the form
// @ParameterName1, @ParameterName2, etc.
Dim sql As String = "INSERT INTO " & tableName & _
" (CHECKLIST_ID, TYPE, SHAPEDATA ) " & _
"VALUES " & _
" (@ChecklistId, @Type, @ShapeData )"

// The Parameter List. Note that the Parameter name must exactly match
// what you use in the query:
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@CheckListId", checklist_id)
cmd.Parameters.AddWithValue("@Type", type)
cmd.Parameters.AddWithValue("@ShapeData", shape.ShapeData)

cmd.ExecuteNonQuery()

关于asp.net - SqlCommand 占位符参数 : "Incorrect syntax near ' ?'" and "Must declare the scalar variable @param"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11007505/

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