Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Copying table

Copying table

Scheduled Pinned Locked Moved C#
databasetutorial
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    I have a created a database by the name of ABC. I want to copy the employees table from the northwind database to my database. Whats the best way to do it. Can someone direct me to a useful link or let me know how to do it dynamically.

    M L P A 4 Replies Last reply
    0
    • H humayunlalzad

      I have a created a database by the name of ABC. I want to copy the employees table from the northwind database to my database. Whats the best way to do it. Can someone direct me to a useful link or let me know how to do it dynamically.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Use SQL Server Management Studio SSMS. SQL 2008 (earlier are similar) Click on the database (ABC) select tasks select import data follow the wizard

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • H humayunlalzad

        I have a created a database by the name of ABC. I want to copy the employees table from the northwind database to my database. Whats the best way to do it. Can someone direct me to a useful link or let me know how to do it dynamically.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        That does not sound very C#-ish

        H 1 Reply Last reply
        0
        • H humayunlalzad

          I have a created a database by the name of ABC. I want to copy the employees table from the northwind database to my database. Whats the best way to do it. Can someone direct me to a useful link or let me know how to do it dynamically.

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          If they're on the same server you can use a SELECT statement to create a new table with the contents you want.

          1 Reply Last reply
          0
          • L Lost User

            That does not sound very C#-ish

            H Offline
            H Offline
            humayunlalzad
            wrote on last edited by
            #5

            I am trying something like this, but its not very accurate

            SqlDataAdapter da;
            DataSet ds = new DataSet();

                    string conString = @"server = .\\sqlexpress;
                                        integrated security = true;
                                        database = northwind";
                    da = new SqlDataAdapter("select \* from employees", conString);
                    da.Fill(ds);
                    DataTable table = ds.Tables\[0\];
            
                    string conString2 = @"server = .\\sqlexpress;
                                        integrated security = true;
                                        database = ABZ";
                    string commandString = "Create table Employees ( ";
                    SqlConnection con = new SqlConnection(conString2);
                    DataTable tab = new DataTable("Employees");
                    foreach (DataColumn  column in table.Columns )
                    {
                      
                        commandString = commandString + column.ColumnName + " " + column.DataType + " " + "nvarchar (50), "  ;
                       
                    }
            

            I can make a switch statement for the data type. But the maximum size of the data always shows up as -1

            1 Reply Last reply
            0
            • H humayunlalzad

              I have a created a database by the name of ABC. I want to copy the employees table from the northwind database to my database. Whats the best way to do it. Can someone direct me to a useful link or let me know how to do it dynamically.

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #6

              This[^] might help.

              The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files

              H 1 Reply Last reply
              0
              • A Abhinav S

                This[^] might help.

                The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files

                H Offline
                H Offline
                humayunlalzad
                wrote on last edited by
                #7

                Thanks in advance, as that was the second part of my problem, to copy the data once the table has been copied. The first one was to copy the table itself, still looking for an answer.

                P 1 Reply Last reply
                0
                • H humayunlalzad

                  Thanks in advance, as that was the second part of my problem, to copy the data once the table has been copied. The first one was to copy the table itself, still looking for an answer.

                  P Online
                  P Online
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  So you didn't look into using a SELECT to create and populate the table?

                  H 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    So you didn't look into using a SELECT to create and populate the table?

                    H Offline
                    H Offline
                    humayunlalzad
                    wrote on last edited by
                    #9

                    I was trying something like this

                    SqlDataAdapter da;
                    DataSet ds = new DataSet();

                            string conString = @"server = .\\sqlexpress;
                                                integrated security = true;
                                                database = northwind";
                            da = new SqlDataAdapter("select \* from employees", conString);
                            da.Fill(ds);
                            DataTable table = ds.Tables\[0\];
                    
                            string conString2 = @"server = .\\sqlexpress;
                                                integrated security = true;
                                                database = ABZ";
                            string commandString = "Create table Employees ( ";
                            SqlConnection con = new SqlConnection(conString2);
                            DataTable tab = new DataTable("Employees");
                            foreach (DataColumn  column in table.Columns )
                            {
                              
                                commandString = commandString + column.ColumnName +" nvarchar (50), "  ;
                               
                            }
                            
                            commandString = commandString + ")";
                    
                            SqlCommand cmd = new SqlCommand(commandString, con);
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                            MessageBox.Show("Table Created");
                    

                    But the prob was I was getting a -1 value for column.MaxLength when I was trying this

                    foreach (DataColumn column in table.Columns )
                    {

                                commandString = commandString + column.ColumnName +" nvarchar (" + column.MaxLength + "), "  ;
                               
                            }
                    

                    But how do you suggest I use the Select statement?

                    P 1 Reply Last reply
                    0
                    • H humayunlalzad

                      I was trying something like this

                      SqlDataAdapter da;
                      DataSet ds = new DataSet();

                              string conString = @"server = .\\sqlexpress;
                                                  integrated security = true;
                                                  database = northwind";
                              da = new SqlDataAdapter("select \* from employees", conString);
                              da.Fill(ds);
                              DataTable table = ds.Tables\[0\];
                      
                              string conString2 = @"server = .\\sqlexpress;
                                                  integrated security = true;
                                                  database = ABZ";
                              string commandString = "Create table Employees ( ";
                              SqlConnection con = new SqlConnection(conString2);
                              DataTable tab = new DataTable("Employees");
                              foreach (DataColumn  column in table.Columns )
                              {
                                
                                  commandString = commandString + column.ColumnName +" nvarchar (50), "  ;
                                 
                              }
                              
                              commandString = commandString + ")";
                      
                              SqlCommand cmd = new SqlCommand(commandString, con);
                              con.Open();
                              cmd.ExecuteNonQuery();
                              con.Close();
                              MessageBox.Show("Table Created");
                      

                      But the prob was I was getting a -1 value for column.MaxLength when I was trying this

                      foreach (DataColumn column in table.Columns )
                      {

                                  commandString = commandString + column.ColumnName +" nvarchar (" + column.MaxLength + "), "  ;
                                 
                              }
                      

                      But how do you suggest I use the Select statement?

                      P Online
                      P Online
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      A statement like SELECT * INTO JunkCopy FROM Junk.dbo.JunkName will copy a table and it contents. To add rows thereafter, a statement like INSERT INTO JunkCopy SELECT * FROM Junk.dbo.JunkName, but probably with a WHERE CLAUSE to avoid duplicates, should do the trick. But these will only work when the databases are on the same server or in linked servers. I have also had to copy tables among different database systems with code similar to (but infinitely better than :-D ) the code you posted.

                      H 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        A statement like SELECT * INTO JunkCopy FROM Junk.dbo.JunkName will copy a table and it contents. To add rows thereafter, a statement like INSERT INTO JunkCopy SELECT * FROM Junk.dbo.JunkName, but probably with a WHERE CLAUSE to avoid duplicates, should do the trick. But these will only work when the databases are on the same server or in linked servers. I have also had to copy tables among different database systems with code similar to (but infinitely better than :-D ) the code you posted.

                        H Offline
                        H Offline
                        humayunlalzad
                        wrote on last edited by
                        #11

                        Thanks a lot that did the trick. That was what I was looking for.

                        Select * into

                        P 1 Reply Last reply
                        0
                        • H humayunlalzad

                          Thanks a lot that did the trick. That was what I was looking for.

                          Select * into

                          P Online
                          P Online
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          I thought it might be.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups