0

I have a mdf database file that I am using in my project with ADO.NET Entity Data Model.

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.

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.

Jake_2
  • 78
  • 15

1 Answers1

0

You can get the script into your code as under:

  1. Save the entire script as a text file e.g. DbScript.txt
  2. Add 'DbScript.txt' as embedded resource. Click Project Menu > YourProject Properties > Resources > Add Resource > Add Existing File (then browse and select DbScript.txt file).
  3. In your code write

    string s = YourProject.Properties.Resources.DbScript;
    
Rupesh
  • 242
  • 2
  • 6