0

我有一个方法,它直接在 .CS 文件中包含一个非常大的 sql 查询。推荐的重构方法是什么?

4

3 回答 3

2

您可以将大型复杂 SQL 查询放入 SQL 视图或存储过程中,然后在代码中使用它。

于 2012-04-04T09:49:15.930 回答
1

You should use stored procedure

string commandText = "SP_Your_Sp_Name";

using (SqlConnection objSqlConnection = Connection)

{

    using (SqlCommand cmd = new SqlCommand(commandText, objSqlConnection))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Parameter_Name", value));
        if (cmd.Connection.State != ConnectionState.Open)
        {
            cmd.Connection.Open();
        }
        result = (string)cmd.ExecuteScalar();
    }
}
于 2012-04-04T09:55:25.007 回答
0

使用存储过程。在这种情况下,当执行计划存储在缓存中时,查询执行会更快。

于 2012-04-17T05:49:15.460 回答