0

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

插入语句中缺少什么?完整的代码现在在这里。我无法将新字段值添加到 .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)INSERT INTO 语句中的语法错误 - 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 & "' )"
4

2 回答 2

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
于 2013-06-05T15:02:31.130 回答
0

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

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

Me.cndt.Text & "'," & Me.cdue.Text & ",'" & Me.cuid.Text 
于 2013-06-05T14:44:31.623 回答