2016-04-20 17 views
0
öffnen

Dies ist mein Code zum Lesen von Daten aus EXCEL-Datei mit ODBC driver und Schreiben in MySql Datenbank.Verbindung muss gültig sein und VB.net Fehler mit vb.net

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 

ich diesen Fehler bekam,

"Verbindung muss gültig und offen sein"

was falsch im Code? Ich bin ein Neuling für VB. Jede Hilfe wäre willkommen.

+0

SQL Injection Sicherheitslücken: Fügen Sie niemals externe Werte zu einem SQL-Befehl mit String-Verkettung hinzu. * Verwenden Sie immer Parameter *. – Richard

+1

Sie haben eine 'connMysql.Open()' in Button1_Click mnissed –

Antwort

1

Sie haben vergessen, Ihre Verbindung zu öffnen.

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