难道DataAdapter.Fill方法()时,抛出一个异常关闭它的连接?它的、抛出、异常、方法

2023-09-02 10:49:22 作者:慕汐

我使用ADO.NET(.NET 1.1)在传统的应用程序。我知道,DataAdapter.Fill方法()打开和关闭连接,如果连接尚未手动打开之前它给DataAdapter的。

我的问题:?是否也关闭了连接,如果.fill伪()导致异常(由于SQL Server无法达到,或其他)。是否泄漏连接或它有一个内置的最后子句,以确保连接被关闭。

code例:

 昏暗CMD作为新的SqlCommand
昏暗的大作为新的SqlDataAdapter
昏暗的DS作为新的数据集
cmd.Connection =新的SqlConnection(strConnection)
cmd.CommandText = STRSQL
da.SelectCommand = CMD
da.Fill(DS)
 

解决方案

如果连接是打开的fill()方法被调用之前,则没有,该连接不会被DataAdapter的关闭。

不过,如果你没有明确地打开连接,而是让DataAdapter的打开和关闭填充()命令中的连接,那么连接将被关闭的错误。

数据访问:OleDbDataAdapter Fill 一个DataSet时,报错 至少一个参数没有被指定值,通过编译,求大神解答

这可以从文档的多个来源,包括这一个暗示策略使用ADO.NET和SQL

此外,这可以通过编写一个程序,将出错了,然后检查连接的状态表现在code。

从Windows此code窗体应用程序证明了这一点。第一个消息框会说打开,第二个闭。

 字符串CONNSTRING =;
        私人无效Form1_Load的(对象发件人,EventArgs的)
        {
            CONNSTRING = Properties.Settings.Default.EventLoggingConnectionString;
            ExplicitlyOpenConnection();
            LetDataAdapterHandleIt();
        }

        私人无效ExplicitlyOpenConnection()
        {
            System.Data.SqlClient.SqlConnection CN =新System.Data.SqlClient.SqlConnection(CONNSTRING);
            System.Data.DataSet中DS =新的DataSet();
            System.Data.SqlClient.SqlDataAdapter广告=新System.Data.SqlClient.SqlDataAdapter(选择nonexistenttable bogusdata,CN);

            cn.Open();
            尝试
            {
                ad.Fill(DS);
            }
            赶上(例外前)
            {

            }

            的MessageBox.show(cn.State.ToString());
            cn.Close();
        }
        私人无效LetDataAdapterHandleIt()
        {
            System.Data.SqlClient.SqlConnection CN =新System.Data.SqlClient.SqlConnection(CONNSTRING);
            System.Data.DataSet中DS =新的DataSet();
            System.Data.SqlClient.SqlDataAdapter广告=新System.Data.SqlClient.SqlDataAdapter(选择nonexistenttable bogusdata,CN);

            尝试
            {
                ad.Fill(DS);
            }
            赶上(例外前)
            {

            }
            的MessageBox.show(cn.State.ToString());
        }
 

I am using ADO.NET (.NET 1.1) in a legacy app. I know that DataAdapter.Fill() opens and closes connections if the connection hasn't been opened manually before it's given to the DataAdapter.

My question: Does it also close the connection if the .Fill() causes an Exception? (due to SQL Server cannot be reached, or whatever). Does it leak a connection or does it have a built-in Finally-clause to make sure the connection is being closed.

Code Example:

Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim ds As New DataSet
cmd.Connection = New SqlConnection(strConnection)
cmd.CommandText = strSQL
da.SelectCommand = cmd
da.Fill(ds)

解决方案

If the connection is open before the Fill() method is called, then no, the connection will not be closed by the DataAdapter.

However, if you do not explicitly open the connection, and instead let the DataAdapter open and close the connection within the Fill() command, then the connection will be closed on error.

This can be implied from multiple sources of documentation, including this one: Data Access Strategies Using ADO.NET and SQL

Further, this can be demonstrated in code by writing a routine that will error out and then checking the connection's State.

This code from a Windows Forms app proves it. The first message box will say "Open" and the second "Closed".

              string connString = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            connString = Properties.Settings.Default.EventLoggingConnectionString;
            ExplicitlyOpenConnection();
            LetDataAdapterHandleIt();
        }

        private void ExplicitlyOpenConnection()
        {
            System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(connString);
            System.Data.DataSet ds = new DataSet();
            System.Data.SqlClient.SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter("Select bogusdata from nonexistenttable", cn);

            cn.Open();
            try
            {
                ad.Fill(ds);
            }
            catch (Exception ex)
            {

            }

            MessageBox.Show(cn.State.ToString());
            cn.Close();
        }
        private void LetDataAdapterHandleIt()
        {
            System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(connString);
            System.Data.DataSet ds = new DataSet();
            System.Data.SqlClient.SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter("Select bogusdata from nonexistenttable", cn);

            try
            {
                ad.Fill(ds);
            }
            catch (Exception ex)
            {

            }
            MessageBox.Show(cn.State.ToString());
        }