将数据从sql数据库发送到mysql数据库
我需要从sql数据库获取数据,然后通过编程将其发送到mysql数据库。我该怎么做 这是我目前正在尝试的将数据从sql数据库发送到mysql数据库,mysql,database,wordpress,vb.net,select-query,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_stat
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
下面将分隔元值,并使用insert查询中的变量作为值
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 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
然后将数据直接读取到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索引的,第一行的第一个元素将是表_数据(0)(0),依此类推。行中的项数存储在item_count中,行数存储在table_data.count()中
您想要访问的任何其他项目都将通过表_data(row)(col)访问
现在,您可以通过建立连接并遍历数据将数据放入另一个表中:
Dim column_data as New Arraylist
Dim table_data as New Arraylist
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
我知道这个过程比简单地填充一个数据适配器要复杂得多,但它可以让您更好地控制数据,以便您可以随心所欲地使用它
希望能有所帮助。我通过使用下面的子查询,成功地将所需的数据拆分为各自的列。我想我会把它寄出去
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'
如果您只是直接处理数据而不使用数据适配器,它可能会帮助您了解发生的具体情况。如果您对使用数据适配器以外的其他产品感兴趣,请告诉我。我将作为答案发布。谢谢您的帮助,我认为浏览所有信息并将其插入正确列的唯一方法是进行子查询。非常感谢^^^我认为这将比我尝试的方式更好。如果您需要帮助实现它,请回信,我将向您提供详细信息。我经常使用这个处理数据的过程。我也经常在MySQL和SQL server之间传输一些数据。我可以给你发邮件吗?我无法实现它,但我通过执行子查询成功地获得了所需的结果。您能帮助我了解以下内容的用途吗?column_array.Add(dbReader(x))item_count=dbReader.fieldcount回答您的问题:dbReader正在从数据库读取项。它一次读取一列项。所以“x”是它当前所在列的索引。如果有多个列,那么x从0开始,每列增加一个。FieldCount是数据读取器读取行时找到的列数。column_array.add(),将每列添加到数组列表中,一次添加一个字段。读取所有字段后,整个列表将作为新行table_data.add(您的行)添加到table_data arraylist中。