Connection属性尚未初始化 - VB.net ExecuteNonQuery Error

时间:2017-06-26 21:25:53

标签: vb.net oledbconnection

我有这个错误" ExecuteNonQuery:Connection属性尚未初始化"当试图连接并将文本框中的数据添加到" .accdb"数据库文件。请看下面的代码。这对我来说似乎都很好。 干杯, 斯坦

Imports System.Data.OleDb

Public Class Form1
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection





Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    dataFile = "C:\Database7.accdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString

    myConnection.Open()

    Dim str As String
    str = "Insert into MkX ([ID], [Project], [Rotor Number], [MLC thickness]) Values (?,?,?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("ID", CType(TextBox1.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Project", CType(TextBox2.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Rotor Number", CType(TextBox3.Text, String)))

    Try


        cmd.Dispose()
        cmd.ExecuteNonQuery()

        myConnection.Close()


        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
  End Class

0 个答案:

没有答案