连接必须有效并使用vb.net打开VB.net错误

时间:2016-04-20 05:57:09

标签: mysql database vb.net odbc

这是我使用ODBC driver从EXCEL文件读取数据并在MySql数据库中写入的代码。

Public Class WebForm3
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MySqlCmd = New SqlCommand()

        ' Dim dialog As New System.Windows.Forms.OpenFileDialog()
        'Dim dialog As New OpenFileDialog()
        'dialog.Filter = "Excel files |*.xls;*.xlsx"
        'dialog.InitialDirectory = "C:\"
        'dialog.Title = "Select file for import"
        'If dialog.ShowDialog() = DialogResult.OK Then
        Try
            Dim dt As DataTable
            Dim buff0 As String
            Dim buff1 As String
            Dim buff2 As String

            dt = ImportExceltoDatatable("C:\\Book1.xls")

            For i = 0 To dt.Rows.Count - 1
                buff0 = dt.Rows(i)(0)
                buff1 = dt.Rows(i)(1)
                buff2 = dt.Rows(i)(2)

                Dim connStr As String = "server=localhost;user=root;database=ajaxsamples;port=3306;password=innoera;"
                Dim connMysql As MySqlConnection = New MySqlConnection(connStr)
                Dim sql As String = "INSERT INTO ajaxsamples.customers  VALUES('" & buff0 & "','" & buff1 & "','" & buff2 & "')"
                Dim cmd As MySqlCommand = New MySqlCommand(sql, connMysql)
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                connMysql.Close()
            Next
        Catch ex As Exception
            MsgBox(Err.Description, MsgBoxStyle.Critical)
        End Try

        'End If
    End Sub


    Public Shared Function ImportExceltoDatatable(filepath As String) As DataTable
        ' string sqlquery= "Select * From [SheetName$] Where YourCondition";
        Dim dt As New DataTable
        Try
            Dim ds As New DataSet()
            Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filepath & ";Extended Properties=""Excel 12.0;HDR=YES;"""
            Dim con As New OleDbConnection(constring & "")

            con.Open()
            Dim myTableName = con.GetSchema("Tables").Rows(0)("TABLE_NAME")
            Dim sqlquery As String = String.Format("SELECT * FROM [{0}]", myTableName)
            'Dim myTableName = con.GetSchema("Tables").Rows(0)("TABLE_NAME")
            'Dim sqlquery As String = String.Format("SELECT * FROM Sheet1$") ' "Select * From " & myTableName

            Dim da As New OleDbDataAdapter(sqlquery, con)
            da.Fill(ds)

            dt = ds.Tables(0)
            Return dt
        Catch ex As Exception
            MsgBox(Err.Description, MsgBoxStyle.Critical)
            Return dt
        End Try
    End Function

End Class

我收到了这个错误,

  

“连接必须有效并且打开”

代码中有什么问题吗?我是VB的新手。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

您忘了打开连接。

Try
    connMysql = New MySqlConnection
    connMysql.ConnectionString = connStr
    connMysql.Open() 'You forgot to open your connection
    sql = "SELECT * FROM users"

    cmd = New MySqlCommand(sql, connMysql)
    cmd.ExecuteNonQuery()
    cmd.Dispose()
Catch ex As Exception
    'your error code here
Finally
    connMysql.Close() 'close your connection
End Try