【问题标题】:Sending data from sql database to mysql database将数据从sql数据库发送到mysql数据库
【发布时间】:2017-07-14 11:24:27
【问题描述】:

我需要从 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")

问题在于将正确的元值分配给变量,以便可以将其插入正确的列中

【问题讨论】:

  • 如果您只是直接处理数据而不使用数据适配器,它可能会帮助您了解正在发生的事情的细节。如果您对使用数据适配器以外的其他东西感兴趣,请告诉我。我将作为答案发布。
  • 感谢所有帮助,我认为提取所有信息并将它们插入正确列的唯一方法是进行子查询。

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


【解决方案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中存储了一行的item数,行数为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

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

希望对您有所帮助。

【讨论】:

  • 非常感谢 ^^ 认为它会比我尝试做的方式更好。
  • 如果您在实现它时需要帮助,请回信,我会给您详细信息。我经常使用这种处理数据的过程。我还经常在 MySQL 和 SQL 服务器之间传输大量数据。
  • 我可以给你我的电子邮件吗?我无法实现它,但我设法通过执行子查询得到了我需要的结果
  • 您能帮我理解以下内容的用途吗? column_array.Add(dbReader(x)) item_count = dbReader.FieldCount
  • 回答您的问题:dbReader 正在从数据库中读取项目。它一次读取一列项目。所以'x'是它当前所在列的索引。如果有多个列,则 x 从 0 开始,每列增加 1。 FieldCount 是数据读取器读取行时找到的列数。 column_array.add(),将每一列添加到您的数组列表中,一次一个字段。读取所有字段后,整个列表将作为新行 table_data.add(your row) 添加到 table_data 数组列表中。
【解决方案2】:

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

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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-14
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多