OleDb 错误:“ExecuteNonQuery() 需要打开且可用的连接”[关闭]
Posted
技术标签:
【中文标题】OleDb 错误:“ExecuteNonQuery() 需要打开且可用的连接”[关闭]【英文标题】:OleDb error: "ExecuteNonQuery() requires an open and available connection" [closed] 【发布时间】:2015-03-06 14:54:20 【问题描述】:我一直在尝试连接 Access 和 VB 2013,但它一直给我这个错误
ExecuteNonQuery() 需要一个开放且可用的连接
这是我的代码:
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】:Provider=Microsoft.Jet.Oledb.12.0
不是有效的 OLEDB 提供程序名称。您要么需要使用
Provider=Microsoft.Jet.OLEDB.4.0
或
Provider=Microsoft.ACE.OLEDB.12.0
【讨论】:
以上是关于OleDb 错误:“ExecuteNonQuery() 需要打开且可用的连接”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章