如何从C#执行.sql?

pup*_*eno 15 c# sql sql-server sql-server-2008

对于某些集成测试,我想连接到数据库并运行.sql文件,该文件具有实际运行测试所需的模式,包括GO语句.我怎么能执行.sql文件?(或者这完全是错误的方式?)

在MSDN论坛上发现了一条显示此代码的帖子:

using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
            FileInfo file = new FileInfo("C:\\myscript.sql");
            string script = file.OpenText().ReadToEnd();
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            Server server = new Server(new ServerConnection(conn));
            server.ConnectionContext.ExecuteNonQuery(script);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但在最后一行我收到此错误:

System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.TypeInitializationException:''抛出异常的类型初始值设定项.---> .ModuleLoadException:在appdomain初始化期间无法加载C++模块.---> System.DllNotFoundException:无法加载DLL"MSVCR80.dll":找不到指定的模块.(来自HRESULT的异常:0x8007007E).

我被告知要从某个地方下载该DLL,但这听起来非常h​​acky.有更清洁的方式吗?还有另一种方法吗?我究竟做错了什么?

我正在使用Visual Studio 2008,SQL Server 2008,.Net 3.5SP1和C#3.0.

Mat*_*ell 29

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
Run Code Online (Sandbox Code Playgroud)

您不应该需要SMO来执行查询.请尝试使用SqlCommand对象.删除这些using语句.使用此代码执行查询:

 SqlConnection conn = new SqlConnection(sqlConnectionString);
 SqlCommand cmd = new SqlCommand(script, conn);
 cmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

另外,删除对SMO的项目引用.注意:您需要正确清理资源.

更新:

ADO.NET库不支持"GO"关键字.看起来您的选择是:

  1. 解析脚本.删除"GO"关键字并将脚本拆分为不同的批次.将每个批处理作为自己的SqlCommand执行.
  2. 将脚本发送到shell中的SQLCMD(David Andres的回答).
  3. 像博客文章中的代码一样使用SMO.

实际上,在这种情况下,我认为SMO可能是最好的选择,但你需要找出为什么没有找到dll.


Dav*_*res 9

MSVCR80是Visual C++ 2005运行时.您可能需要安装运行时包.有关详细信息,请参阅http://www.microsoft.com/downloads/details.aspx?FamilyID=200b2fd9-ae1a-4a14-984d-389c36f85647&displaylang=en.

除了解决DLL问题和Matt Brunell的答案(我认为它更适合您正在尝试做的事情)之外,您还可以使用SQLCMD命令行工具(来自SQL客户端工具安装)来执行这些SQL脚本.请确保它在您的路径上,这样您就不会对路径位置感到困惑.

这将是这样的:

实际命令:

SQLCMD -S myServer -D myDatabase -U myUser -P myPassword -i myfile.sql
Run Code Online (Sandbox Code Playgroud)

参数(案例事项):

S: server
d: database
U: User name, only necessary if you don't want to use Windows authentication
P: Password, only necessary if you don't want to use Windows authentication
i: File to run
Run Code Online (Sandbox Code Playgroud)

执行SQL文件的代码:

var startInfo = new ProcessStartInfo();
startInfo.FileName = "SQLCMD.EXE";
startInfo.Arguments = String.Format("-S {0} -d {1}, -U {2} -P {3} -i {4}",
                                    server,
                                    database,
                                    user,
                                    password,
                                    file);
Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud)

有关SQLCMD工具的更多信息,请参见http://msdn.microsoft.com/en-us/library/ms162773.aspx.