【问题标题】:How to run a SQL Server 2008 R2 Database Script using sqlcmd in C#?如何在 C# 中使用 sqlcmd 运行 SQL Server 2008 R2 数据库脚本?
【发布时间】:2014-03-10 04:53:12
【问题描述】:

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

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 SCRIPT 脚本来创建一个数据库。但我想使用 sqlcmd。在客户端,如果我执行我的 .exe 文件,它会完成我的工作(将数据库附加到服务器)。

请帮我解决这个问题。

【问题讨论】:

    标签: sql-server-2008-r2 sqlcmd database-scripts


    【解决方案1】:
    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);
    

    如果你转储这个填充字符串,你会发现可以在命令行上运行的东西。

    SQLCMD 是一个命令行程序,顺便需要在您的客户端机器上安装它。

    命令行程序采用您在前几行代码中构建的一堆参数。

    这段代码有一些问题:

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

    【讨论】:

    • 但是我想知道上面的结果我没有得到任何结果
    • 结果是什么?在变量中?在数据库中?我建议你做一个断点并转储argument 变量。现在进入命令提示符(开始/运行/CMD)并输入SQLCMD.EXE + 参数的值(即您的程序正在做什么),您可能会获得更多信息。你应该对 SQLCMD 做更多的研究。请注意,您的输出会发送到一个不太有用的临时文件 - 为什么不将输出写入以后可以找到的文件?
    • 如果我在命令提示符下完成上述操作,我会超时
    • 我想要数据库中的结果
    • 请发布确切的错误。根据这些参数,您需要安装一个本地 SQL Server,并且实例名称为 SQLEXPRESS。您是否能够测试与本地安装的 SQLEXPRESS 的连接(使用 ODBC、UDL、SQL Server Management Studio?),因为这是它试图连接的内容。请注意,您需要改进代码以发现这些错误并报告它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    相关资源
    最近更新 更多