C#SQLを呼び出すストアド・プロシージャのインタフェースと実装

3547 ワード

 1.    ExecuteStoredProcedure(string storedProcedureName, params ObjectParameter[] parameters)
 2.          ,     。 
 3.   :    (       Sql/MySql/ODBC  );     DbCommand;  Text  Type;    Parameter(DBParameter    ,      connection  );      ( Open);   Execute  ;     。 
 4. 
     : 
 
 

// In V1 of the EF, the context connection is always an EntityConnection                 EntityConnection entityConnection = (EntityConnection)protocolDB.Connection;                   // The EntityConnection exposes the underlying store connection                 DbConnection storeConnection = entityConnection.StoreConnection;                 DbCommand command = storeConnection.CreateCommand();                 command.CommandText = storedProcedureName;                 command.CommandType = CommandType.StoredProcedure;                   if (storeConnection is SqlConnection)                 {                     foreach (ObjectParameter p in parameters)                     {                         command.Parameters.Add(new SqlParameter { ParameterName = p.Name, Value = p.Value });                     }                 }                 else if (storeConnection is MySqlConnection)                 {                     foreach (ObjectParameter p in parameters)                     {                         command.Parameters.Add(new MySqlParameter { ParameterName = p.Name, Value = p.Value });                     }                 }                 else                 {                     return enProtocolDBReturnCodes.OPERATION_FAILED;                 }                   bool openingConnection = command.Connection.State == ConnectionState.Closed;                 if (openingConnection)                 {                     command.Connection.Open();                 }                   command.ExecuteNonQuery();                   if (openingConnection && command.Connection.State == ConnectionState.Open)                 {                      command.Connection.Close();                 }