C# 从ASP.Net C运行SQL脚本#应用程序性能问题
我正在从C#代码运行SQL脚本,我的问题是代码需要很长时间才能完成执行,它有许多表需要在脚本中创建(至少30个表),所以我的代码可以工作,但它有性能问题。 这是密码C# 从ASP.Net C运行SQL脚本#应用程序性能问题,c#,sql,asp.net,C#,Sql,Asp.net,我正在从C#代码运行SQL脚本,我的问题是代码需要很长时间才能完成执行,它有许多表需要在脚本中创建(至少30个表),所以我的代码可以工作,但它有性能问题。 这是密码 public static bool executeSqlScript(string scriptPath,string serverName,string databaseName) { try { SqlConnection myConn = new SqlCon
public static bool executeSqlScript(string scriptPath,string serverName,string databaseName)
{
try
{
SqlConnection myConn = new SqlConnection("Server=.;Integrated security=SSPI;database=master");
Server server = new Server(new ServerConnection(myConn));
string CreateCommand = "CREATE DATABASE " + databaseName + "";
string appendText;
//delete first line from script(Oldscript)
var lines = File.ReadAllLines(scriptPath).Skip(1);
File.WriteAllLines(scriptPath, lines);
using (StreamReader sreader = new StreamReader(scriptPath))
{
appendText = sreader.ReadToEnd();
}
File.Delete(scriptPath);
using (StreamWriter swriter = new StreamWriter(scriptPath, false))
{
appendText = "USE [" + databaseName + "]" + Environment.NewLine + appendText;
swriter.Write(appendText);
swriter.Close();
}
string readtext = File.ReadAllText(scriptPath);
SqlCommand myCommand = new SqlCommand(CreateCommand, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
server.ConnectionContext.ExecuteNonQuery(readtext);
return true;
}
catch (Exception e)
{
throw e;
return false;
}
}
我的建议是将其中的大部分迁移到SQL Server,一旦设置了新的DB和用户,调用存储过程来读取包含必要DDL的SQL文件
我的上一个项目是为一家托管公司设计的,我们的CMS使用了150多个数据库对象。我们使用了一个“控制”数据库,我们传递新的数据库信息,它将在不到一分钟的时间内完成新的表、函数和过程 使用Parallel.ForEach创建表,如
public static bool executeSqlScript(string scriptPath, string serverName, string databaseName)
{
try
{
SqlConnection myConn = new SqlConnection("Server=.;Integrated security=SSPI;database=master");
//Server server = new Server(new ServerConnection(myConn));
string CreateCommand = "CREATE DATABASE " + databaseName + "";
string appendText;
//delete first line from script(Oldscript)
//create db first
var myCommand = new SqlCommand(CreateCommand, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
List<string[]> list = File.ReadLines(scriptPath)
.Select(line => line.ToLower().Split(new string[] { "go" }, StringSplitOptions.None))
.ToList();
Parallel.ForEach(list, (sql) =>
{
using (var mysqlConn = new SqlConnection("Server=.;Integrated security=SSPI;database=master"))
{
var mysql = "USE [" + databaseName + "]" + Environment.NewLine + string.Join("", sql);
var mysqlCommand = new SqlCommand(mysql, mysqlConn);
myConn.Open();
mysqlCommand.ExecuteNonQuery();
}
});
//server.ConnectionContext.ExecuteNonQuery(readtext);
return true;
}
catch (Exception e)
{
throw e;
return false;
}
}
publicstaticboolexecutesqlscript(stringscriptpath、stringservername、stringdatabasename)
{
尝试
{
SqlConnection myConn=newsqlconnection(“服务器=;集成安全=SSPI;数据库=主机”);
//服务器服务器=新服务器(新服务器连接(myConn));
string CreateCommand=“创建数据库”+数据库名称+”;
字符串追加文本;
//从脚本中删除第一行(旧脚本)
//先创建数据库
var myCommand=new-SqlCommand(CreateCommand,myConn);
myConn.Open();
myCommand.ExecuteOnQuery();
myConn.Close();
List List=File.ReadLines(脚本路径)
.Select(line=>line.ToLower().Split(新字符串[]{“go”},StringSplitOptions.None))
.ToList();
Parallel.ForEach(list,(sql)=>
{
使用(var mysqlConn=newsqlconnection(“Server=;integratedsecurity=SSPI;database=master”))
{
var mysql=“USE[“+databaseName+”]”+Environment.NewLine+string.Join(“,sql”);
var mysqlCommand=newsqlcommand(mysql,mysqlConn);
myConn.Open();
mysqlCommand.ExecuteNonQuery();
}
});
//server.ConnectionContext.ExecuteNonQuery(readtext);
返回true;
}
捕获(例外e)
{
投掷e;
返回false;
}
}
问题在于脚本中的Go关键字,我找到了这个解决方案
(如果您试图执行SQL生成的脚本文件,则必须删除所有“GO”。为此,您必须使用以下代码…)
这是我的密码:
string sqlConnectionString = "Data Source=.;Initial Catalog=master;Integrated Security=True";
FileInfo file = new FileInfo(@"D:\Script.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
conn.Open();
script = script.Replace("GO", "");
SqlCommand cmd = new SqlCommand(script, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
请将代码粘贴到您的问题正文中。您需要多久创建一次这些表?是不是可以用并行的方式而不是串行的?串行是什么意思?意思是你在创建一个,然后创建另一个,然后创建另一个。我还看到您正在创建一个数据库,这可能是导致其速度减慢的原因。好的,您能提供此方法的代码吗?您能给我一个此方法的示例吗?不能提供详细信息,但有一个论坛讨论可以指导您