将数据从 sql 数据库发送到 mysql 数据库
Sending data from sql database to mysql database
我需要从 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")
这个问题是将正确的元值分配给变量,以便它可以插入到正确的列中
假设您已经拥有连接到数据库所需的信息(看起来您已经拥有),请像这样设置您的 reader:
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)
可以访问您想要访问的任何其他项目
现在您可以通过建立连接并遍历数据将数据放入另一个 table:
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
我知道这个过程比简单地填充数据适配器要复杂得多,但它可以让您更好地控制数据,因此您可以随心所欲地使用它。
希望对您有所帮助。
我设法通过使用以下子查询将我需要的数据拆分到它们自己的列中。以为我会 post 它
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'
我需要从 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")
这个问题是将正确的元值分配给变量,以便它可以插入到正确的列中
假设您已经拥有连接到数据库所需的信息(看起来您已经拥有),请像这样设置您的 reader:
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)
可以访问您想要访问的任何其他项目现在您可以通过建立连接并遍历数据将数据放入另一个 table:
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
我知道这个过程比简单地填充数据适配器要复杂得多,但它可以让您更好地控制数据,因此您可以随心所欲地使用它。
希望对您有所帮助。
我设法通过使用以下子查询将我需要的数据拆分到它们自己的列中。以为我会 post 它
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'