【问题标题】:Run one SqlCommand for two queries perfomance运行一个 Sql 命令以获得两个查询性能
【发布时间】:2018-09-14 14:08:11
【问题描述】:

到目前为止,我正在创建两个 sql 命令来运行两个不同的查询。我想知道如果我将运行相同的 sql 命令来执行这两个查询,性能是否会改变

这是我到目前为止的方法

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command1 = new SqlCommand(commandText1, connection))
{
}
using (SqlCommand command2 = new SqlCommand(commandText2, connection))
{
}
// etc
}

方法二

var command = new SqlCommand("<SQL Command>", myConnection);
    command.ExecuteNonQuery();


command.CommandText = "<New SQL Command>";
   command.ExecuteNonQuery();

性能有什么不同,或者我用什么都没关系。

【问题讨论】:

标签: c#


【解决方案1】:

性能差异可以忽略不计,但如果您永远并且总是想要运行这两个确切的查询,方法 2 可以节省打开与数据库的新连接。但是您需要在设置新的command.CommandText 属性之前调用command.Parameters.Clear();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多