日期:2014-05-19  浏览次数:21139 次

以释放所有代码路径的类型为“'conn'”的本地 SqlConnection
警告 200 CA2000   :   Microsoft.Reliability   :   修改   DaBBS.ColumnInsert(BBSColumn):Boolean   以释放所有代码路径的类型为“ 'conn '”的本地   SqlConnection。

我没有使用using语句,而是使用try   catch   finally   ,它给出这样的建议,我想知道怎么正确来写

                      SqlConnection   conn   =   new   SqlConnection(ConnectionString.DbBBSString);
                        SqlCommand   comm   =   new   SqlCommand();
……
try
                        {
                                conn.Open();
                                ……
                        }
                        catch   (SqlException   ex)
                        {
                                ……
                        }
                        catch   (Exception   ex)
                        {
                                ……
                        }
                        finally
                        {
                                conn.Close();
                        }

------解决方案--------------------
if(!Page.IsPostBack)
{
SqlConnection cn = null;
SqlCommand cmd = null;
SqlDataReader dr = null;
try
{
string str = ...;
cn = new SqlConnection(str);

str = ...;
cmd = new SqlCommand(str, cn);

cn.Open();
dr = cmd.ExecuteReader();
...
}
catch(System.Data.SqlClient.SqlException )
{

}
finally
{
if(dr != null)
dr.Close();
if (cn.State == ConnectionState.Open)
cn.Close();
}
}
------解决方案--------------------
这个是代码书写的时候的问题