我尝试用Visual的代码在我的SQL Script文件夹中执行bin文件,以前它运行良好,我使用SQL Server 2012时,我最近安装了SQL server 2019,我使用SQL Server 2019创建了脚本,当应用程序试图执行脚本时,我得到了错误
创建/更改过程必须是查询批处理中的第一条语句
我去找了一个有SQL Server 2012的朋友,用他的电脑创建了脚本,但是当我用更新的版本创建脚本时,我得到了错误,这是我读取脚本文件的代码:
public static string ReadDBFile(string path)
{
string str;
StreamReader reader = new StreamReader(path);
str = reader.ReadToEnd();
str = str.Replace("GO", "");
reader.Close();
return str;
}下面是我正在执行脚本文件的地方:
cmd.CommandText = ReadDBFile(Application.StartupPath + "/scriptfile.sql");
cmd.ExecuteNonQuery();我确信这与SQL Server的升级有关,有什么线索可以说明我怎样才能让它起作用吗?
发布于 2020-03-03 13:52:42
读取DB文件,将\r\n替换为\n,在字符串"\nGO\n"上拆分*并循环结果数组,逐个执行其中的语句
更像是:
public static string ExecBatchSql(string path)
{
string f = File.ReadAllText(path).Replace("\r\n", "\n");
string fs = f.Split(new[]{"\nGO\n"}, StringSplitOptions.RemoveEmptyEntries);
foreach(string cmd in fs){
SqlCommand c = new SqlCommand(cmd, connStr);
c.ExecuteNonQuery();
}
}https://stackoverflow.com/questions/60508511
复制相似问题