在 C# 中从数据库生成脚本
我正在开发一个 C# 应用程序,我想要一个窗口来显示数据库中所有现有的表,用户可以选择要在 sql 紧凑型新数据库中重新创建的表,某些表必须包含初始信息,其他表必须包含初始信息只是表的结构。
有什么帮助可以重新创建这个表吗?
我想生成现有表(*.sql)的脚本并从文件创建表到新数据库,但我不知道如何进行查询来生成sql表。
PS:现有的表在sqlserver中,我想在sqlcompact中创建一个表。
I am developing a C # application and I want a window that displays all existing tables in the database the user make the choice of tables that want to recreate in a new database in a sql compact, some tables must contain the initial information and the other just the structure of the table.
Is there any help to recreate this table?
I think to generate the script of the existing table (*.sql) and creating table from file to the new database, but I do not know how to make a query to generate a sql table.
PS: the existing table is in sqlserver, and I want to create a table in sqlcompact.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看看下面的视图:
我手头没有代码(抱歉),但我之前已经使用过这些来基于纯 TSQL 中数据库自己的元数据重新创建表定义,并且没有理由不能这样做也可以在应用程序中用 C# 完成。将它们连接在一起,为最终定义声明一个字符串,然后将光标浏览结果集以获取表中的所有字段,从而构建输出。完成后,将其返回给客户。出于测试目的,将代码的输出与要求 SSMS 编写表创建脚本时生成的输出进行比较,希望您能够相对快速地重新创建表定义。
Have a look at the following views:
I don't have the code to hand (sorry) but I've used these before to recreate a table definition based off the database's own metadata in pure TSQL and there's no reason it couldn't be done in C# within the application just as well. Join them up together, declare a string for your final definition and cursor through the results set to get all the fields in your table, building up the output. When you're done, return that to the client. For testing purposes compare the output of your code to what SSMS produces when you ask it to script creation of a table and hopefully you'll be able to get your table definitions recreated relatively quickly.
您可以使用 SMO 来执行此操作。
SQL Server 管理对象
这里有一个简单的示例:
http://www.sqlservercentral.com/articles/SMO/scriptdatabaseobjectswithsmo/2342/
You can use SMO to do this.
SQL Server Management Objects
A quick example is here:
http://www.sqlservercentral.com/articles/SMO/scriptdatabaseobjectswithsmo/2342/
我不知道Sql Server 和 Sql Server CE 之间的差异,但是您可以使用 Sql Server Management Studio 为表、过程等生成脚本。右键单击数据库,然后单击“任务”>“生成脚本。
据我所知,大多数不支持的功能是查询、过程、视图......
I don't know the differences between Sql Server and Sql Server CE but you can use Sql Server Management Studio to generate script for tables, procedures etc. Right click to database then Tasks > Generate Scripts.
As I know most of the not supported features are queries, procedures, views...
如果您使用 NHibernate 进行数据访问,他们有一个很好的 SchemaExport 工具,该工具适用于 NHibernate 支持的所有数据库(不仅仅是 SQL Server)。因此,您的解决方案是列出所有映射实体,让用户选择他想要的实体,使用所选实体创建新配置,并使用 SchemaExport 以您想要的方言生成 SQL。
If you are using NHibernate for data access, they have a nice SchemaExport tool that will work for all databases supported by NHibernate (not just SQL Server). Thus, your solution would be to list all mapped entities, let user pick the ones he wants, create a new configuration with selected entities and use SchemaExport to generate SQL in the dialect you want.
您可以使用我的脚本 API/库以正确的 SQL Server Compact 方言生成 CREATE TABLE 脚本。 SMO 无法为您做到这一点,它针对的是各种 SQL Server 版本。
http://exportsqlce.codeplex.com
You can use my scripting API/library to generate CREATE TABLE scripts in proper SQL Server Compact dialect. SMO cannot do that for you, it i targeted at various SQL Server versions.
http://exportsqlce.codeplex.com