简体   繁体   English

从MDF数据库生成脚本

[英]Generating Script from MDF database

I have a mdf database file that I am using in my project with ADO.NET Entity Data Model. 我有一个在ADO.NET实体数据模型中使用的mdf数据库文件。

It is possible in Server Explorer to get a script file of the database by pressing: 在服务器资源管理器中,可以通过按以下命令获取数据库的脚本文件:

Data Connections => Tabels => [My table Name]Images =>Show Table Data => Script to file 数据连接=>表格=> [我的表名称]图像=>显示表数据=>脚本文件

This will get a script that will insert data into the database. 这将获得一个脚本,该脚本会将数据插入数据库。 What I what to achieve is a script (That I can add to git) that will create the database and insert all data. 我要实现的是一个脚本(可以将其添加到git中),该脚本将创建数据库并插入所有数据。

Question (Just to be clear): How do I in code generate the script that built my database and inserts data? 问题 (仅需说明):我如何在代码中生成用于构建数据库并插入数据的脚本? Example : 范例

GetTheScriptFromMDFDataBase() =>

“CREATE TABLE [dbo].[Image] (
    [Id]            INT            IDENTITY (1, 1) NOT NULL,
    [ImageFileName] NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

SET IDENTITY_INSERT [dbo].[Image] ON
INSERT INTO [dbo].[Image] ([Id], [ImageFileName]) VALUES (6337, N'L:\Database of fundus images\Not fundus images\epicam not fundus\2017-03-02_10-42-40_video_000000405.jpeg')
INSERT INTO [dbo].[Image] ([Id], [ImageFileName]) VALUES (6338, N'L:\Database of fundus images\Not fundus images\epicam not fundus\2017-03-02_10-42-40_video_000000406.jpeg')
INSERT INTO [dbo].[Image] ([Id], [ImageFileName]) VALUES (6339, N'L:\Database of fundus images\Not fundus images\epicam not fundus\2017-03-02_10-42-40_video_000000407.jpeg')
INSERT INTO [dbo].[Image] ([Id], [ImageFileName]) VALUES (6340, N'L:\Database of fundus images\Not fundus images\epicam not fundus\2017-03-02_10-42-40_video_000000408.jpeg')
INSERT INTO [dbo].[Image] ([Id], [ImageFileName]) VALUES (6341, N'L:\Database of fundus images\Not fundus images\epicam not fundus\2017-03-02_10-42-40_video_000000409.jpeg')
SET IDENTITY_INSERT [dbo].[Image] OFF

I have been looking at SQL Server Management Objects (SMO) But not being able to get it to work. 我一直在查看SQL Server管理对象(SMO),但无法使其正常工作。

You can get the script into your code as under: 您可以将脚本放入代码中,如下所示:

  1. Save the entire script as a text file eg DbScript.txt 将整个脚本另存为文本文件,例如DbScript.txt
  2. Add 'DbScript.txt' as embedded resource. 添加“ DbScript.txt”作为嵌入式资源。 Click Project Menu > YourProject Properties > Resources > Add Resource > Add Existing File (then browse and select DbScript.txt file). 单击项目菜单> YourProject属性>资源>添加资源>添加现有文件(然后浏览并选择DbScript.txt文件)。
  3. In your code write 在你的代码中写

     string s = YourProject.Properties.Resources.DbScript; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM