I am using SQL Server 2008 for implement databases for C# applications (My C# program was developed by using WPF). I want to create a database at installation time. I heard about that we can write database implementation inside a DLL file and use it in installation time. It is possible ? and how can I do it ? Hope you guys can help me to get some solution.
-
What, exactly, might you mean? You can script the database DDL and hard-code it in your DLL, and just have part of your create script check if it exists already..Or am I missing something vital here? – Nonym Dec 17 '11 at 14:26
-
This is a little old but I think it addresses the question. http://msdn.microsoft.com/en-us/library/aa496043(v=SQL.80).aspx Your question is vague and shows little research on your part. The installer package allows you to cusomize an installation. – paparazzo Dec 17 '11 at 14:42
3 Answers
You can create a database project which will allow you to deploy a database to target server, or, if you are using entity framework you can use Database.SetInitializer to create the database

- 15,915
- 3
- 48
- 72
-
I fund some information and steps from this link http://msmvps.com/blogs/deborahk/archive/2010/05/02/vs-2010-database-project-an-introduction.aspx Thank u for telling about Database Projects to me. I hadn't heard about before. – Shashika Dec 17 '11 at 19:23
-
I have created dbschema script and now i want use this script in installation time to create database. how can I do it ? Hope you can help me to get some introduction. – Shashika Dec 29 '11 at 15:14
-
what installer are you using? regardless, you will need to connect to the database server and run the script – Jason Dec 29 '11 at 16:22
-
First of all Thank you for your response. I don't have much knowledge about installers. but i used Visual Studio Installer project to create installer. i want to add this script to that type of installer. can i do it ? – Shashika Dec 29 '11 at 17:23
-
i'm sure you can, but i don't know how. type "visual studio 2010 setup project" into google, and go from there. good luck. – Jason Dec 30 '11 at 02:11
I think if you write a SQL commands that make a new Data Base and execute it at installation time, if your DB account has permission to create a DB, you can do this.

- 4,350
- 12
- 56
- 91
-
1"Write a text"? That sounds just like mailing a letter to Windows Installer. – NGLN Dec 17 '11 at 15:29
You have to export your sql database to script, and then follow the instructions to create a custom action during your setup project:
here's what you have to do:
add a class library to your solution then add an Installer class to it and finally write this function in it:
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
//run the sql script here
}
Add a Visual Studio Installer\Setup Project to your solution. In the File System Editor, right click on Application Folder, Add a Project Output. Select your class library which you've just created, select Primary output and press OK.
Now, in Custom Actions editor, add right click on Commit, select add custom action, select application folder, select the primary output that you've just added, press OK.
In the Properties window, make sure that the InstallerClass property is set to True (this is the default).