Sql server 2008 r2 如何在C#中使用sqlcmd运行SQL Server 2008 R2数据库脚本?

Sql server 2008 r2 如何在C#中使用sqlcmd运行SQL Server 2008 R2数据库脚本?,sql-server-2008-r2,sqlcmd,database-scripts,Sql Server 2008 R2,Sqlcmd,Database Scripts,我是在C#中使用sqlcmd运行SQL脚本的新手。我在网上看到了一些代码,但我不明白它是如何工作的 string path = string.Empty; OpenFileDialog opd = new OpenFileDialog(); opd.Filter = "sql files|*.sql"; if (opd.ShowDialog() == DialogResult.OK) {

我是在C#中使用sqlcmd运行SQL脚本的新手。我在网上看到了一些代码,但我不明白它是如何工作的

string path = string.Empty;
            OpenFileDialog opd = new OpenFileDialog();
            opd.Filter = "sql files|*.sql";
            if (opd.ShowDialog() == DialogResult.OK)
            {
                path = opd.FileName;//Here i am taking Database sqlScript
            }
        string tmpFile = Path.GetTempFileName();
            SqlConnectionStringBuilder connection=new SqlConnectionStringBuilder(@"Data Source=LPTP2\LPTP2;Initial Catalog=Database;Integrated Security=True");
        string argument = string.Format(@" -S {0} -d {1} -i ""{2}"" -o ""{3}""",
            @".\SQLEXPRESS", "Database", path, tmpFile);

        // append user/password if not use integrated security
        if (!connection.IntegratedSecurity)
            argument += string.Format(" -U {0} -P {1}", "sa", "abc@123");

        var process = Process.Start("sqlcmd.exe", argument);
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.Start();
        while (true)
        {
            // wait for the process exits. The WaitForExit() method doesn't work
            if (process.HasExited)
                break;
            Thread.Sleep(500);
        }
我不明白这三条线是如何运作的

 string tmpFile = Path.GetTempFileName();
            SqlConnectionStringBuilder connection=new SqlConnectionStringBuilder(@"Data Source=LPTP2\LPTP2;Initial Catalog=HemoTrace;Integrated Security=True");
        string argument = string.Format(@" -S {0} -d {1} -i ""{2}"" -o ""{3}""",
            @".\SQLEXPRESS", "HemoTrace", path, tmpFile);

        // append user/password if not use integrated security
        if (!connection.IntegratedSecurity)
            argument += string.Format(" -U {0} -P {1}", "sa", "abc@123");
我这样做的原因是我想运行一个SQL脚本,该脚本用于创建数据库。但是我想使用sqlcmd。在客户端,如果我执行.exe文件,它将完成我的工作(将数据库连接到服务器)

请帮我解决这个问题

string tmpFile = Path.GetTempFileName();
声明名为tmpFile的字符串变量,并使用
Path.GetTempFileName()
生成唯一的临时文件名并将其存储在变量中

SqlConnectionStringBuilder connection=new 
SqlConnectionStringBuilder(@"Data Source=LPTP2\LPTP2;Initial Catalog=HemoTrace;
Integrated Security=True");
使用
SqlConnectionStringBuilder
类构建SQL Server连接字符串。这实际上并没有连接到任何东西,它只是生成一个连接字符串

string argument = string.Format(@" -S {0} -d {1} -i ""{2}"" -o ""{3}""",
@".\SQLEXPRESS", "HemoTrace", path, tmpFile);
声明一个名为
argument
的字符串,并将其设置为一组字符,包括先前生成的临时文件的路径。这组字符适合用作SQLCMD命令行的参数

// append user/password if not use integrated security
if (!connection.IntegratedSecurity)
argument += string.Format(" -U {0} -P {1}", "sa", "abc@123");
使用
SqlConnectionStringBuilder
类的属性来确定是否应该添加命令行开关以指示受信任的安全性

完成所有这些操作后,您可以运行:

var process = Process.Start("sqlcmd.exe", argument);
如果您转储这个填充字符串,您将发现可以在命令行上运行的东西

// append user/password if not use integrated security
if (!connection.IntegratedSecurity)
argument += string.Format(" -U {0} -P {1}", "sa", "abc@123");
SQLCMD是一个命令行程序,顺便说一句,它需要安装在您的客户机上

命令行程序接受您在代码的前几行中构建的一组参数

此代码中存在一些问题:

  • 您需要安装SQLCMD.EXE才能使其工作
  • 您将可信安全性硬编码为字符串,将其加载到一个特殊类中,然后使用该类确定您是否使用可信安全性。。。你已经硬编码了
  • 您还可以在连接字符串中硬编码某个服务器,然后在SQLCMD的参数中硬编码另一个服务器
  • 在这种情况下,连接字符串(中间线)似乎是完全冗余的

但我想知道以上结果,我没有得到任何结果结果结果是什么?在变量中?在数据库里?我建议您设置一个断点并转储
参数
变量。现在进入命令提示符(Start/Run/CMD)并键入
SQLCMD.EXE
+参数值(即程序正在执行的操作),您可能会获得更多信息。您应该对SQLCMD做更多的研究。请注意,您的输出会转到一个临时文件,这不是很有帮助-为什么不将输出写入一个您可以在以后找到的文件中?如果我在命令提示符下执行了上述操作,我将超时,我希望结果显示在数据库中,请发布确切的错误。根据这些参数,您需要安装一个本地SQL Server,实例名为SQLEXPRESS。您是否能够测试与本地安装的SQLEXPRESS的连接(使用ODBC、UDL、SQL Server Management Studio?),因为它正试图连接到该连接。请注意,您需要改进代码以发现这些错误并报告它们。