Click here to Skip to main content
15,889,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

I'm working with a problem where I couldn't find a better way insert a recordset to sql table.

Currently, I'm looping each recordset to insert. It is working fine for now but it is not a good solution when the recordset size expands future.

Thus, anyone here can help me to do something like one time "insert into" table.

I got stuck in half way, see below

VB
'rstemp-record set
'SQLTable - Sql Table

tmpSql = ""
tmpSql = tmpSql & "Insert Into SQLTable (field1, field2, field3, field4) "
tmpSql = tmpSql & "Select field1, field2, field3, field4 From xxxxx "
Cmd.CommandText = tmpSql
Cmd.Execute



How to state the recordset at the xxxxx. Can anyone help?

Kindly post the url if there is similar thread to this.

Thanks,
Skunkhead :)
Posted

Look at doing a bulk insert via SqlBulkCopy[^]

Here is one way to do it, inserting a whole DataTable:
VB
Dim strConnect As String = "Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True"
Using con As New SqlConnection(strConnect)
	con.Open()
	Using bulk As New SqlBulkCopy(con)
		bulk.DestinationTableName = "Test"
		Dim dt As New DataTable()
		dt.Columns.Add("Id")
		dt.Columns.Add("Data")
		dt.Rows.Add("ID 1", "DATA 1")
		dt.Rows.Add("ID 2", "DATA 2")
		dt.Rows.Add("ID 3", "DATA 3")
		dt.Rows.Add("ID 4", "DATA 4")
		bulk.WriteToServer(dt)
	End Using
End Using
 
Share this answer
 
Comments
Maciej Los 20-May-12 11:14am    
Good answer, my 5!
hi in sql server 2008

SQL
create database solution
go

create table test
(
filed1 int identity primary key,
filed2 nvarchar(50),
filed3 nvarchar(50),
filed4 bigint)
)
go

create proc ups_test
@a nvarchar(50),
@b nvarchar(50),
@c bigint
as
begin
insert dbo.test
values(@a,@b,@c)
end

in the above code first create database solution then create table test and at the end of all create proc ups_test

then go and create c# code
in the click event write this and fill the procedure value with 3 text box value

C#
private void btninsert_Click(object sender, EventArgs e)
       {
           try
           {
               SqlCommand sqm = new SqlCommand("ups_insert",               new SqlConnection("data source=.;database=test;uid=your sql username;pwd=your sql password));
               sqm.Connection.Open();
               sqm.CommandType = CommandType.StoredProcedure;
               sqm.Parameters.Add("@a", SqlDbType.NVarChar, 5).Value = txtyear.Text;
               sqm.Parameters.Add("@b", SqlDbType.NVarChar, 5).Value = txtmonth.Text;
               sqm.Parameters.Add(@c, SqlDbType.BigInt).Value = txtqty.Text;
               sqm.ExecuteNonQuery();
               sqm.Connection.Close();
           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.Message);
           }



       }
 
Share this answer
 
v2
Comments
Member 13848678 29-May-18 3:31am    
I want transfer ms access table records to sql server table using connection through VB6. can any one give solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900