如何使用 C# 调用 SQL Server Express 命令行来运行 .sql 文件
我必须为窗口应用程序进行设置,因此我需要在客户端计算机上创建一个数据库。因此,我决定使用 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误已解决,因为我必须设置该 .sql 文件的属性,复制属性窗口中的每个 timr。
error solved as i have to set the properties of that .sql file copy every timr in property window.