在将值插入.mdb文件时,Visual Studio 2010会反复插入语法错误

时间:2013-06-05 12:32:16

标签: vb.net

insert语句中缺少什么?完整的代码现在在这里。我无法将新字段值添加到.mdb文件。开发此应用程序需要3天时间,现在它还没有运行。

insert语句中缺少什么?完整的代码现在在这里。我无法将新字段值添加到.mdb文件。开发此应用程序需要3天时间,现在它还没有运行。

Public Class client
Dim cnn As New OleDb.OleDbConnection
Private Sub reloaddata()
    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If
    Dim da As New OleDb.OleDbDataAdapter("select * from clientsData", cnn)
    Dim dt As New DataTable
    da.Fill(dt)
    Me.cview.DataSource = dt
    cnn.Close()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    display.Hide()
    cnn = New OleDb.OleDbConnection
    cnn.ConnectionString = "provider= microsoft.jet.oledb.4.0; data source=" & Application.StartupPath & "\clients.mdb"
    Me.reloaddata()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    display.Show()
End Sub
Private Sub cview_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles cview.CellClick

    If cnn.State = ConnectionState.Closed Then
        cnn.Open()

    End If
    Dim i As Integer
    i = cview.CurrentRow.Index
    If cview.CurrentCell.Value Is Nothing Then
        MsgBox("Empty Field")
    Else
        view.lbl1.Text = cview.Item(0, i).Value.ToString
        view.lbl2.Text = cview.Item(1, i).Value.ToString
        view.lbl3.Text = cview.Item(2, i).Value.ToString
        view.lbl4.Text = cview.Item(3, i).Value.ToString
        view.lbl5.Text = cview.Item(4, i).Value.ToString
        view.lbl6.Text = cview.Item(5, i).Value.ToString
        view.lbl7.Text = cview.Item(6, i).Value.ToString
        view.lbl8.Text = cview.Item(11, i).Value.ToString
        view.lbl9.Text = cview.Item(12, i).Value.ToString
        view.lbl10.Text = cview.Item(7, i).Value.ToString
        view.lbl11.Text = cview.Item(8, i).Value.ToString
        view.lbl12.Text = cview.Item(9, i).Value.ToString
        view.lbl13.Text = cview.Item(10, i).Value.ToString
        view.lbl14.Text = cview.Item(13, i).Value.ToString
        view.Show()

    End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim cmd As New OleDb.OleDbCommand
    If Not cnn.State = ConnectionState.Open Then
      cnn.Open()
    End If

    cmd.Connection = cnn       
    cmd.CommandText = "insert into clientsdata(ID,Client,Project,Domain,Hosting,bulk sms,maintenance,Order date,amount,last billing,next billing,username,password,due amount) VALUES ('" & Me.cid.Text & "','" & Me.cname.Text & "','" & Me.cproj.Text & "','" & Me.cdmn.Text & "','" & Me.chost.Text & "','" & Me.csms.Text & "','" & Me.cmain.Text & "','" & Me.codt.Text & "','" & Me.camnt.Text & "','" & Me.cldt.Text & "','" & Me.cndt.Text & "','" & Me.cuid.Text & "','" & Me.cpass.Text & "','" & Me.cdue.Text & "' )"
    cmd.ExecuteNonQuery()
    Me.reloaddata()
   cnn.Close()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    cid.Text = "cid"
    cname.Text = "cname"
    cproj.Text = "cpro"
    cdmn.Text = "domain"
    chost.Text = "chost"
    csms.Text = "sms"
    cmain.Text = "main"
    codt.Text = "codt"
    camnt.Text = "mount"
    cldt.Text = "last"
    cndt.Text = "next"
    cdue.Text = "due"
    cuid.Text = "uid"
    cpass.Text = "pass"
   End Sub

结束班

好的问题解决了!!我刚在字段中添加了[]。

来源:(VB.NET)Syntax Error in INSERT INTO statement - MICROSOFT JET DATABASE ENGINE

cmd.CommandText = "insert into clientsdata([ID],[Client],[Project],[Domain],[Hosting],[bulk sms],[maintenance],[Order date],[amount],[last billing],[next billing],[username],[password],[due amount]) VALUES (" & Me.cid.Text & ",'" & Me.cname.Text & "','" & Me.cproj.Text & "','" & Me.cdmn.Text & "','" & Me.chost.Text & "','" & Me.csms.Text & "','" & Me.cmain.Text & "','" & Me.codt.Text & "','" & Me.camnt.Text & "','" & Me.cldt.Text & "','" & Me.cndt.Text & "','" & Me.cuid.Text & "','" & Me.cpass.Text & "','" & Me.cdue.Text & "' )"

2 个答案:

答案 0 :(得分:1)

您的date字段是关键字。你必须用括号括起来:[date]。此外,任何包含空格的字段名称也需要括号:[Order date]

您确实应该使用参数来避免SQL注入,并解决更新数据库时的许多其他问题。

我也会避免尝试管理数据库的连接状态。只需使用Using语法,即连接始终关闭。

If Me.cid.Text = "" Then
  MessageBox.Show("Please input values")
Else
  Using con As New OleDb.OleDbConnection("...")
    con.Open()
    Using cmd As New OleDb.OleDbCommand()
      cmd.Connection = con
      cmd.CommandText = "..."
      cmd.Parameters.AddWithValue("@ID", Me.cid.Text)
      cmd.Parameters.AddWithValue(...more)
      cmd.ExecuteNonQuery()
    End Using
  End Using

  reloaddata()
End If

答案 1 :(得分:0)

如果不知道每个.Text值中包含的内容,并且没有看到您正在填写的表的定义,很难说出问题是什么。但我可能会猜测{{{ 1}}和Me.camnt.Text可能不希望被单引号包围;也就是说,你可能需要这样的东西......

Me.cdue.Text

Me.codt.Text & "'," & Me.camnt.Text & ",'" & Me.cldt.Text