Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从源代码重构sql查询_C#_.net_Sql_Refactoring - Fatal编程技术网

C# 从源代码重构sql查询

C# 从源代码重构sql查询,c#,.net,sql,refactoring,C#,.net,Sql,Refactoring,我有一个方法,它直接在.CS文件中包含一个非常大的sql查询。推荐的重构方法是什么 您可以将大型复杂SQL查询放入SQL视图或存储过程,并在代码中使用。您可以将大型复杂SQL查询放入SQL视图或存储过程,并在代码中使用。您应该使用存储过程 string commandText = "SP_Your_Sp_Name"; using (SqlConnection objSqlConnection = Connection) { using (SqlCommand cmd = new S

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

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

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

您应该使用存储过程

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();
    }
}

您应该使用存储过程

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();
    }
}
使用存储过程。 在这种情况下,当执行计划存储在缓存中时,查询执行会更快。

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

将数据库代码移到它所属的位置。。。创建存储过程。将数据库代码移到它所属的位置。。。创建存储过程。在服务器上创建存储过程不是那么容易(出于某种原因)在服务器上创建存储过程不是那么容易(出于某种原因)