SQL SMO要执行批处理脚本TSQL批处理、脚本、SQL、SMO

2023-09-03 13:34:12 作者:无.

我使用SMO执行一批SQL脚本。在Management Studio中的脚本约2秒钟执行。用下面的code,大约需要15秒。

I'm using SMO to execute a batch SQL script. In Management Studio, the script executes in about 2 seconds. With the following code, it takes about 15 seconds.

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。

The script creates a new database and inserts a few thousand rows across a few tables. The resulting DB size is about 5MB.

任何人有任何这方面的经验或对为什么会如此缓慢,SMO?

Anyone have any experience with this or have a suggestion on why this might be running so slowly with SMO?

推荐答案

SMO做了很多奇怪的东西......在后台,这是你所支付的治疗服务器/数据库对象在一个面向对象的方式能力的价格。 既然你不使用SMO的OO capabilites,你为什么不只是忽略SMO完全,只是通过正常的ADO运行脚本?

SMO does lots of weird .. stuff in the background, which is a price you pay for ability to treat server/database objects in an object-oriented way. Since you're not using the OO capabilites of SMO, why don't you just ignore SMO completely and simply run the script through normal ADO?