Hi @AMER SAID ,
First, you need to create a template table with five columns. For example:
Dim dt As DataTable = New DataTable
dt.Columns.Add("cola")
dt.Columns.Add("colb")
dt.Columns.Add("colc")
dt.Columns.Add("cold")
dt.Columns.Add("cole")
Then merge your 'dt2' into the table.
dt.Merge(dt2)
Finally, you can refer to the following code to save datatable to database.
Dim cmd As New OleDbCommand("INSERT INTO EXPORT_TB(EXPORT_a,EXPORT_b,EXPORT_c,EXPORT_d,EXPORT_e) VALUES (@EXPORT_a,@EXPORT_b,@EXPORT_c,@EXPORT_d,@EXPORT_e)", con)
con.Open()
For Each row As DataRow In dt.Rows
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("EXPORT_a", IIf(row("cola").ToString() = "", "-", row("cola").ToString()))
cmd.Parameters.AddWithValue("EXPORT_b", IIf(row("colb").ToString() = "", "-", row("colb").ToString()))
cmd.Parameters.AddWithValue("EXPORT_c", IIf(row("colc").ToString() = "", "-", row("colc").ToString()))
cmd.Parameters.AddWithValue("EXPORT_d", IIf(row("cold").ToString() = "", "-", row("cold").ToString()))
cmd.Parameters.AddWithValue("EXPORT_e", IIf(row("cole").ToString() = "", "-", row("cole").ToString()))
cmd.ExecuteNonQuery()
Next
con.Close()
Hope it could be helpful.
Best Regards,
Xingyu Zhao
*
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.