在 asp.net 应用程序中更新查询语法错误
Posted
技术标签:
【中文标题】在 asp.net 应用程序中更新查询语法错误【英文标题】:update query syntax error in asp.net application 【发布时间】:2011-07-14 03:24:38 【问题描述】:编辑:
这个问题已经改变。 HansUp 在更新语句中解决了这个语法问题。现在发生的事情完全不同。过程是
用户选择了一个网格视图项 它将它们重定向到更新页面并使用数据读取器,根据 url 中传递的 id 填充文本框和复选框 然后用户可以对文本框/复选框进行更改,然后按下运行更新查询的更新按钮。我发现正在发生的是,尽管用户可能会更改文本,但当他们提交更改时,更新查询仍在使用页面加载时数据阅读器加载到该文本框中的任何内容。下面是代码:
Protected Sub SubmitBTN_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateBTN.Click
Dim tiresdim As Integer = 0
If TiresCHK.Checked = True Then
tiresdim = -1
ElseIf TiresCHK.Checked = False Then
tiresdim = 0
End If
Dim repairs As Integer = 0
If RepairsCheckBX.Checked = True Then
repairs = -1
ElseIf RepairsCheckBX.Checked = False Then
repairs = 0
End If
Dim onlotdim As Integer = 0
If OnLotCheckBX.Checked = True Then
onlotdim = -1
ElseIf OnLotCheckBX.Checked = False Then
onlotdim = 0
End If
Dim offpropdim As Integer = 0
If OffPropertyCheckBX.Checked = True Then
offpropdim = -1
ElseIf OffPropertyCheckBX.Checked = False Then
offpropdim = 0
End If
Dim soldim As Integer = 0
If SoldCheckBX.Checked = True Then
soldim = -1
ElseIf SoldCheckBX.Checked = False Then
soldim = 0
End If
Dim id = CType(Request.QueryString("param1"), Integer)
Dim connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jason\Desktop\UsedCarProductionSched\app_data\UsedCars.accdb;Persist Security Info=False;")
Dim sql As String = "update Master set stocknum='" & StockNumTxt.Text & "',[year]='" & YearTxt.Text & "',make='" & MakeTxt.Text & "', model='" & ModelTxt.Text & "', color='" & ColorTxt.Text & "',location='" & LocationDropDownList.SelectedValue & "',tiresneeded=" & tiresdim & ",stockin=#" & StockInDateTxt.Text & "#,SvcRONum='" & SrvcROnumTxt.Text & "',ucistartdate=#" & UCIStartDateTxt.Text & "#,UCIEstCompleteDate=#" & UCIEstComDateTXT.Text & "#,repairs=" & repairs & ",CollisionRONum='" & CollisionRONumTXT.Text & "',[detail]=#" & DetailTXTbox.Text & "#, other='this has to work',onlot=" & onlotdim & ",offproperty=" & offpropdim & ",sold=" & soldim & " WHERE recnum=" & id
connection.Open()
Dim cmd As New OleDb.OleDbCommand(sql, connection)
cmd.ExecuteNonQuery()
connection.Close()
'Dim updateta As New DataSet1TableAdapters.Master1TableAdapter
'updateta.UpdateQuery(StockNumTxt.Text, YearTxt.Text, MakeTxt.Text, ModelTxt.Text, ColorTxt.Text, LocationDropDownList.SelectedValue, TiresCHK.Checked, StockInDateTxt.Text, SrvcROnumTxt.Text, UCIStartDateTxt.Text, UCIEstComDateTXT.Text, RepairsCheckBX.Checked, CollisionRONumTXT.Text, DetailTXTbox.Text, OtherTxt.Text, OnLotCheckBX.Checked, OffPropertyCheckBX.Checked, SoldCheckBX.Checked, Request.QueryString("param1"))
Response.Redirect("success.aspx")
End Sub
Function myCStr(ByVal test As Object) As String
If isdbnull(test) Then
Return ("")
Else
Return CStr(test)
End If
End Function
Public Shared Function IsDBNull( _
ByVal value As Object _
) As Boolean
Return DBNull.Value.Equals(value)
End Function
Private Sub getData(ByVal user As String)
'declare variables to fill
Dim stock As String, make As String, color As String, stockin As Date, ucistart As Date, repairs As Boolean, _
tires As Boolean, onlot As Boolean, sold As Boolean, year As Boolean, model As String, location As String, srvcRO As String, ucicompldate As Date, _
collRO As String, other As String, offprop As Boolean, detail As Date
Dim dt As New DataTable()
Dim connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jason\Desktop\UsedCarProductionSched\app_data\UsedCars.accdb;Persist Security Info=False;")
connection.Open()
Dim sqlcmd As String = "SELECT * from Master WHERE RecNum = @recnum"
Dim FileCommand3 As New OleDb.OleDbCommand(sqlcmd, connection)
FileCommand3.Parameters.AddWithValue("@recnum", user)
Dim Reader3 As OleDb.OleDbDataReader = FileCommand3.ExecuteReader()
If Reader3.Read Then
stock = myCStr(Reader3("StockNum"))
make = myCStr(Reader3("Make"))
color = myCStr(Reader3("Color"))
stockin = IIf(Reader3("stockin") Is DBNull.Value, Nothing, Reader3("stockin"))
ucistart = IIf(Reader3("ucistartdate") Is DBNull.Value, Nothing, Reader3("ucistartdate"))
repairs = Reader3("Repairs")
tires = Reader3("tiresneeded")
onlot = Reader3("onlot")
sold = Reader3("sold")
year = myCStr(Reader3("year"))
model = myCStr(Reader3("model"))
location = myCStr(Reader3("location"))
srvcRO = myCStr(Reader3("svcROnum"))
ucicompldate = IIf(Reader3("uciestcompletedate") Is DBNull.Value, Nothing, Reader3("uciestcompletedate"))
collRO = myCStr(Reader3("collisionROnum"))
other = myCStr(Reader3("other"))
offprop = Reader3("offProperty")
detail = IIf(Reader3("detail") Is DBNull.Value, Nothing, Reader3("detail"))
End If
connection.Close()
If detail <> Nothing Then
DetailTXTbox.Text = detail.ToString("M/dd/yyyy")
Else : DetailTXTbox.Text = ""
End If
If ucicompldate <> Nothing Then
UCIEstComDateTXT.Text = ucicompldate.ToString("MM/dd/yyyy")
Else : UCIEstComDateTXT.Text = ""
End If
If stockin <> Nothing Then
StockInDateTxt.Text = stockin.ToString("MM/dd/yyyy")
Else : StockInDateTxt.Text = ""
End If
If ucistart <> Nothing Then
UCIStartDateTxt.Text = ucistart.ToString("M/dd/yyyy")
Else : UCIStartDateTxt.Text = ""
End If
StockNumTxt.Text = stock
MakeTxt.Text = make
ColorTxt.Text = color
RepairsCheckBX.Checked = repairs
TiresCHK.Checked = tires
OnLotCheckBX.Checked = onlot
SoldCheckBX.Checked = sold
YearTxt.Text = year
ModelTxt.Text = model
If location <> Nothing Then
LocationDropDownList.SelectedValue = location
End If
SrvcROnumTxt.Text = srvcRO
CollisionRONumTXT.Text = collRO
OtherTxt.Text = other
OffPropertyCheckBX.Checked = offprop
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
getData(Request.QueryString("param1"))
End Sub
我的 asp.net 应用程序应该对访问数据库执行简单的更新查询,但它却引发了语法错误。我已将确切的查询直接复制并粘贴到我的访问数据库中,并且它可以正确执行。代码如下:
Dim connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jason\Desktop\UsedCarProductionSched\app_data\UsedCars.accdb;Persist Security Info=False;")
Dim sql As String = "update Master " _
+ "set stocknum='" & StockNumTxt.Text & "',year='" & YearTxt.Text & "',make='" & MakeTxt.Text & "', model='" & ModelTxt.Text & "', color='" & ColorTxt.Text & "',location='" & LocationDropDownList.SelectedValue & "',tiresneeded=" & tiresdim & ",stockin=#" & StockInDateTxt.Text & "#,SvcRONum='" & SrvcROnumTxt.Text & "',ucistartdate=#" & UCIStartDateTxt.Text & "#,UCIEstCompleteDate=#" & UCIEstComDateTXT.Text & "#,repairs=" & repairs & ",CollisionRONum='" & CollisionRONumTXT.Text & "',detail=#" & DetailTXTbox.Text & "#, other='" & OtherTxt.Text & "',onlot=" & onlotdim & ",offproperty=" & offpropdim & ",sold=" & soldim & " " _
+ "WHERE recnum=" & Request.QueryString("param1")
connection.Open()
Dim cmd As New OleDb.OleDbCommand(sql, connection)
cmd.ExecuteNonQuery()
connection.Close()
【问题讨论】:
能否请您发布您打算运行的确切 SQL 查询,以便我进行比较 从调试中的即时窗口复制这是它试图运行的 SQL 查询:update Master set stocknum='1',year='True',make='1', model='1', color='1',location='Select A Location...',tiresneeded=-1,stockin=#06/01/2011#,SvcRONum='1',ucistartdate=#6/10/2011#,UCIEstCompleteDate=#07/09/2011#,repairs=-1,CollisionRONum='1',detail=#7/09/2011#, other='THIS WORKSSSSSSSSS',onlot=-1,offproperty=-1,sold=-1 WHERE recnum=15"
您评论中的 UPDATE 语句末尾包含一个杂散的双引号。这实际上是声明的一部分吗?
是的。我添加了它,因此在 where 子句开始之前会有一个空格。我想我可以把那个空格放在“where”之前,而不是有 2 个杂散引号。
【参考方案1】:
您的两列使用保留字作为名称:year;细节。如果您必须保留这些名称,请将它们括在 UPDATE 语句中的方括号中,以避免混淆 Access 的数据库引擎。
年份字段是什么数据类型?您评论中的完成语句包括 year='True' 如果“year”是文本数据类型,那很好。但如果是“是/否”字段,请去掉 True 周围的引号。
【讨论】:
好的...年份是字符串数据类型。这是我的输入错误,它说的是真的,但它没有任何区别。放括号解决了一个问题,但现在我有另一个问题。我现在收到此错误:InvalidCastException was unhandled by user code Conversion from string " WHERE recnum=" to type 'Double' is not valid.
我假设它与我从查询字符串请求的字符串值与访问数据库的标识值不匹配
请向我们展示产生该错误的当前 UPDATE 语句的确切文本。将其复制并粘贴为您的问题的编辑。还请告诉我们,在打开 UsedCars.accdb 的 Access 会话中将相同文本粘贴到新查询的 SQL 视图中时会发生什么。
现在对我来说似乎是一个不同的问题。之前是 Access 数据库引擎抱怨 UPDATE 语法。现在它是关于别的事情......显然是一些对我来说是希腊语的 Dot.Net 问题。【参考方案2】:
什么是语法错误?此外,您应该先验证 SQL 的输入,然后再将其投入数据库更新。如果文本中有撇号会导致问题。参数化查询也会更理想。
【讨论】:
我已经包含了整个错误详细信息的一部分:System.Data.OleDb.OleDbException 未被用户代码处理 ErrorCode=-2147217900 Message=UPDATE 语句中的语法错误。 Source=Microsoft Office Access 数据库引擎 您确定您提供的语句在 Access 中执行时运行良好? 您应该感谢 HansUp 的回答并将您的新问题作为单独的问题发布。【参考方案3】:SkinnyWhiteNinja 建议的参数化 SQL 操作示例
我有一个有 4 个列的表,ColCode 和 CollSeq 是键,TermType 和 TermText 是可修改的数据 该代码解释了如何在 SQL 中插入、更新或删除带有 instaed 参数的行。 该代码仅适用于 ACCESS、SQL SERVER 或 mysql 需要不同的模板代码并具有不同的 DbTypes
在程序的第一部分:
' Insert
Dim DbConn As New OleDbConnection(SqlProv)
Dim SQLTwInsert As String = "INSERT INTO SearchTerms (CollCode, CollSeq, TermType, TermText) VALUES (?, ?, ?, ?)"
Dim DRTwInsert As OleDbDataReader = Nothing
Dim DCCTwInsert As OleDbCommand
Dim TwInsP1 As New OleDbParameter("@CollCode", OleDbType.VarChar, 4)
Dim TwInsP2 As New OleDbParameter("@CollSeq", OleDbType.Integer, 4)
Dim TwInsP3 As New OleDbParameter("@TermType", OleDbType.VarChar, 4)
Dim TwInsP4 As New OleDbParameter("@TermText", OleDbType.VarChar, 255)
DCCTwInsert = New OleDbCommand(SQLTwInsert, DbConn)
DCCTwInsert.Parameters.Add(TwInsP1)
DCCTwInsert.Parameters.Add(TwInsP2)
DCCTwInsert.Parameters.Add(TwInsP3)
DCCTwInsert.Parameters.Add(TwInsP4)
' Delete
Dim SQLTwDelete As String = "DELETE FROM SearchTerms WHERE CollCode = ? AND CollSeq = ? AND TermType = ? AND TermText = ?"
Dim DRTwDelete As OleDbDataReader = Nothing
Dim DCCTwDelete As OleDbCommand
Dim TwDelP1 As New OleDbParameter("@CollCode", OleDbType.VarChar, 4)
Dim TwDelP2 As New OleDbParameter("@CollSeq", OleDbType.Integer, 4)
Dim TwDelP3 As New OleDbParameter("@TermType", OleDbType.VarChar, 4)
Dim TwDelP4 As New OleDbParameter("@TermText", OleDbType.VarChar, 255)
DCCTwDelete = New OleDbCommand(SQLTwDelete, DbConn)
DCCTwDelete.Parameters.Add(TwDelP1)
DCCTwDelete.Parameters.Add(TwDelP2)
DCCTwDelete.Parameters.Add(TwDelP3)
DCCTwDelete.Parameters.Add(TwDelP4)
' Update
Dim SQLTwUpdate As String = "UPDATE SearchTerms SET TermType = ?, TermText = ? WHERE CollCode = ? AND CollSeq = ? AND TermType = ? AND TermText = ?"
Dim DRTwUpdate As OleDbDataReader = Nothing
Dim DCCTwUpdate As OleDbCommand
Dim TwUpdP1 As New OleDbParameter("@TermType", OleDbType.VarChar, 4)
Dim TwUpdP2 As New OleDbParameter("@TermText", OleDbType.VarChar, 255)
Dim TwUpdP3 As New OleDbParameter("@CollCode", OleDbType.VarChar, 4)
Dim TwUpdP4 As New OleDbParameter("@CollSeq", OleDbType.Integer, 4)
Dim TwUpdP5 As New OleDbParameter("@oldTermType", OleDbType.VarChar, 4)
Dim TwUpdP6 As New OleDbParameter("@oldTermText", OleDbType.VarChar, 255)
DCCTwUpdate = New OleDbCommand(SQLTwUpdate, DbConn)
DCCTwUpdate.Parameters.Add(TwUpdP1)
DCCTwUpdate.Parameters.Add(TwUpdP2)
DCCTwUpdate.Parameters.Add(TwUpdP3)
DCCTwUpdate.Parameters.Add(TwUpdP4)
DCCTwUpdate.Parameters.Add(TwUpdP5)
DCCTwUpdate.Parameters.Add(TwUpdP6)
在程序的处理部分:
' Update
TwUpdP1.Value = new value TermType
TwUpdP2.Value = new value TermText
TwUpdP3.Value = key value CollCode
TwUpdP4.Value = key value CollSeq
TwUpdP5.Value = old value TermType to avoid updating a row that 1 millisecond earlier was modified by someone else
TwUpdP6.Value = old value TermText
Try
DRTwUpdate = DCCTwUpdate.ExecuteReader()
Catch ex As Exception
your type of report exception
Finally
If Not (DRTwUpdate Is Nothing) Then
DRTwUpdate.Dispose()
DRTwUpdate.Close()
End If
End Try
' Insert
TwInsP1.Value = new key value CollCode
TwInsP2.Value = new key value CollSeq
TwInsP3.Value = value TermType
TwInsP4.Value = value TermText
Try
DRTwInsert = DCCTwInsert.ExecuteReader()
Catch ex As Exception
your type of report exception
Finally
If Not (DRTwInsert Is Nothing) Then
DRTwInsert.Dispose()
DRTwInsert.Close()
End If
End Try
' Delete
TwDelP1.Value = key value CollCode
TwDelP2.Value = key value CollSeq
TwDelP3.Value = old value TermType to avoid deleting a row that 1 millisecond earlier was modified by someone else
TwDelP4.Value = old value TermText
Try
DRTwDelete = DCCTwDelete.ExecuteReader()
Catch ex As Exception
your type of report exception
Finally
If Not (DRTwDelete Is Nothing) Then
DRTwDelete.Dispose()
DRTwDelete.Close()
End If
End Try
试一试,确实避免了很多问题,虽然写起来有点笨。
【讨论】:
以上是关于在 asp.net 应用程序中更新查询语法错误的主要内容,如果未能解决你的问题,请参考以下文章
在 ASP.net 应用程序的 Informix 查询中使用命名参数
帮忙挑毛病 C# asp.net “语法错误 (操作符丢失) 在查询表达式''中”错误
Asp.net mvc 应用程序在用户登录前抛出 js 语法错误