如何在运行时程序中使用脚本文件创建数据库

如何在运行时程序中使用脚本文件创建数据库

问题描述:

如何在运行时程序中使用脚本文件创建数据库

在客户端系统中安装后,不需要在SQL Server中附加数据库...

我的数据库有:

10表,79程序和5view ...我想用C#创建(当主表单运行时)

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)

连接到你的SQL服务器以通常的方式,使用具有创建数据库权限的用户标识/密码组合。

然后将数据库脚本作为命令字符串运行,就像它是一个普通的ExecuteNonQuery命令一样(它是)。

您可以使用SSMS从现有数据库中获取脚本,右键单击数据库名称,然后从上下文菜单中选择脚本数据库为和创建为。

将脚本保存为文件,并将其加载到您的应用程序中。
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.


这就是OriginalGriff解决方案的代码。几个月前我写这篇文章基本上是为了做你正在做的事情。它旨在处理具有 GO 语句的脚本。



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.

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);
                }

            }
        }
    }
}