Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How create database with script file in runtime program
After installation in client system , does not need to Attach database in SQL Server...
my database have :
10 table , 79 procedure and 5view ... i want create in C# (when main form run)
Posted
Comments
Richard MacCutchan 16-May-15 9:30am    
And what is the problem? Creating a database in C# is a fairly straightforward process. And if you are using an installer then you can package a precreated database in with the application.

Connect to your SQL Server in the usual way, using a user id/password combo that has permission to create databases.
Then run your DB script as a command string, exactly as if it was a normal ExecuteNonQuery command (which it is).
You can get the script from an existing DB using SSMS, by right clicking the database name and selecting "Script Database As" and "CREATE TO" from the context menu.
Save the script as a file, and load it in your app.
 
Share this answer
 
This is what the code would look like to OriginalGriff's solution. I wrote this a couple months ago to do basically what you are doing. It is designed to handle scripts that have the GO statement.

C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlCommandExecutor
{
    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectionString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=localhost";

            Console.WriteLine("Loading scripts...");
            StreamReader createDatabaseScriptStreamReader = File.OpenText("CreateDatabase.sql");
            StreamReader loadDataScriptStreamReader = File.OpenText("LoadDatabase.sql");

            using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
            {
                sqlConnection.Open();

                ExecuteScript(createDatabaseScriptStreamReader, sqlConnection);
                ExecuteScript(loadDataScriptStreamReader, sqlConnection);

                sqlConnection.Close();
            }

            Console.WriteLine("Finished.");
        }

        private static void ExecuteScript(StreamReader createDatabaseScriptStreamReader, SqlConnection sqlConnection)
        {
            StringBuilder stringBuilder = new StringBuilder();

            while (!createDatabaseScriptStreamReader.EndOfStream)
            {
                string line = createDatabaseScriptStreamReader.ReadLine();

                if (line == "GO")
                {
                    try
                    {
                        string command = stringBuilder.ToString();
                        string message;

                        if (command.Length > 15)
                            message = command.Substring(0, 15);
                        else
                            message = command;

                        message = message.Trim();

                        Console.WriteLine("Executing command \"" + message + "...\"");

                        SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);
                        sqlCommand.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine();
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine(ex.Message);
                        Console.ResetColor();
                        Console.WriteLine();
                    }

                    stringBuilder = new StringBuilder();
                }
                else
                {
                    stringBuilder.AppendLine(line);
                }

            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900