2015-02-11 86 views
0

在VB2010 &我在MYSQL數據庫中的新手。 我有2個數據庫之一在MS SQL 2008(BigData)和另一個在Mysql上。我在VB2010中編寫了一些代碼來從SQL2008中獲取數據並插入到MySQL中。我的目標是儘可能快地將所有數據從MS SQL傳輸到MySQL,這就是爲什麼我創建了一個簡單的VB腳本,它將作爲中間件將數據從MS SQL傳輸到MySQL。從MS SQL 2008插入查詢結果到MYSQL通過VB2010

我的頭痛是,將近28000條記錄從MS SQL傳輸到MySQL數據庫將近1小時。有沒有最簡單的方式來傳輸數據或我需要增強我的VBScript程序。請幫助改進我的VBScript下面。

預先感謝您。

進口MySql.Data.MySqlClient

進口System.Data

進口System.Data.SqlClient的

公共類Form1中

Dim SQLConnectionSQL As MySqlConnection = New MySqlConnection 
Dim connectionStringSQL As String = "Data Source=solomon;Initial Catalog=testapp;Persist Security Info=True;User ID=sa;Password=Passw0rd" 


Dim connectionString As String = "Server=192.168.1.199; User Id=gil; Password=Passw0rd; Database=testapp" 
Dim SQLConnection As MySqlConnection = New MySqlConnection 
Dim oDt_sched As New DataTable() 


Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click 
    Dim con_Solomon As String 
    Dim connection As SqlConnection 
    Dim command As SqlCommand 
    Dim adapter As New SqlDataAdapter 
    Dim ds As New DataSet 
    Dim sql As String 

    Dim str_carSql As String 

    lblCounter.Text = 0 

    con_Solomon = "Data Source=solomon;Initial Catalog=MARYLANDAPP;Persist Security Info=True;User ID=sa;Password=Passw0rd" 

    sql = "SELECT LTRIM(RTRIM(DocType)) as DocType, LTRIM(RTRIM(cust_classID)) as cust_classID, LTRIM(RTRIM(salesman)) as salesman, LTRIM(RTRIM(CustId)) as CustId, LTRIM(RTRIM(name)) as name, LTRIM(RTRIM(ClassID)) as ClassID, LTRIM(RTRIM(invoice_no)) AS refnbr, invoice_delv_date AS Delv_DocDate, Age, AR_Amount, LTRIM(RTRIM(STATUS)) as STATUS, LTRIM(RTRIM(AGE_GROUP)) as AGE_GROUP, LTRIM(RTRIM(company)) AS comp, '' AS Deposit_Date, Credit_Limit, LTRIM(RTRIM(Terms)) as Terms, LTRIM(RTRIM(customer_name)) AS ShipName, LTRIM(RTRIM(PO_Number)) as PO_Number, LTRIM(RTRIM(Kob)) as Kob, LTRIM(RTRIM(check_date)) as check_date FROM a_aging_ardoc_report" 

    connection = New SqlConnection(con_Solomon) 

    Try 
     connection.Open() 
     command = New SqlCommand(sql, connection) 
     command.CommandTimeout = 420 
     adapter.SelectCommand = command 
     adapter.Fill(ds, "PO_Monitoring") 
     adapter.Dispose() 
     command.Dispose() 
     connection.Close() 


     ''****** MYSQL CONNECTION ***** 
     SQLConnection = New MySqlConnection() 
     SQLConnection.ConnectionString = connectionString 
     SQLConnection.Open() 
     Dim sqlCommand As New MySqlCommand 

     Dim delv_docdate, check_date 

     For a = 0 To ds.Tables(0).Rows.Count - 1 

      With ds.Tables(0).Rows(a) 


       If Not IsDBNull(.Item(7)) Then 
        delv_docdate = .Item(7) 
       Else 
        delv_docdate = "" 
       End If 


       If Not IsDBNull(.Item(19)) Then 
        check_date = .Item(19) 
       Else 
        check_date = "" 
       End If 

       str_carSql = "insert into agingreportsummary(doctype,cust_classid,salesman,custId,name,classid,refnbr,delv_docdate,age,ar_amount,status,age_group,comp,credit_limit,terms,shipname,po_number,kob,check_date) " & _ 
       "VALUES('" & .Item(0) & "','" & .Item(1) & "','" & Replace(.Item(2), "'", "") & "','" & .Item(3) & "','" & Replace(.Item(4), "'", "") & "','" & Replace(.Item(5), "'", "") & "','" & .Item(6) & "','" & delv_docdate & "'," & Replace(.Item(8), ",", "") & "," & Replace(.Item(9), ",", "") & ",'" & Replace(.Item(10), "'", "") & "','" & .Item(11) & "','" & .Item(12) & "','" & .Item(14) & "','" & .Item(15) & "','" & Replace(.Item(16), "'", "") & "','" & Replace(.Item(17), "'", "") & "','" & .Item(18) & "','" & check_date & "');" 


      End With 


      sqlCommand.Connection = SQLConnection 
      sqlCommand.CommandText = str_carSql 
      sqlCommand.ExecuteNonQuery() 
     Next a 

     SQLConnection.Close() 

     MsgBox("Finish") 
    Catch ex As Exception 
     MsgBox(str_carSql) 
     MsgBox(ex.Message) 
    End Try 
End Sub 

末級

回答

0

你可以嘗試使用參數化查詢而不是構建查詢爲每一行。這應該會稍微改善,因爲聲明不需要每次都準備好。

將所有必需的參數添加到命令中。

設置命令文本一次,並將其更改爲使用參數。

裏面的循環中,您將只設置的參數值,並調用ExecuteNonQuery方法

這本來不是容易受到SQL注入的額外好處。

希望可以幫到

+0

嗨,感謝您的及時回覆。你能教我如何運作嗎?請...導致我在VB2010新手。再次感謝。 – 2015-02-11 04:03:01

+0

這不是這個網站的工作原理。自己嘗試一下,詢問你是否卡住了 – Spock 2015-02-11 04:05:03