OleDb错误:“ExecuteNonQuery()需要一个开放且可用的连接”

时间:2015-03-06 14:54:21

标签: vb.net ms-access-2007 oledb

我一直在尝试连接Access和VB 2013,但它一直给我这个错误

  

ExecuteNonQuery()需要一个open和Avaliable连接

这是我的代码:

Imports System.Data.OleDb
Imports System.IO

Public Class Form3
Public sEditType As String = String.Empty
Dim cn As New OleDbConnection
Dim constring As String = "Provider=Microsoft.Jet.Oledb.12.0; Data Source=C:\Users\MarkTT\Documents\major.mdb; persist security info = false"


    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.KeyPress
End Sub

Private Sub Label4_Click(sender As Object, e As EventArgs)

End Sub

Private Sub TextBox2_TextChanged_1(sender As Object, e As EventArgs)

End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)

End Sub

Private Sub Label5_Click(sender As Object, e As EventArgs)

End Sub

Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click

End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
    Me.WindowState = FormWindowState.Minimized
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    Me.Close()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cm As New OleDbCommand
    Try
        Dim sqlquery As String = "INSERT INTO tablea (Name,Cost,Supplier,Quantity) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
        With cm
            .CommandText = sqlquery
            .Connection = cn
            .ExecuteNonQuery()
        End With
        MsgBox("Added")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try


End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs)

End Sub

Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click

    TextBox1.Enabled = True
    TextBox2.Enabled = True
    TextBox3.Enabled = True
    TextBox4.Enabled = True

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    TextBox1.Enabled = False
    TextBox2.Enabled = False
    TextBox3.Enabled = False
    TextBox4.Enabled = False

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub TextBox2_TextChanged_2(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

End Sub

Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    cn.ConnectionString = constring
    cn.Open()
End Sub

End Class

1 个答案:

答案 0 :(得分:2)

Provider=Microsoft.Jet.Oledb.12.0

不是有效的OLEDB提供程序名称。您需要使用

Provider=Microsoft.Jet.OLEDB.4.0

Provider=Microsoft.ACE.OLEDB.12.0