Update语句的语法错误 [英] Syntax Error with Update Statement
本文介绍了Update语句的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以基本上我正在尝试创建一个到Microsoft Access的VB.net连接。
我的问题是每当我尝试通过我的VB.net更新我的Access中的行时它不会工作。它不断弹出上述同样的错误。我总是出现错误总是出现的区域。如果有人可以花时间阅读我的代码并找出我的错误,我会很高兴。
公众 类客户
Dim con 作为 新 OleDb.OleDbConnection
Dim dbProdivder As String
Dim dbSource As 字符串
Dim ds As 新 DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As < span class =code-keyword> String
Dim inc As Integer
Dim MaxRows 作为 整数
私有 Sub Customer_Load(发件人作为系统。对象,e As System.EventArgs)句柄 MyBase .Load
dbProdivder = PROVIDER = Microsoft.Jet.OLEDB.4.0;
dbSource = < span class =code-string> Data Source = C:\ Users \Owner\DVD System Rental.mdb
con.ConnectionString = dbProdivder& dbSource
con.Open()
sql = SELECT * FROM Customer
da = 新 OleDb.OleDbDataAdapter(sql,con)
da.Fill(ds, DVD系统租赁)
con.Close()
MaxRows = ds.Tables( DVD System Rental)。Rows.Count
inc = -1
结束 Sub
私有 Sub NavigateRecords()
TxtCustomerID.Text = ds.Tables(< span class =code-string> DVD System Rental)。Rows(inc).Item( 0 )
TxtCustomerName.Text = ds.Tables( DVD系统租用)。行(inc).Item( 1 )
TxtDOB.Text = ds.Tables( DVD系统租赁)。行(inc).Item( 2 )
TxtAddress.Text = ds.Tables( < span class =code-string> DVD System Rental)。Rows(inc).Item( 3 )
TxtContact.Text = ds.Tables( DVD System Rental)。Rows(inc).Item( 4 )
结束 Sub
私有 Sub BtnNext_Click(发件人作为系统。对象,e 作为 System.EventArgs) 句柄 BtnNext.Click
如果 inc<> MaxRows - 1 然后
inc = inc + 1
NavigateRecords()
其他
MsgBox( 没有更多行)
结束 如果
结束 Sub
私有 Sub BtnPrevious_Click(发件人作为系统。对象,e As System.EventArgs)句柄 BtnPrevious。点击
如果 inc> 0 然后
inc = inc - 1
NavigateRecords()
ElseIf inc = -1 然后
MsgBox( 还没有记录)
ElseIf inc = 0 然后
MsgBox( 第一条记录)
结束 如果
结束 Sub
私有 Sub BtnLast_Click(发件人 As System。 Object ,e As System.EventArgs)句柄 BtnLast.Click
如果 inc<> MaxRows - 1 然后
inc = MaxRows - 1
NavigateRecords()
结束 如果
结束 Sub
私有 Sub BtnFirst_Click(sender As System。 Object ,e As System.EventArgs)句柄 BtnFirst.Click
如果 inc<> 0 然后
inc = 0
NavigateRecords()
结束 如果
结束 Sub
私有 < span class =code-keyword> Sub BtnUpdate_Click(sender As System。 Object , e As System.EventArgs)句柄 BtnUpdate.Click
Dim cb 作为 新 OleDb.OleDbCommandBuilder(da)
ds.Tables( DVD系统租赁)。行(包含)。项目( 0 )= TxtCustome rID.Text
ds.Tables( DVD系统租赁)。行(包) .Item( 1 )= TxtCustomerName.Text
ds.Tables( DVD System Rental)。Rows(inc).Item( 2 )= TxtDOB.Text
ds.Tables ( DVD系统租用)。行(inc).Item( 3 )= TxtAddress.Text
ds.Tables( DVD System Rental )。Rows(inc).Item( 4 )= TxtContact.Text
da.Update(ds, DVD系统租赁)< -------问题在这里
MsgBox( 数据更新)
结束 Sub
私有 Sub BtnAdd_Click(发件人作为系统。对象,e As System.EventArgs)句柄 BtnAdd.Click
BtnCommit.Enabled = True
BtnAdd.Enabled = False
BtnUpdate.Enabled = False
BtnDelete.Enabled = False
TxtCustomerID.Clear()
TxtCustomerName .Clear()
TxtDOB.Clear()
TxtAddress.Clear()
TxtContact.Clear()
End Sub
私有 Sub BtnClear_Click(sender As System。 Object ,e As System.EventArgs)句柄 BtnClear.Click
BtnCommit.Enabled = 错误
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDelete.Enabled = True
inc = 0
NavigateRecords()
结束 Sub
私有 Sub BtnCommit_Click(发件人作为系统。 对象,e As System.EventArgs) Handl es BtnCommit.Click
如果 inc<> -1 然后
Dim cb 作为 新 OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables( DVD系统租赁)。NewRow()
dsNewRow.Item( 0 )= TxtCustomerID.Text
dsNewRow.Item( 1 )= TxtCustomerName.Text
dsNewRow.Item( 2 )= TxtDOB.Text
dsNewRow.Item( 3 )= TxtAddress.Text
dsNewRow.Item( 4 )= TxtContact.Text
ds.Tables( DVD系统租赁)。Rows.Add(dsNewRow)
da.Update(ds, DVD系统租用)< -------问题在这里
MsgBox( 添加到数据库的新记录)
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDelete.Enabled = True
结束 如果
结束 Sub
私有 Sub BtnDelete_Click(发件人作为系统。 Object ,e As System.EventArgs) Handles BtnDelete.Click
如果 MessageBox.Show( 你真的想要吗?删除此记录?, 删除,MessageBoxButtons.YesNo,MessageBoxIcon.Warning) = DialogResult.No 然后
MsgBox( 操作已取消)
退出 Sub
结束 如果
Dim cb As 新 OleDb.OleDbCommandBuilder(da)
ds.Tables( DVD System Rental)。Rows(inc).Delete()
MaxRows = MaxRows - < span class =code-digit> 1
inc = 0
da.Update( ds, DVD系统租用)< -------问题这里
NavigateRecords()
结束 Sub
解决方案
尝试在更新按钮中使用它:
尝试
da = 新 OleDbDataAdapter( SELECT * FROM NamesList WHERE ID,conn)
Dim cmd 作为 新 OleDb.OleDbComm和
Dim Str As String
Str = update tablename set columnName =
Str + = & textbox1.text&
Str + = 其中ID =
Str + = Label6.Text.Trim()
cmd = 新 OleDbCommand(Str,conn)
cmd.ExecuteNonQuery()
ds.Clear()
da.Fill(ds, tablename)
MsgBox( 已成功更新...... )
Catch ex As 异常
MsgBox(ex.Message& ,& ex.Source)
结束 尝试
删除按钮
如果 MsgBox( 您确定要删除此记录吗?,MsgBoxStyle.YesNo + MsgBoxStyle.Question, 删除)= vbYes 然后
尝试
Dim sql 作为 字符串 = 从TBLName中删除ID =& DataGridView1.SelectedRows( 0 )。单元格( 0 )。值& ;
Dim cmd 作为 新 OleDbCommand
cmd.Connection = conn
cmd.CommandText = sql
cmd.ExecuteNonQuery()
MsgBox( 已成功删除!,MsgBoxStyle .Question 或 MsgBoxStyle.Information, 信息)
Catch ex As Exception
MessageBox.Show( 请先选择!)
结束 尝试
Else
MsgBox( 删除文件已被取消!, MsgBoxStyle.Question 或 MsgBoxStyle.Information, 信息)
结束 如果
SO basically i am trying to create a VB.net connection to Microsoft Access.
My problem is that whenever i try to update the rows in my Access through my VB.net it doesn't work at all. it keep popping out the same error stated above. I have bolded the Area where the error is always appearing. I would be nice if someone can take the time to read my code and find what my mistake is.
Public Class Customer
Dim con As New OleDb.OleDbConnection
Dim dbProdivder As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim inc As Integer
Dim MaxRows As Integer
Private Sub Customer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dbProdivder = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\Users\Owner\DVD System Rental.mdb"
con.ConnectionString = dbProdivder & dbSource
con.Open()
sql = "SELECT * FROM Customer"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "DVD System Rental")
con.Close()
MaxRows = ds.Tables("DVD System Rental").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
TxtCustomerID.Text = ds.Tables("DVD System Rental").Rows(inc).Item(0)
TxtCustomerName.Text = ds.Tables("DVD System Rental").Rows(inc).Item(1)
TxtDOB.Text = ds.Tables("DVD System Rental").Rows(inc).Item(2)
TxtAddress.Text = ds.Tables("DVD System Rental").Rows(inc).Item(3)
TxtContact.Text = ds.Tables("DVD System Rental").Rows(inc).Item(4)
End Sub
Private Sub BtnNext_Click(sender As System.Object, e As System.EventArgs) Handles BtnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Rows")
End If
End Sub
Private Sub BtnPrevious_Click(sender As System.Object, e As System.EventArgs) Handles BtnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
ElseIf inc = -1 Then
MsgBox("No Records Yet")
ElseIf inc = 0 Then
MsgBox("First Record")
End If
End Sub
Private Sub BtnLast_Click(sender As System.Object, e As System.EventArgs) Handles BtnLast.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub BtnFirst_Click(sender As System.Object, e As System.EventArgs) Handles BtnFirst.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub BtnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles BtnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("DVD System Rental").Rows(inc).Item(0) = TxtCustomerID.Text
ds.Tables("DVD System Rental").Rows(inc).Item(1) = TxtCustomerName.Text
ds.Tables("DVD System Rental").Rows(inc).Item(2) = TxtDOB.Text
ds.Tables("DVD System Rental").Rows(inc).Item(3) = TxtAddress.Text
ds.Tables("DVD System Rental").Rows(inc).Item(4) = TxtContact.Text
da.Update(ds, "DVD System Rental") <------- Problem here
MsgBox("Data updated")
End Sub
Private Sub BtnAdd_Click(sender As System.Object, e As System.EventArgs) Handles BtnAdd.Click
BtnCommit.Enabled = True
BtnAdd.Enabled = False
BtnUpdate.Enabled = False
BtnDelete.Enabled = False
TxtCustomerID.Clear()
TxtCustomerName.Clear()
TxtDOB.Clear()
TxtAddress.Clear()
TxtContact.Clear()
End Sub
Private Sub BtnClear_Click(sender As System.Object, e As System.EventArgs) Handles BtnClear.Click
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDelete.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub BtnCommit_Click(sender As System.Object, e As System.EventArgs) Handles BtnCommit.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("DVD System Rental").NewRow()
dsNewRow.Item(0) = TxtCustomerID.Text
dsNewRow.Item(1) = TxtCustomerName.Text
dsNewRow.Item(2) = TxtDOB.Text
dsNewRow.Item(3) = TxtAddress.Text
dsNewRow.Item(4) = TxtContact.Text
ds.Tables("DVD System Rental").Rows.Add(dsNewRow)
da.Update(ds, "DVD System Rental") <------- Problem here
MsgBox("New Record added to the Database")
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDelete.Enabled = True
End If
End Sub
Private Sub BtnDelete_Click(sender As System.Object, e As System.EventArgs) Handles BtnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("DVD System Rental").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "DVD System Rental") <------- Problem here
NavigateRecords()
End Sub
解决方案
Try Using It in your update button:
Try da = New OleDbDataAdapter("SELECT * FROM NamesList WHERE ID", conn) Dim cmd As New OleDb.OleDbCommand Dim Str As String Str = "update tablename set columnName=" Str += """" & textbox1.text & """" Str += " where ID=" Str += Label6.Text.Trim() cmd = New OleDbCommand(Str, conn) cmd.ExecuteNonQuery() ds.Clear() da.Fill(ds, "tablename") MsgBox("Updated Successfully...") Catch ex As Exception MsgBox(ex.Message & "," & ex.Source) End Try
Delete Button
If MsgBox("Are you Sure you want to Delete this record?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Delete") = vbYes Then Try Dim sql As String = "delete from TBLName where ID =" & DataGridView1.SelectedRows(0).Cells(0).Value & ";" Dim cmd As New OleDbCommand cmd.Connection = conn cmd.CommandText = sql cmd.ExecuteNonQuery() MsgBox("Successfully Deleted!", MsgBoxStyle.Question Or MsgBoxStyle.Information, "Information") Catch ex As Exception MessageBox.Show("Please Select First!") End Try Else MsgBox("Deleting Files is Cancelled!", MsgBoxStyle.Question Or MsgBoxStyle.Information, "Information") End If
这篇关于Update语句的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文