?
Solved

Load data from db insert into another

Posted on 2006-07-12
6
Medium Priority
?
283 Views
Last Modified: 2010-04-16
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;

        try
        {

            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();
           
            //ds.Clear();
            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
0
Comment
Question by:ziwez0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
DarkXiphoid earned 1920 total points
ID: 17089991
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);  
sqlCom.ExecuteNonQuery;
        }

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.

Regards,

Robert
0
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 80 total points
ID: 17090243
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



dataadapter.update(dataset2)
0
 
LVL 2

Author Comment

by:ziwez0
ID: 17091078
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);
                                command.ExecuteNonQuery();
                         
                            }
                    connection.Close();
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:ziwez0
ID: 17091515
This worked

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

why did the command.Parameters not work?
                               
0
 
LVL 4

Expert Comment

by:DarkXiphoid
ID: 17092221
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?
0
 
LVL 2

Author Comment

by:ziwez0
ID: 17092925
works now, thanks for all your help. (no other errors)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question