简体   繁体   English

调用存储过程时需要Connection.Open()吗?

[英]Connection.Open() necessary when calling stored procedure?

Normaly I would use either "Using" or just connection.open() and connection.close(). 通常,我将使用“使用”或仅使用connection.open()和connection.close()。 But when I'm calling stored procedure, this is not needed. 但是,当我调用存储过程时,不需要这样做。 How come? 怎么会? (Yes the codesnippit below works without using or open). (是,下面的codenippitpit无需使用或打开即可工作)。

try {
    SqlCommand cmd = new SqlCommand("***", connectionSiteDb);
    DataTable dt = new DataTable();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@ProcessSegmentID", ProcessSSegmentID));
    cmd.Parameters.Add(new SqlParameter("@PO_RecipeID", PO_RecipeID));
    cmd.Parameters.Add(new SqlParameter("@ProductSegmentVersion", ProductSegmentVersion));


    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    da.Fill(dt);
    return dt;
}
catch (Exception e) {
     Console.WriteLine(e);
     return null;
}

Using using statement or not doesn't depends on your CommandType is Text or StoredProcedure . 是否使用using语句并不取决于您的CommandTypeText还是StoredProcedure

SqlDataAdapter.Fill opens connection itself. SqlDataAdapter.Fill打开连接本身。

From Populating a DataSet from a DataAdapter DataAdapter中填充数据集

The Fill method implicitly opens the Connection that the DataAdapter is using if it finds that the connection is not already open. 如果Fill方法发现连接尚未打开,则隐式打开DataAdapter正在使用的Connection。 If Fill opened the connection, it will also close the connection when Fill is finished. 如果“填充”打开了连接,则在“填充”完成时,它也会关闭连接。 This can simplify your code when dealing with a single operation such as a Fill or an Update. 当处理诸如填充或更新之类的单个操作时,这可以简化您的代码。

Also DbDataAdapter.Fill(DataTable) DbDataAdapter.Fill(DataTable)

The connection object associated with the SELECT statement must be valid, but it does not need to be open . 与SELECT语句关联的连接对象必须有效, 但是不必打开 If the connection is closed before Fill is called, it is opened to retrieve data, then closed. 如果在调用Fill之前关闭了连接,则将其打开以检索数据,然后关闭。 If the connection is open before Fill is called, it remains open. 如果在调用Fill之前连接已打开,则它将保持打开状态。

Since SqlDataAdapter doesn't implement IDisposable , you don't need to use using statement with it. 由于SqlDataAdapter没有实现IDisposable ,因此您无需将using语句与其一起使用。

If you want to look under the hood, you can check QuietClose method and QuietOpen method implementations; 如果要深入了解,可以检查QuietClose方法QuietOpen方法的实现。

static private void QuietClose(IDbConnection connection, ConnectionState originalState)
{
      // close the connection if:
      // * it was closed on first use and adapter has opened it, AND
      // * provider's implementation did not ask to keep this connection open
      if ((null != connection) && (ConnectionState.Closed == originalState)) {
          // we don't have to check the current connection state because
          // it is supposed to be safe to call Close multiple times
          connection.Close();
      }
}

// QuietOpen needs to appear in the try {} finally { QuietClose } block
// otherwise a possibility exists that an exception may be thrown, i.e. ThreadAbortException
// where we would Open the connection and not close it
static private void QuietOpen(IDbConnection connection, out ConnectionState originalState)
{
     originalState = connection.State;
     if (ConnectionState.Closed == originalState) {
         connection.Open();
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM