vb.net将数据从datatable导入到sql表

时间:2017-10-25 12:10:57

标签: sql vb.net

我有以下VB.NET代码将数据从.csv文件复制到数据表(dt)。列是F1,F2,F3,F4。我想使用以下列将数据表(dt)中的数据复制到sql表(ontledings):BLOk,KULTIVAR,DATUM和SUIKER。

    Private Sub OesskattingsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OesskattingsToolStripMenuItem.Click

    Dim folder = "C:\Users\Administrator\Desktop\Skedulering\EZY Wine Data\"
    Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
    Dim dt As New DataTable
    Using Adp As New OleDbDataAdapter("select * from labanal2.csv", CnStr)
        Adp.Fill(dt)
    End Using



    Dim strsql As String = "insert into ontledings (BLOK,KULTIVAR,DATUM,SUIKER) values (@F1,@KF2,@F3,@SF4)"

    Dim SqlconnectionString As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Administrator\Desktop\Skedulering\Skedulering\Skedulering\SkeduleringDatabasis.mdf;Integrated Security=True"

    Using connection As New SqlClient.SqlConnection(SqlconnectionString)
        Dim cmd As New SqlClient.SqlCommand(strsql, connection)
        With cmd.Parameters
            .Add("@F1", SqlDbType.VarChar, 50, "BLOK")
            .Add("@F2", SqlDbType.VarChar, 50, "KULTIVAR")
            .Add("@F3", SqlDbType.Date, 50, "DATUM")
            .Add("@F4", SqlDbType.Decimal, 50, "SUIKER")
        End With

        Dim adapter As New SqlClient.SqlDataAdapter()

        adapter.InsertCommand = cmd

        Dim iRowsInserted As Int32 = adapter.Update(dt)

    End Using
    MsgBox("Finished")
End Sub

然而,当我执行代码时,它运行没有错误,但没有数据插入到sql表中。我已经尝试并阅读了许多问题和答案,现在真的很困惑。

非常感谢任何帮助。

0 个答案:

没有答案