我正在使用 SMO 执行批处理 SQL 脚本。在 Management Studio 中,脚本的执行时间约为 2 秒。使用以下代码,大约需要 15 秒。
var connectionString = GetConnectionString();
// need to use master because the DB in the connection string no longer exists
// because we dropped it already
var builder = new SqlConnectionStringBuilder(connectionString)
{
InitialCatalog = "master"
};
using (var sqlConnection = new SqlConnection(builder.ToString()))
{
var serverConnection = new ServerConnection(sqlConnection);
var server = new Server(serverConnection);
// hangs here for about 12 -15 seconds
server.ConnectionContext.ExecuteNonQuery(sql);
}
该脚本创建一个新数据库并在几个表中插入几千行。生成的数据库大小约为 5MB。
有人对此有任何经验,或者对为什么 SMO 运行如此缓慢有任何建议吗?
最佳答案
SMO 在后台做了很多奇怪的..事情,这是你为以面向对象的方式处理服务器/数据库对象的能力付出的代价。
既然您没有使用 SMO 的 OO 功能,为什么不完全忽略 SMO 而只是通过普通 ADO 运行脚本呢?
关于c# - SQL SMO 执行批处理 TSQL 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4136834/