是否SqlDataAdapter的关闭填充()函数后的SqlConnection? [英] Does SqlDataAdapter close the SqlConnection after Fill() function?

查看:315
本文介绍了是否SqlDataAdapter的关闭填充()函数后的SqlConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者我需要关闭它自己吗?

 字符串cnStr = @数据源= TEST;初始目录=套房;坚持安全信息= TRUE;用户ID =应用;密码= Immmmmm;
        CN =新的SqlConnection(cnStr);
        的SqlCommand CMD =新的SqlCommand(SELECT TOP 10 * FROM日期,CN);
        SqlDataAdapter的适配器=新的SqlDataAdapter(CMD);

        的DataSet ds为新的DataSet();
        adapter.Fill(DS);

        cn.Close()// ????????

        Console.WriteLine(ds.Tables [0] .Rows.Count);
        Console.WriteLine(cn.State);
 

解决方案

在目前的使用情况,它会关闭你:

  

如果在的IDbConnection   被封闭填充被调用之前,它被打开来检索数据和   然后关闭。如果连接是打开前填充被调用,它   保持打开状态。

<一个href="http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx">http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx

我觉得它总是最好明确满足它自己一个使用语句:

 使用(SqlConnection的康恩=新的SqlConnection())
{
    conn.Open();

    //做的东西。

} //关闭在这里部署。
 

这是通常更可读,并且不依赖于人的理解 SqlDataAdapter.Fill 的内部工作,仅仅是使用语句和连接。

然而,如果你的知道的适配器使用它之前(如,您刚刚创建的连接),它不是用于其它连接被关闭,你的code是非常安全和有效的。

就个人而言,我会写是这样的:

 字符串cnStr =数据源= TEST;初始目录=套房;坚持安全信息= TRUE;用户ID =应用;密码= Immmmmm;
    的DataSet ds为新的DataSet();

    使用(SqlConnection的CN =新的SqlConnection(cnStr))
    使用(CMD的SqlCommand =新的SqlCommand(SELECT TOP 10 * FROM日期,CN))
    使用(SqlDataAdapter的适配器=新的SqlDataAdapter(CMD))
    {
        conn.Open();
        adapter.Fill(DS);
    }
 

or I need close it myself?

        string cnStr = @"Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm";
        cn = new SqlConnection(cnStr);
        SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Date", cn);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        adapter.Fill(ds);

        cn.Close() // ????????

        Console.WriteLine(ds.Tables[0].Rows.Count);
        Console.WriteLine(cn.State);

解决方案

In your current usage, it will close for you:

If the IDbConnection is closed before Fill is called, it is opened to retrieve data and then closed. If the connection is open before Fill is called, it remains open.

http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx

I think it's always better to explicitly cater for it yourself with a using statement:

using (SqlConnection conn = new SqlConnection(""))
{
    conn.Open();

    // Do Stuff.

} // Closes here on dispose.

This is often more readable and doesn't rely on people understanding the inner workings of SqlDataAdapter.Fill, just the using statement and connections.

However, if you know the connection is closed before the adapter uses it (as in, you've just created the connection) and it's not used for anything else, your code is perfectly safe and valid.

Personally, I'd write something like this:

    string cnStr = "Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm";
    DataSet ds = new DataSet();

    using (SqlConnection cn = new SqlConnection(cnStr))
    using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Date", cn))
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
    { 
        conn.Open();
        adapter.Fill(ds);       
    }

这篇关于是否SqlDataAdapter的关闭填充()函数后的SqlConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆