2015-05-04 110 views
-4

我不知道爲什麼,當過我點擊更新按鈕,我在UPDATE語句得到一個錯誤 語法錯誤錯誤消息:在查詢表達式語法錯誤(缺少操作員)

我沒有知道什麼地方出了錯在我的代碼

這是我的代碼:

Public Class Form1 
Private Function vld(ByVal ParamArray ctl() As Object) As Boolean 
    For i As Integer = 0 To UBound(ctl) 
     If ctl(i).text = "" Then 
      ErrorProvider1.SetError(ctl(i), ctl(i).tag) 
      Return False 
      Exit For 
     End If 
    Next 
    Return True 
End Function 

Dim cn As New OleDbConnection 
Dim cm As New OleDbCommand 
Dim da As OleDbDataAdapter 
Dim dt As New DataTable 

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    cn.Close() 
End Sub 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    TxtExamtime.Format = DateTimePickerFormat.Custom 
    TxtExamtime.CustomFormat = "hh:MM tt" 
    cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;" 
    cn.Open() 
    FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]") 

End Sub 

Private Sub FillDataGridView(ByVal Query As String) 
    da = New OleDbDataAdapter(Query, cn) 
    dt.Clear() 
    da.Fill(dt) 
    With DataGridView1 
     .DataSource = dt 
     .Columns(0).HeaderText = "ID" 
     .Columns(1).HeaderText = "Family Name" 
     .Columns(2).HeaderText = "Given Name" 
     .Columns(3).HeaderText = "Gender" 
     .Columns(4).HeaderText = "DOB" 
     .Columns(5).HeaderText = "Exam Date" 
     .Columns(6).HeaderText = "Exam Time" 
     .Columns(7).HeaderText = "Street Name" 
     .Columns(8).HeaderText = "House Nr" 
     .Columns(9).HeaderText = "PLZ" 
     .Columns(10).HeaderText = "City" 
     .Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 
    End With 
End Sub 

Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click 
    Try 
     FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'") 
     TxtFamilyname.Text = dt.Rows(0).Item(1) 
     TxtGivenname.Text = dt.Rows(0).Item(2) 
     TxtGender.Text = dt.Rows(0).Item(3) 
     TxtDob.Text = dt.Rows(0).Item(4) 
     TxtExamdate.Text = dt.Rows(0).Item(5) 
     TxtExamtime.Text = dt.Rows(0).Item(6) 
     TxtStreet.Text = dt.Rows(0).Item(7) 
     TxtHouse.Text = dt.Rows(0).Item(8) 
     TxtPlz.Text = dt.Rows(0).Item(9) 
     TxtCity.Text = dt.Rows(0).Item(10) 
    Catch ex As Exception 
     MsgBox(ex.Message, MsgBoxStyle.Critical, Text) 
    End Try 
End Sub 

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click 
    If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then 
     Exit Sub 
    Else 

    End If 

    Try 
     With cm 
      .Connection = cn 
      .CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "')" 
      .ExecuteNonQuery() 
     End With 
     FillDataGridView("select * from [edit$]") 
    Catch ex As Exception 
     MsgBox(ex.Message, MsgBoxStyle.Critical, Text) 
     Return 
    End Try 
    MsgBox("succefully Saved!", MsgBoxStyle.Information, Text) 
End Sub 

Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click 
    Try 
     With cm 
      .Connection = cn 
      .CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where [ID] ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "' and [Gender] = '" & TxtGender.Text & "'and [DOB] = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and [PLZ] = '" & TxtPlz.Text & "'and [CITY] = '" & TxtCity.Text & "'" 
      .ExecuteNonQuery() 
     End With 
     FillDataGridView("select * from [edit$]") 
    Catch ex As Exception 
     MsgBox(ex.Message, MsgBoxStyle.Information, Text) 
     Return 
    End Try 
    MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text) 
End Sub 

Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click 
    Close() 
End Sub 

Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click 
    TxtId.Clear() 
    TxtFamilyname.Clear() 
    TxtGivenname.Clear() 
    TxtStreet.Clear() 
    TxtHouse.Clear() 
    TxtPlz.Clear() 
    TxtCity.Clear() 
    'To see all the data in DataGridView 
    FillDataGridView("select * from[edit$]") 

End Sub 

Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click 
    Try 
     With cm 
      .Connection = cn 
      .CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and [ID] ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and [Gender] = '" & TxtGender.Text & "'and [DOB] = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and [PLZ] = '" & TxtPlz.Text & "'and [CITY] = '" & TxtCity.Text & "'" 
      .ExecuteNonQuery() 
     End With 
     MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text) 
     FillDataGridView("select * from [edit$]") 
    Catch ex As Exception 
     MsgBox(ex.Message, MsgBoxStyle.Information, Text) 

    End Try 


End Sub 


End Class 
+0

語法錯誤很可能告訴你到底是什麼問題與你的語法。這就是錯誤所在。 – TZHX

+3

這不是重複的http://stackoverflow.com/questions/30025617/error-message-syntax-error-in-update-statement/30025723 –

+0

爲什麼你使用不同的用戶發佈相同的問題? Duplicate http://stackoverflow.com/questions/30025617/error-message-syntax-error-in-update-statement/30025723 – equisde

回答

0

看看這可以幫助你:

.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID = '" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "' and DOB = '" & TxtDob.Text & "' and [Exam Date] = '" & TxtExamdate.Text & "' and [Exam Time] = '" & TxtExamtime.Text & "' and [Street Name] = '" & TxtStreet.Text & "' and [House Nr] = '" & TxtHouse.Text & "' and PLZ = '" & TxtPlz.Text & "' and CITY = '" & TxtCity.Text & "'" 

改變我做:在

  • 增加空間值和與關鍵字
  • 添加缺少的字段名和值之間的等號(例如,TxtExamdate.Text & "'and [Exam Time]「和」前需要有一個空格)(例如之間, and [Exam Date]'" & TxtExamdate.Text應該看起來更像and [Exam Date] = '" & TxtExamdate.Text
  • 擺脫了多餘的括號

,如果你仍然有問題,考慮到要插入的字符串(這可能potentiall y包含文本框中的任何可打印字符(以及不可打印的字符!),直接放入您的語句中。如果這些字符串包含非法字符(例如,單引號或雙引號),它們將更改UPDATE語句的含義,並可能使語法無效。之前已經建議(在other question)您應該使用parametrized queries。最後,如果任何表列不是字符串(如「ID」),則可能會得到數據類型不匹配錯誤,因爲您將它們全部作爲字符串發送(用雙引號括起來)。

而且,請僅從一個帳戶發佈一次問題。

+0

他的擴展屬性也是錯誤的。 –

相關問題