Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do you copy records from one table to another?

Posted on 2005-03-07
9
Medium Priority
?
194 Views
Last Modified: 2010-04-16
Hi,

I'd like to copy the entire contents of a table to another table, with the same schema. And I know there must be an easy one line "SELECT * FROM TABLE1" type of command.

Can anybody finish the line above to direct the results to Table2?


Thanks,
Bob
0
Comment
Question by:ba272
  • 5
  • 4
9 Comments
 
LVL 11

Accepted Solution

by:
sachiek earned 1500 total points
ID: 13482722

ds.Tables[0].Copy() will copy both schema and data to new table.


private void CopyDataSet(DataSet myDataSet){
   // Create an object variable for the copy.
   DataSet copyDataSet;
   copyDataSet = myDataSet.Copy();
   // Insert code to work with the copy.
}

Same case for Table

private void CopyDataTable(DataTable myDataTable){
    // Create an object variable for the copy.
    DataTable copyDataTable;
    copyDataTable = myDataTable.Copy();
    // Insert code to work with the copy.
 }


hope you got it.

Sachi
0
 

Author Comment

by:ba272
ID: 13482770
Hi,

I don't use anyting like what you showed me.  I use SQL commands to handle everything so that it will be portable to other platforms.

I was looking for a "SELECT *" type of command to do the copying.  Do you know it?

Thanks,
Bob
0
 
LVL 11

Expert Comment

by:sachiek
ID: 13482792
Well, That is standard SQL statments.

Above code is by using dataset. That's the difference.

But what is your exact requirement? Do you want it to be done in .net and then insert into SQL Svr or into SQL server using C# directly?


Sachi

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!

 

Author Comment

by:ba272
ID: 13482845
I have written my entire program using OleDbCommand objects using SQL statements, and this isn't the time for me to learn how to use some new technology.  (I'm just about to reach a deliverable date.)

When I do have the chance, will I get a performance improvement by going that route?

In the meantime, do you know the SQL command?  I think it's:

sql = "SELECT * INTO " + targetTable + " FROM " + sourceTable;


Thanks,
Bob
0
 

Author Comment

by:ba272
ID: 13482864
Sachi,

Thanks for the code.  One day I hope to jump into it.

Bob
0
 
LVL 11

Expert Comment

by:sachiek
ID: 13482884
Ya I knew SQL Command.

So you are building a SQL string then executing that statment.

So sorted out this problem?


Sachi
0
 

Author Comment

by:ba272
ID: 13482901
Yes, I think I got it.  But would I someday benefit by using your approach?  Does it perform better than executing an SQL string?

Thanks,
Bob
0
 
LVL 11

Expert Comment

by:sachiek
ID: 13482928
Well, SQLcommand is much faster. Infact if we use dataset - it will fetch all those records to client side adding more data in viewstate etc etc.

I guess it is good to use SQL Commend which is simple and faster for your requirement.

Sachi
0
 

Author Comment

by:ba272
ID: 13482948
Thanks.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

580 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