【问题标题】:Connection State open Method in .Net.Net中的连接状态打开方法
【发布时间】:2013-10-22 05:00:29
【问题描述】:

我正在使用 .net,我对 SqlConnection 打开有疑问

  sqlconnection con = new sqlconnection("Constr");

    if (con.State == ConnectionState.Closed) ||( con.State == ConnectionState.Broken)
    { con.open();}

这种方法现在最好使用这种方法,或者一些程序员使用下面的方法

if (con.State == ConnectionState.Open)
{ con.close();}
con.open();

哪个最好,哪个最好

【问题讨论】:

    标签: c# .net vb.net sql-server-2008 connection


    【解决方案1】:

    为什么不使用using

    using (SqlConnection conn = new SqlConnection (...))
    {
    
    }
    

    我可以这样更好。你也可以使用这个代码:

    if (conn != null && conn .State == ConnectionState.Closed)
    {
    
    }
    

    【讨论】:

    • 你知道有什么不同
    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 2017-06-02
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多