我如何从一个数据库复制表并将Ti保存到另一个数据库 [英] How I Copy Table From One Database And Save Ti To Another Database

查看:100
本文介绍了我如何从一个数据库复制表并将Ti保存到另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个数据库复制表及其所有值并将ti保存到另一个数据库。







1 - 什么是sql查询的

2 - 如何通过使用visual studio c#



复制之前/>


sql server中的数据库

1-company

2-school



公司有两张两张桌子

员工

股票



学校有一张桌子

员工




复制后






公司有两张两张桌子

员工

股票



学校有一张表

员工

股票

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

推荐答案

对于那些简单的东西,可能有很多方法这样做。选择的那个将取决于a)你多久想要这样做和b)是否值得花费大量时间学习它们。



对于SQL我会使用'select into'(参见w3学校的语法)或生成一个创建脚本(这会构建所有索引和触发器,而不是'select into ')然后编辑它为新的数据库然后我将从Sql管理器运行导出和导入(假设其MSSql,其他数据库具有类似的工具)。询问数据库,他们会谈论SSIS工作或类似工作。您可以编写查询来执行相同的任务,但通常我不会打扰。这样的查询通常很慢,并且您可能会遇到大量行的问题,查询的语法会根据您使用的id列而改变。



备选方案包括几个可以同步DB /表的工具。例如,Microsoft Sql Azure同步。虽然当您拥有大型表并希望进行列更改时,这些工具可能会让您感到痛苦。



在c#中,您可以选择多种基本上用C#代码替换SQL脚本的方法。您可以使用实体框架来发出代码,或使用诸如流畅的迁移框架。或者您可以编写自己的表来创建表。我认为您必须使用您选择的数据访问技术迭代数据并将每个数据写入新表。



但如果它是一个一个工作,那么你可能最好使用sql。以上所有都有它们的优点和缺点,但是仅仅围绕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 ?)


在Microsoft SQL Server Management Studio中,您可以通过在对象资源管理器中右键单击来访问表的结构。



选择脚本表作为:创建到:新建查询编辑器窗口。



使用该代码,您可以在不同的数据库中创建表格。



如果您需要结构和数据,可以右键单击对象资源管理器中的源数据库。



选择任务:导出数据



您需要继续选择适合您情况的选项。



这些都不是万无一失的。



如果您不确定自己在做什么,请寻求帮助。
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.


这篇关于我如何从一个数据库复制表并将Ti保存到另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆