如何使用 C# 调用 SQL Server Express 命令行来运行 .sql 文件

发布于 2025-01-02 03:23:49 字数 2040 浏览 5 评论 0原文

我必须为窗口应用程序进行设置,因此我需要在客户端计算机上创建一个数据库。因此,我决定使用 .sql 文件创建它,并将其添加到我的资源文件中。因为有一个选项可以在第一次运行我的程序时执行该文件来创建我的数据库。

但正如我的一位朋友所建议的,该 .sql 文件应该从 SQL Server Express 的命令行执行。

我已经搜索了很多如何在 SQL Server 2008 Express 命令行中从 C# 代码执行 .sql 文件。

我已经阅读了这些问题

代码:

FileInfo file = new FileInfo(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),  "HotelReservationScript.sql"));                
StreamReader fileRead = file.OpenText();

string script = fileRead.ReadToEnd();

fileRead.Close();
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["MasterConnectionString"].ConnectionString;

SqlConnection con = new SqlConnection(strConn);
con.Open();

ExecuteScript(con, script);

protected virtual void ExecuteScript(SqlConnection connection, string script)
{
    string[] commandTextArray = System.Text.RegularExpressions.Regex.Split(script, "\r\n[\t ]*GO");

    SqlCommand _cmd = new SqlCommand(String.Empty, connection);

    foreach (string commandText in commandTextArray)
    {
        if (commandText.Trim() == string.Empty) 
            continue;

        if ((commandText.Length >= 3) && (commandText.Substring(0, 3).ToUpper() == "USE"))
        {
            throw new Exception("Create script contains USE statement. Please provide non database specific create scripts!");
        }

        _cmd.CommandText = commandText;
        _cmd.ExecuteNonQuery();
    }
}

错误消息:

找不到文件“C:\Users\Kiran\Desktop\rahhul\Bookster_28\HotelReservationSystem\HotelReservationSystem\bin\Debug\HotelReservationScript.sql”

编辑:

我也尝试过此操作,但收到相同的错误

FileInfo file = new FileInfo(Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs().ToString()), "HotelReservationScript.sql"));

I have to make a setup for window application so I need to create a database on client machine. So I decided that I will create it using a .sql file which I will add in my resource file. As there is a option to execute that file on first run of my program that will create my database.

But as one of my friend suggested, that .sql file should be executed from command line of SQL Server Express.

I have searched a lot for how to execute a .sql file from c# code in SQL Server 2008 Express command line.

I already read these questions

Code:

FileInfo file = new FileInfo(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),  "HotelReservationScript.sql"));                
StreamReader fileRead = file.OpenText();

string script = fileRead.ReadToEnd();

fileRead.Close();
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["MasterConnectionString"].ConnectionString;

SqlConnection con = new SqlConnection(strConn);
con.Open();

ExecuteScript(con, script);

protected virtual void ExecuteScript(SqlConnection connection, string script)
{
    string[] commandTextArray = System.Text.RegularExpressions.Regex.Split(script, "\r\n[\t ]*GO");

    SqlCommand _cmd = new SqlCommand(String.Empty, connection);

    foreach (string commandText in commandTextArray)
    {
        if (commandText.Trim() == string.Empty) 
            continue;

        if ((commandText.Length >= 3) && (commandText.Substring(0, 3).ToUpper() == "USE"))
        {
            throw new Exception("Create script contains USE statement. Please provide non database specific create scripts!");
        }

        _cmd.CommandText = commandText;
        _cmd.ExecuteNonQuery();
    }
}

Error message:

Could not find file 'C:\Users\Kiran\Desktop\rahhul\Bookster_28\HotelReservationSystem\HotelReservationSystem\bin\Debug\HotelReservationScript.sql'

EDITED :

I also tried this but I get the same error

FileInfo file = new FileInfo(Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs().ToString()), "HotelReservationScript.sql"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2025-01-09 03:23:49
FileInfo file = new FileInfo(Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs().ToString()), "HotelReservationScript.sql"));

错误已解决,因为我必须设置该 .sql 文件的属性,复制属性窗口中的每个 timr。

FileInfo file = new FileInfo(Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs().ToString()), "HotelReservationScript.sql"));

error solved as i have to set the properties of that .sql file copy every timr in property window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文