SqlConnection . MARS, . , , , . , , - , .
SqlConnections , / , . , , ; SQL Server, SQL Server .
, , / SqlConnection, , , . , , , .
public IEnumerable<SqlResults> ExecuteStoredProcedure(string procedure, params SqlParameter[] parameters) {
using(SqlConnection connection = new SqlConnection(MyConnectionStringProperty)) {
try {
connection.Open();
using(SqlCommand command = new SqlCommand(procedure, connection)) {
command.CommandType = CommandType.StoredProcedure;
if(parameters != null) {
command.Parameters.AddRange(parameters);
}
using(SqlDataReader reader = command.ExecuteReader()) {
yield return new SqlResults {
Reader = reader;
};
}
}
}
finally {
if(connection.State != ConnectionState.Closed) {
connection.Close();
}
}
}
}
, , - , . , , . SqlResults SqlDataReader , .
static, , singleton - , ( /). - IoC Dependency Injection .
, , . , - :
foreach(SqlResults results in MySqlHelper.ExecuteStoredProcedure(myProcedureName, new SqlParameter("myParamName", myParamValue)) {
}
, . , using SqlClient . MySqlHelper, SQL-, , , ( ).
IoC/DI, Castle Windsor. . Control ( ) , . , , MySqlHelper, , , MySqlHelper. , . , "enter", , , IoC/DI ( ). . .
IoC/DI , , singleton. MySqlHelper , , , . IoC/DI MySqlHelper, , , , .