简体   繁体   English

从.NET应用程序执行SQL Server脚本的最佳实践?

[英]Best practices for executing SQL Server Scripts from a .NET Application?

What is the recommended method for executing a SQL Server script from within a .NET application and why? 从.NET应用程序中执行SQL Server脚本的推荐方法是什么?为什么?

I'm in process of creating an application we can run from our installer to handle upgrading our database when a prior version of our application is installed. 我正在创建一个应用程序,我们可以从我们的安装程序运行,以便在安装我们的应用程序的早期版本时处理数据库的升级。 The database has a version table that I can programatically access and determine which upgrade scripts to run to get the database upgraded. 数据库有一个版本表,我可以以编程方式访问并确定要运行哪些升级脚本以升级数据库。 It makes sense for me to embed this in an application that our installer can just call to do the upgrade. 我将它嵌入到我们的安装程序可以调用以进行升级的应用程序中是有意义的。

I am looking for how best to execute the appropriate upgrade scripts. 我正在寻找如何最好地执行适当的升级脚本。 Searching the web I've seen recommendations for splitting a script by "GO" statements, and using SqlCommand.ExecuteNonQuery() . 在网上搜索我已经看到了通过“GO”语句拆分脚本并使用SqlCommand.ExecuteNonQuery()的建议 I've also seen recommendations for using SQL Server Management Objects (SMO) . 我也看到了使用SQL Server管理对象(SMO)的建议

I'm looking for advice on the pros/cons of the various ways to execute these scripts from within a .NET application. 我正在寻找有关从.NET应用程序中执行这些脚本的各种方法的优缺点的建议。

Although it may not specifically meet your needs to be run from within a .NET application, sqlcmd is well-suited (and designed) for this process. 虽然它可能无法满足您在.NET应用程序中运行的需求,但sqlcmd非常适合(和设计)用于此过程。 By implementing the ability parse & run a SQL Script, you are duplicating the tool functionality. 通过实现解析和运行SQL脚本的能力,您将复制工具功能。 sqlcmd is available in a standalone installer that could be bundled with your application. sqlcmd在独立安装程序中提供 ,可以与您的应用程序捆绑在一起。

If you choose to use the linked SqlCommand.ExecuteNonQuery() solution, it will be more memory efficient for large scripts to read the script into a smaller buffer searching for the "GO" statement, "instead of all-at-once". 如果您选择使用链接的SqlCommand.ExecuteNonQuery()解决方案,那么对于大型脚本来说,将脚本读入较小的缓冲区来搜索“GO”语句“而不是一次性”将更有效。

I have had consistent results using a System.Diagnostics.Process and calling into OSQL.exe. 我使用System.Diagnostics.Process并调用OSQL.exe获得了一致的结果。 its made for a sql file.... 它为sql文件....

I created a temporary file where i wrote my embedded sql file, had OSQL execute it, then cleaned it up. 我创建了一个临时文件,我写了我的嵌入式sql文件,OSQL执行它,然后清理它。

    process.StartInfo = new System.Diagnostics.ProcessStartInfo();
    process.StartInfo.FileName = OSQLpath;
    process.StartInfo.UseShellExecute = false;
    ...
    if (!System.IO.Directory.Exists(workingDirectory))
                        System.IO.Directory.CreateDirectory(workingDirectory);
    ...
    StringBuilder sbArgs = new StringBuilder();

    sbArgs.Append("-S ").Append(_serverName);
    sbArgs.Append(" -d ").Append(_databaseName);
    sbArgs.Append(" -E");
    sbArgs.Append(" -i ").Append("\"").Append(inputSQLFilePath).Append("\""); // input file
    sbArgs.Append(" -o ").Append("\"").Append(System.IO.Path.Combine(workingDirectory, String.Format("osqloutput_{1}_{0}.txt", fileUnique, fileName))).Append("\""); // output file

    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;

    process.StartInfo.Arguments = sbArgs.ToString();
    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process.StartInfo.CreateNoWindow = true;

...

    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
    process.Start();
    process.WaitForExit();

The best method would be any method that meets all functional and nonfunctional restraints. 最好的方法是满足所有功能和非功能限制的任何方法。

What kind of speed do you need, is there any problem with the current situation What is your backup/failure strategy 您需要什么样的速度,当前情况是否有任何问题您的备份/故障策略是什么?

I would probably use the SqlCommand.ExecuteNonQuery() because its a part of the .net runtime that needs no extra installation of applications the user might not need. 我可能会使用SqlCommand.ExecuteNonQuery(),因为它是.net运行时的一部分,不需要额外安装用户可能不需要的应用程序。

From your question I presume there is an existing application with lots of embedded query string. 根据您的问题,我假设存在一个包含大量嵌入式查询字符串的现有应用程序。 I would try to get the into a repository of some kind that automatically places them on the server (as a stored procedure) and inside your code. 我会尝试进入一种自动将它们放在服务器上(作为存储过程)和代码内部的存储库。

Don't be afraid of simple text files or other config files with query's. 不要害怕简单的文本文件或带有查询的其他配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM