将数据从sql数据库发送到mysql数据库

时间:2017-07-14 11:24:28

标签: mysql database wordpress vb.net select-query

我需要从sql数据库中获取数据并以编程方式将其发送到mysql数据库,我该怎么做?

以下是我正在尝试的内容

    Try
        con3.ConnectionString = MyConnectionString
        con3.Open()
        Dim cmd As New MySqlCommand _
        ("SELECT kjuy6_postmeta.meta_value, kjuy6_posts.ID, kjuy6_posts.post_status, kjuy6_woocommerce_order_items.order_item_name  FROM kjuy6_postmeta INNER JOIN kjuy6_posts ON kjuy6_postmeta.post_id = kjuy6_posts.ID And kjuy6_postmeta.post_id = kjuy6_posts.ID, kjuy6_woocommerce_order_items WHERE kjuy6_posts.post_type = 'shop_order' AND kjuy6_postmeta.meta_key = '_paid_date' OR kjuy6_postmeta.meta_key = '_billing_phone'")
        cmd.Connection = con3
        'Dim reader As MySqlDataReader = cmd.ExecuteReader
        Dim da As New MySqlDataAdapter(cmd)
        da.Fill(ds)

        Dim a = ds.Tables(0).Rows(0).Item("meta_value")
        Dim b = ds.Tables(0).Rows(0).Item("meta_value")
        Dim c = ds.Tables(0).Rows(0).Item("post_status")
        Dim d = ds.Tables(0).Rows(0).Item("order_item_name")

        If Not IsNothing(cmd) Then
            con.Open()
            ExecuteData("INSERT INTO BillingInfo (vchUsername, vchExpiryDate, vchOrderStatus, vchSubscriptionType, intSubscriberid) VALUES('" & a & "','" & b & "','" & c & "','" & d & "', '" & SubscriberId & "')")
        End If

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    Finally
        con3.Close()
        con.Close()
    End Try

以下是分离元值并将插入查询中的变量用作值

 Dim da As New MySqlDataAdapter(cmd)
        da.Fill(ds)
        Dim a = ds.Tables(0).Rows(0).Item("meta_value")
        Dim b = ds.Tables(0).Rows(0).Item("meta_value")
        Dim c = ds.Tables(0).Rows(0).Item("post_status")
        Dim d = ds.Tables(0).Rows(0).Item("order_item_name")

这个问题是为变量分配正确的元值,以便可以将其插入到正确的列中

2 个答案:

答案 0 :(得分:1)

假设您已拥有与数据库建立连接所需的信息(看起来像这样),请按以下方式设置您的阅读器:

cmd.CommandText = "SELECT kjuy6_postmeta.meta_value, kjuy6_posts.ID, kjuy6_posts.post_status, kjuy6_woocommerce_order_items.order_item_name  FROM kjuy6_postmeta INNER JOIN kjuy6_posts ON kjuy6_postmeta.post_id = kjuy6_posts.ID And kjuy6_postmeta.post_id = kjuy6_posts.ID, kjuy6_woocommerce_order_items WHERE kjuy6_posts.post_type = 'shop_order' AND kjuy6_postmeta.meta_key = '_paid_date' OR kjuy6_postmeta.meta_key = '_billing_phone'"

现在创建一个ArrayList来保存所有数据:

Dim column_data as New Arraylist
Dim table_data as New Arraylist

然后直接将数据读入你的arraylists

Dim dbReader As MySqlDataReader = Cmd.ExecuteReader
While dbReader.Read()
  For x As Integer = 0 To dbReader.FieldCount - 1
     column_array.Add(dbReader(x))
     item_count = dbReader.FieldCount
  Next
  table_data.Add(column_data.ToArray)
  column_data.Clear()
 End While
 dbReader.Close()
 con3.Close()

现在,您将所有数据都整理成一个名为table_data的二维数组。因此,您只需浏览行和列并提取您要使用的数据即可。记住数组是0索引的,第一行的第一个元素是table_data(0)(0),依此类推。一行中的项目数存储在item_count中,行数为table_data.count()

您想要访问的任何其他项目将由table_data(row)(col)

访问

现在,您可以通过建立连接并迭代数据将数据放入另一个表中:

dim insert_string as string = ""
For x as integer = 0 to table_data.count -1
  insert_string = "insert into mytable (meta_value1, meta_value2,
  post_status, order_item_name) values ("+ table_data(x)(0) +"," +  
  table_data(x)(1) +","+ table_data(x)(2)+","+ table_data(x)(3) + ")"
  cmd.commandtext = insert_string
  cmd.executeNonQuery()
Next x

我知道这个过程比简单地填充数据适配器更复杂,但它可以让您更好地控制数据,以便您可以随意使用它。

希望有所帮助。

答案 1 :(得分:0)

我设法通过使用以下子查询将我需要的数据拆分到他们自己的列中。虽然我会发布它

SELECT kjuy6_posts.ID, kjuy6_posts.post_status, kjuy6_woocommerce_order_items.order_item_name, b.meta_value AS PaidDate, b.meta_value AS MobileNumber
FROM kjuy6_posts INNER JOIN kjuy6_woocommerce_order_items ON kjuy6_woocommerce_order_items.order_id = kjuy6_posts.ID INNER JOIN
                             (SELECT post_id, meta_value
                               FROM kjuy6_postmeta
                               WHERE (meta_key = '_billing_phone') b ON b.post_id = kjuy6_posts.ID
WHERE kjuy6_posts.post_type = 'shop_order'