• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Load data from db insert into another

Hi could someone give me a quick example of loading data from DB1.table into DB2.table (win forms)
so far..


        OleDbConnection conn;
        OleDbDataAdapter dAdapter;
        DataSet ds;


            string strdblocation = textBox1.Text;
            string cmdString = "Select Email, Initials FROM tblrequest";
            conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + strdblocation + "'");
            dAdapter = new OleDbDataAdapter(cmdString, conn);
            ds = new DataSet();
            dAdapter.Fill(ds, "tblrequest");
          dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "tblrequest";
        catch (Exception ex)
            label2.Text = ex.ToString();

I want to then insert the data into a sql database:Maindb, table: eaddress. fields: eName & eClient
  • 3
  • 2
2 Solutions
After getting the dataset from your above code:

//insert your SQL server connection process here: make the connection, and open it.

        foreach (DataRow row in ds.Tables[0].Rows)
sqlCom Command = new SqlCommand;
sqlCom.CommandText = @"INSERT INTO eadress (eName, eClient) VALUES (?eName, ?eClient);
string eName = row[Email].ToString();
sqlCom.Parameters.Add("?eName", eName);  
string eClient = row[Initials].ToString();
sqlCom.Paramaters.Add("?eClient", eClient);  

I hope this works, I didn't test it though. The row[Initials] and row[Email] part might have slightly wrong syntax - tinker around with it a bit :)

I hope this helps.


Gautham JanardhanCommented:
above example is a good option if performance is not an issue ... the problem is that the query would run for as many rows in the data table

another soln is

Fill another dataset  with an adapterwith the table u want to insert into with a where clause like 1=2 so no data would
be present but u would get the data structure

then u could pium data from the first data set to the second using the code above
and the after that call

ziwez0Author Commented:
opps got an error "System.IndexOutOfRangeException"

foreach (DataRow row in ds.Tables["tblrequest"].Rows)

                          SqlCeCommand command = connection.CreateCommand();
                                SqlCeCommand sqlCom = new SqlCeCommand();
                                command.CommandText = @"INSERT INTO Eaddress (name, emailad) VALUES (@Email, @Initials)";
                                string stremail = row["Email"].ToString();
                                command.Parameters.Add("Email", stremail);
                                string strclient = row["Initials"].ToString();
                                command.Parameters.Add("Initials", strclient);
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

ziwez0Author Commented:
This worked

string stremail = row["Email"].ToString();
  command.CommandText = @"INSERT INTO Eaddress (name) VALUES ('" + stremail + "')";

why did the command.Parameters not work?
I don't know - I'm used to MySql so I might have a syntax error. But if it works using your way - thats perfect!

Are you getting any other errors or does it work now?
ziwez0Author Commented:
works now, thanks for all your help. (no other errors)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now