gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:43:10 25 4
gpt4 key购买 nike

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

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

最佳答案

假设您已经拥有连接到数据库所需的信息(看起来您已拥有),请按如下方式设置阅读器:

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

然后将数据直接读入数组列表

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数据库发送到mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com