设计者生成的表适配器如何处理连接

发布于 2024-07-22 09:28:42 字数 105 浏览 7 评论 0原文

表适配器如何使用连接?

稍微解释一下,它们是否会自动打开和关闭连接,或者如果我在调用表适配器方法之前已经打开连接,它们是否会使用它并使其保持打开状态?

问候

How do table adapters make use of connections?

To explain that a bit, do they automatically open and shut connections or if I already have the connection open before calling a tableadapter method, do they use it and leave it open?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

謌踐踏愛綪 2024-07-29 09:28:42

如果您查看设计器生成的代码,您会发现如果存在连接,适配器会重用它,否则会创建一个新连接。 执行查询方法时,如果连接未打开,该方法将打开它。 如果该方法打开了它,它会在完成后关闭它。 默认情况下,您会为每个表适配器获得一个新连接。

If you look at the designer-generated code, you'll see that if there is a connection, the adapter reuses it, otherwise it creates a new one. When executing a query method, if the connection isn't open, the method opens it. If the method opened it, it closes it when it is done. By default you get a new connection for every table adapter.

时光清浅 2024-07-29 09:28:42

以下是设计者生成的典型表适配器函数的代码:

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
    [global::System.ComponentModel.DataObjectMethodAttribute(global::System.ComponentModel.DataObjectMethodType.Select, true)]
    public virtual Styles.OrdersDataTable GetOrders() {
        this.Adapter.SelectCommand = this.CommandCollection[0];
        Styles.OrdersDataTable dataTable = new Styles.OrdersDataTable();
        this.Adapter.Fill(dataTable);
        return dataTable;
    }

根据 MSDN,DbDataAdapter.Fill 的行为如下:

Fill 方法使用关联的 SelectCommand 属性指定的 SELECT 语句从数据源检索行。 与 SELECT 语句关联的连接对象必须有效,但不需要打开。 如果连接在调用 Fill 之前关闭,则会打开该连接以检索数据,然后关闭。 如果连接在调用 Fill 之前打开,它将保持打开状态。

参考:填充方法(数据表)

但是,在设计器生成的插入/删除/更新,代码如下所示:

global::System.Data.ConnectionState previousConnectionState = this.Adapter.InsertCommand.Connection.State;
        if (((this.Adapter.InsertCommand.Connection.State & global::System.Data.ConnectionState.Open) 
                    != global::System.Data.ConnectionState.Open)) {
            this.Adapter.InsertCommand.Connection.Open();
        }
        try {
            int returnValue = this.Adapter.InsertCommand.ExecuteNonQuery();
            return returnValue;
        }
        finally {
            if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
                this.Adapter.InsertCommand.Connection.Close();
            }
        }

Here is the code of a typical designer-generated Table Adapter's function:

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
    [global::System.ComponentModel.DataObjectMethodAttribute(global::System.ComponentModel.DataObjectMethodType.Select, true)]
    public virtual Styles.OrdersDataTable GetOrders() {
        this.Adapter.SelectCommand = this.CommandCollection[0];
        Styles.OrdersDataTable dataTable = new Styles.OrdersDataTable();
        this.Adapter.Fill(dataTable);
        return dataTable;
    }

According to MSDN, DbDataAdapter.Fill behave like this:

The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.

Ref: Fill Method (DataTable)

However, in a designer-generated Insert/Delete/Update, the code would look like this:

global::System.Data.ConnectionState previousConnectionState = this.Adapter.InsertCommand.Connection.State;
        if (((this.Adapter.InsertCommand.Connection.State & global::System.Data.ConnectionState.Open) 
                    != global::System.Data.ConnectionState.Open)) {
            this.Adapter.InsertCommand.Connection.Open();
        }
        try {
            int returnValue = this.Adapter.InsertCommand.ExecuteNonQuery();
            return returnValue;
        }
        finally {
            if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
                this.Adapter.InsertCommand.Connection.Close();
            }
        }
傲影 2024-07-29 09:28:42

是的,如果您之前打开连接并传递给适配器,则表适配器将保持连接打开,如果您传递关闭的连接,则打开并关闭适配器,或者保留默认连接

yea, tableadapter is leave the connection open if you opened it before and pass to adapter, and open and closa adapter if you pass closed connection, or leave the default conenction

猫九 2024-07-29 09:28:42

你可以这样做:

using (var MyConnection = new SqlConnection("Connection String Here"))
{
   var MyDataAdapter = new SqlDataAdapter("Select * from [stuff]", MyConnection);
   MyDataAdapter.Fill(MyDataSet);
}

或者

using (var MyConnection = new SqlConnection("Connection String Here"))
{
   var MyDataAdapter = new SqlDataAdapter();
   var SelectCommand = MyConnection.CreateCommand();
   SelectCommand.CommandText = "select * from [stuff]";
   MyDataAdapter.SelectCommand = SelectCommand;
   MyDataAdapter.Fill(MyDataSet);
}

至于连接寿命,如果没有打开,它会为你打开和关闭它。

You can do it like this:

using (var MyConnection = new SqlConnection("Connection String Here"))
{
   var MyDataAdapter = new SqlDataAdapter("Select * from [stuff]", MyConnection);
   MyDataAdapter.Fill(MyDataSet);
}

or

using (var MyConnection = new SqlConnection("Connection String Here"))
{
   var MyDataAdapter = new SqlDataAdapter();
   var SelectCommand = MyConnection.CreateCommand();
   SelectCommand.CommandText = "select * from [stuff]";
   MyDataAdapter.SelectCommand = SelectCommand;
   MyDataAdapter.Fill(MyDataSet);
}

As for the connection lifespan, if it is not opened, it will open and close it for you.

梦在深巷 2024-07-29 09:28:42

时会发生什么

Dim Dt As dataset1.UsersDataTable
With New dataset1TableAdapters.UsersTableAdapter
   Dt = .GetData()
End With

当连接保持打开状态 ? 或者它关闭了?

What happens when

Dim Dt As dataset1.UsersDataTable
With New dataset1TableAdapters.UsersTableAdapter
   Dt = .GetData()
End With

Did the connection leave open? or it closed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文