Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
how i copy table and its all values from one database and save ti to another database.



1-what is the sql query for that
2-how do this by using visual studio c#

before copying

databases in sql server
1-company
2-school

company have two two tables
employee
stock

school have one table
employee


after copying


company have two two tables
employee
stock

school have one table
employee
stock
Posted
Comments
Kornfeld Eliyahu Peter 12-May-14 13:46pm    
Why C#? You can move data between SQL tables using only SQL!
Member 10689154 12-May-14 13:49pm    
ok then sql query for that ?
Kornfeld Eliyahu Peter 12-May-14 14:06pm    
Oh, that?! It's peace-of-cake, you only need to learn SQL...

For something so 'simple' , there are probably dozens of ways of doing this . And the one chosen will depend on a) How often you want to do this and b) Is it worth spending lots of time learning them .

For SQL I would either use 'select into' ( see w3 schools for syntax) or generate a create script ( this builds all the indexes and triggers unlike 'select into' ) Then edit it for the new db then I would run an export and import from Sql Manager ( assuming its MSSql , other DBs have similar tools) . Ask a DB and they will talk of SSIS jobs or similar. You can write a query to do the same task but normally I wouldn't bother . Such a query would typically be slow , and you might get issues on large numbers of rows , the syntax of the query would change depending on what id columns you are using .

Alternatives include several tools that can synchronise DBs/Tables . E.g microsoft Sql Azure synch . Although these tools can be a pain in the backside when you have large tables and want to make column changes .

In c# you have the option of many ways of basically replacing the SQL script with C# code . You could use entity framework to emit the code , or use a migration framework such as fluent . Or you could write your own to create the table . I think you then have to iterate over the data using your data access technology of choice and write each one to the new table .

BUT if its a one off job then you are probably better off using sql . All the above have their advantages and disadvantages , but for just moving data around SQL is still the tool to go to most of the time.

It does beg the question though as to why you want company and school tables in the same db ( homework question ?)
 
Share this answer
 
In Microsoft SQL Server Management Studio, you can access the structure of a table by right clicking on it in Object Explorer.

Select Script Table as: Create To: New Query Editor Window.

With that code, you can create the table in a different database.

If you need the structure and the data, you can right click on the source database in object explorer.

Select Tasks: Export Data

You will need to continue with the selections that are correct for your situation.

Neither of these are fool proof.

If you are unsure of what you are doing ask for assistance.
 
Share this answer
 
Comments
ludemade 12-May-14 15:27pm    
I am not sure why I got a down vote with no comment. He asked how to get the SQL("ok then sql query for that?"), and my response is a valid answer to that question.

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