Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何通过ADO.NET运行.sql脚本文件?_Asp.net_Sql_Ado.net - Fatal编程技术网

Asp.net 如何通过ADO.NET运行.sql脚本文件?

Asp.net 如何通过ADO.NET运行.sql脚本文件?,asp.net,sql,ado.net,Asp.net,Sql,Ado.net,我想通过ADO.NET使用ASP.NET网站运行.sql脚本文件。它怎么会不起作用呢 当我尝试 'dbScript is a string and contains contents of the .sql file' Dim cmd As New SqlCommand(dbScript, con) Try con.Open() cmd.ExecuteNonQuery() Catch ex As Exception Finally con.Close() cmd.

我想通过ADO.NET使用ASP.NET网站运行.sql脚本文件。它怎么会不起作用呢

当我尝试

'dbScript is a string and contains contents of the .sql file'
Dim cmd As New SqlCommand(dbScript, con)
Try
    con.Open()
    cmd.ExecuteNonQuery()
Catch ex As Exception
Finally
    con.Close()
    cmd.Dispose()
End Try

在脚本中执行GO语句时会出现异常。如何解决此问题?

GO不是Transact-SQL语句,而是工具批处理分隔符。当批处理中遇到GO时,服务器有理由抱怨语法错误。您需要将文件拆分为多个批,然后执行各个批。使用正则表达式拆分文件inot批处理,并在一行上识别GO不区分大小写。

这是因为GO实际上不是本机TSQL语句,它在Management Studio/Enterprise Manager中用于将脚本拆分为批处理

您需要:
1) 在每个GO语句中将其拆分为多个单独的脚本
2) 在SQL管理对象中使用Server类,例如参见我的博客文章关于。诀窍是使用方法。例如,以下代码将运行目录中的所有脚本,而不考虑GO分隔符:

    using System;
    using System.IO;
    using System.Data.SqlClient;
    using System.Collections.Generic;

    //Microsoft.SqlServer.Smo.dll
    using Microsoft.SqlServer.Management.Smo;
    //Microsoft.SqlServer.ConnectionInfo.dll
    using Microsoft.SqlServer.Management.Common;

    public class RunAllSqlSriptsInDirectory
    {
        public static void Main()
        {
            string scriptDirectory = "c:\\temp\\sqltest\\";
            string sqlConnectionString = "Integrated Security=SSPI;" + 
                "Persist Security Info=True;Initial Catalog=Northwind;Data Source=(local)";
            DirectoryInfo di = new DirectoryInfo(scriptDirectory);
            FileInfo[] rgFiles = di.GetFiles("*.sql");
            foreach (FileInfo fi in rgFiles)
            {
                FileInfo fileInfo = new FileInfo(fi.FullName);
                string script = fileInfo.OpenText().ReadToEnd();
                SqlConnection connection = new SqlConnection(sqlConnectionString);
                Server server = new Server(new ServerConnection(connection));
                server.ConnectionContext.ExecuteNonQuery(script);
            }
        }
    }

使用拆分方法执行批处理有一个小问题。问题在于评论。假设您对文件内容没有权限。你只需要执行它。在这里的每个解决方案示例中,使用“GO”作为分隔符拆分sql代码时,多行注释中的GO都是问题所在。例如:

[some sql code]
GO

/* start of commented out sql code ***********
[some sql code]
GO
end of commented out sql code ****************/

[some sql code]
GO
这将需要比拆分更复杂的解析。这将不再有效:

Regex regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string[] lines = regex.Split(sql);

这段代码还忽略了空格可能导致的错误。

您必须进行两次传递分析。第一步是删除所有注释并构建一个新字符串。第二步是基于GO关键字使用正则表达式拆分

请注意,依赖SMO将需要应用程序预安装SMO可再发行版本,这带来了一点小小的不便。但真正的交易杀手是SMO是版本特定的,并且会断然拒绝连接到更高版本的SQL:使用SQL 2k5中的SMO开发的应用程序不会连接到SQL Server 2k8,这要求开发人员发布使用SMO 2k8的新版本的应用程序。Remus,好的方面。当你在自己的网站上运行脚本时,这些都不是什么大不了的事。穆罕默德在问。谢谢你的帮助。希望尽快找到通用的、独立于平台的解决方案。如果你愿意,请在这里分享。再次感谢您应该将SqlConnection声明包装在in using()中。我发现使用此技术会导致SMO库中的一些非托管代码被加载。如果您使用的是.Net 4.0,您将遇到以下问题:“混合模式程序集是根据运行时的版本“v2.0.50727”构建的,如果没有其他配置信息,则无法在4.0运行时中加载。”请参阅: