gpt4 book ai didi

sql - Visual Basic,复制成功但未将数据插入 SQL TABLE

转载 作者:行者123 更新时间:2023-12-02 18:04:47 27 4
gpt4 key购买 nike

我这里有一些问题。我需要帮助。

最近,我创建了一个名为 stock.mdf 的本地数据库,应用程序会将所有数据从托管 MySQL 数据库获取到此本地 SQL Server 数据库中。

我正在使用sqlBulkCopy插入所有数据。我尝试插入后查看它。但是当我关闭应用程序时,我会返回检查表数据。它没有插入。这是为什么?

这是我的代码:

此处将从托管检索数据

    Dim connStr As String = "server=xxxx;user=xxx;database=xxx;password=xxxx;"
Dim conn As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand
Dim Adapter As New MySqlDataAdapter
Dim StockData As New DataTable

Try
Dim SQL As String = "SELECT * FROM stock"
Console.WriteLine("Connecting to MYSQL.....")
conn.Open()
cmd.Connection = conn
cmd.CommandText = SQL

Adapter.SelectCommand = cmd
Adapter.Fill(StockData)

' StockViewGrid.DataSource = StockData
Catch ex As Exception
Console.WriteLine(ex.ToString())

Finally
conn.Close()
Console.Write("Done")
End Try

这将是 sqlBulkCopy 工作的地方:

此外,我正在尝试从 stock 表中查看。

 Dim Local_connectionStr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|stock.mdf;Integrated Security=True"
Dim Local_conn As New SqlConnection(Local_connectionStr)
Dim Local_cmd As New SqlCommand
Dim Local_Adapter As New SqlDataAdapter
Dim Local_StockData As New DataTable

Try
Using sqlBulkCopy As New SqlBulkCopy(Local_conn)
'Set the database table name
sqlBulkCopy.DestinationTableName = "stock"

'[OPTIONAL]: Map the DataTable columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("stockId", "stockId")
sqlBulkCopy.ColumnMappings.Add("id_android", "id_android")
sqlBulkCopy.ColumnMappings.Add("itemCode", "itemCode")
sqlBulkCopy.ColumnMappings.Add("quantity", "quantity")
Local_conn.Open()
sqlBulkCopy.WriteToServer(StockData)
Local_conn.Close()
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString())

Finally
Local_conn.Close()
Console.Write("Done")

End Try


Try
Dim SQL As String = "SELECT * FROM stock"
Console.WriteLine("Connecting to MYSQL.....")
Local_conn.Open()
Local_cmd.Connection = Local_conn
Local_cmd.CommandText = SQL

Local_Adapter.SelectCommand = Local_cmd
Local_Adapter.Fill(Local_StockData)

StockViewGrid.DataSource = Local_StockData
Catch ex As Exception
Console.WriteLine(ex.ToString())

Finally
Local_conn.Close()
Console.Write("Done")

End Try

最佳答案

由于我无法发表评论(积分不够),我会将我的想法放在这里。您可以检查是否获得了预期的数据

Console.WriteLine(StockData.Rows.Count.ToString)
Console.ReadLine()

此外,您的目标表没有自动递增的 PK,是吗?对于评论作为答案感到抱歉。 (未经测试的代码)

关于sql - Visual Basic,复制成功但未将数据插入 SQL TABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46131391/

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