I need to input multiple rows at once with one insert statement from the data table into a database on PostgreSQL
insert into table1 values
('1','aaa'),
('2','bbbb'),
('3','ccc')
this is my code to input multiple rows with one insert statement and its works
Sub bandingkan_data_tblpibconr()
Dim Bs_access As New DataTable
Bs_access = query.LoadAcces_tblpibconr
Dim dt3 As New DataTable
dt3 = Bs_access
Cmd.Connection = connNpgsql.OpenConnection()
Dim kueri As String
kueri = "insert into tblpibconr values"
For i = 0 To dt3.Rows.Count - 1
kueri = kueri + "('" + dt3.Rows(i)("car").ToString + "','" + dt3.Rows(i)("reskd").ToString + "','" + dt3.Rows(i)("contno").ToString + "','" + dt3.Rows(i)("contukur").ToString.Trim() + "','" + dt3.Rows(i)("conttipe").ToString + "')"
kueri = kueri + ","
Next
kueri = kueri.Remove(kueri.Length - 1, 1)
Cmd.CommandText = kueri
Cmd.ExecuteNonQuery()
connNpgsql.CloseConexion()
End Sub
but I need to add these parameters on my code
Cmd.Parameters.AddWithValue("@car", NpgsqlTypes.NpgsqlDbType.Text).Value = dt3.Rows(i)("car").ToString
Cmd.Parameters.AddWithValue("@reskd", NpgsqlTypes.NpgsqlDbType.Text).Value = dt3.Rows(i)("reskd").ToString
Cmd.Parameters.AddWithValue("@contno", NpgsqlTypes.NpgsqlDbType.Text).Value = dt3.Rows(i)("contno").ToString
Cmd.Parameters.AddWithValue("@contukur", NpgsqlTypes.NpgsqlDbType.Text).Value = dt3.Rows(i)("contukur").ToString.Trim()
Cmd.Parameters.AddWithValue("@conttipe", NpgsqlTypes.NpgsqlDbType.Text).Value = dt3.Rows(i)("conttipe").ToString
I don't know how to combine my parameters and my kueri variable
kueri = kueri + "('" + dt3.Rows(i)("car").ToString + "','" + dt3.Rows(i)("reskd").ToString + "','" + dt3.Rows(i)("contno").ToString + "','" + dt3.Rows(i)("contukur").ToString.Trim() + "','" + dt3.Rows(i)("conttipe").ToString + "')"
kueri = kueri + ","
can anyone help me?
And if you know how to input data from the data table into the PostgreSQL database faster then this method using vb.net please let me know.
DataTablesand only using one of them? TheNewkeyword is used to create a new object so don't use it unless you actually want a new object. The first four lines of that method should be replaced with one:Dim table As DataTable = query.LoadAcces_tblpibconr().DataAdapterlike @jmcilhinney demonstrated in his answer. If you don´t understand it, google it, there are a lot of tutorials about it and come back if you have a specific problem