本文整理汇总了C#中System.Data.Common.DbConnection.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# DbConnection.Dispose方法的具体用法?C# DbConnection.Dispose怎么用?C# DbConnection.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.Common.DbConnection
的用法示例。
在下文中一共展示了DbConnection.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CloseConnection
/// <summary>
/// 关闭一个数据库连接
/// </summary>
/// <param name="dbConn">数据库连接对象</param>
public static void CloseConnection(DbConnection dbConn)
{
if (dbConn == null || dbConn.State != ConnectionState.Open)
{
return;
}
dbConn.Close();
dbConn.Dispose();
dbConn = null;
}
示例2: Close
/// <summary>
/// Close a connection.
/// </summary>
/// <param name="Connection">The connection.</param>
public void Close(DbConnection Connection) {
// Check if the connection is open.
if (Connection != null && Connection.State == ConnectionState.Open) {
// Check if the pool has not been disposed.
if (!_Disposed) {
// Lock this object.
lock (this) {
// Check if the pool has not been disposed.
if (!_Disposed) {
// Add the connection to the pool.
_Pool.Add(Connection);
// Return from the method.
return;
}
}
}
// Dispose of the connection.
Connection.Dispose();
}
}
示例3: CloseConnection
/// <summary>
/// Closes the connection.
/// </summary>
/// <param name="conn">The conn.</param>
public void CloseConnection(DbConnection conn)
{
if (conn != null && conn.State != ConnectionState.Closed)
{
conn.Close();
conn.Dispose();
}
}
示例4: CloseConnection
public void CloseConnection(DbConnection conn)
{
if (conn != null & conn.State != ConnectionState.Closed)
{
try
{
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
}
示例5: MultipleErrorHandling
private void MultipleErrorHandling(DbConnection connection)
{
try
{
Console.WriteLine("MultipleErrorHandling {0}", connection.GetType().Name);
Type expectedException = null;
if (connection is SqlConnection)
{
((SqlConnection)connection).InfoMessage += delegate (object sender, SqlInfoMessageEventArgs args)
{
Console.WriteLine("*** SQL CONNECTION INFO MESSAGE : {0} ****", args.Message);
};
expectedException = typeof(SqlException);
}
connection.Open();
using (DbCommand command = connection.CreateCommand())
{
command.CommandText =
"PRINT N'0';\n" +
"SELECT num = 1, str = 'ABC';\n" +
"PRINT N'1';\n" +
"RAISERROR('Error 1', 15, 1);\n" +
"PRINT N'3';\n" +
"SELECT num = 2, str = 'ABC';\n" +
"PRINT N'4';\n" +
"RAISERROR('Error 2', 15, 1);\n" +
"PRINT N'5';\n" +
"SELECT num = 3, str = 'ABC';\n" +
"PRINT N'6';\n" +
"RAISERROR('Error 3', 15, 1);\n" +
"PRINT N'7';\n" +
"SELECT num = 4, str = 'ABC';\n" +
"PRINT N'8';\n" +
"RAISERROR('Error 4', 15, 1);\n" +
"PRINT N'9';\n" +
"SELECT num = 5, str = 'ABC';\n" +
"PRINT N'10';\n" +
"RAISERROR('Error 5', 15, 1);\n" +
"PRINT N'11';\n";
try
{
Console.WriteLine("**** ExecuteNonQuery *****");
command.ExecuteNonQuery();
}
catch (Exception e)
{
PrintException(expectedException, e);
}
try
{
Console.WriteLine("**** ExecuteScalar ****");
command.ExecuteScalar();
}
catch (Exception e)
{
PrintException(expectedException, e);
}
try
{
Console.WriteLine("**** ExecuteReader ****");
using (DbDataReader reader = command.ExecuteReader())
{
bool moreResults = true;
do
{
try
{
Console.WriteLine("NextResult");
moreResults = reader.NextResult();
}
catch (Exception e)
{
PrintException(expectedException, e);
}
} while (moreResults);
}
}
catch (Exception e)
{
PrintException(null, e);
}
}
}
catch (Exception e)
{
PrintException(null, e);
}
try
{
connection.Dispose();
}
catch (Exception e)
{
PrintException(null, e);
}
}
示例6: Setup
private void Setup()
{
parametersList = new List<DbParameter>();
target = new DatabaseTraceListener_Accessor(
storedProcedureName, logConnectionStringName, null, null);
//target.connectionStringName = "TestConnectionString";
target.factory = MockRepository.GenerateStub<DbProviderFactory>();
target.connectionStringProvider = MockRepository.GenerateStub<IConfigurationValueProvider>();
target.connectionStringProvider.Stub((p) => p[target.connectionStringName]).
Return(target.connectionStringName);
connection = MockRepository.GenerateStub<DbConnection>();
command = MockRepository.GenerateStub<DbCommand>();
parameters = MockRepository.GenerateStub<DbParameterCollection>();
// Stubs for the connection and command
target.factory.Stub(
(f) => f.CreateConnection()).Return(connection);
target.factory.Stub((f) => f.CreateCommand()).Return(command);
// Definitely want to verify that Open is called
connection.Stub((c) => connection.Open());
// Unfortunately can't verify Dispose got called
connection.Stub((c) => connection.Dispose());
// Will verify later if ExecuteNonQuery is called
command.Stub((c) => command.ExecuteNonQuery()).Return(1);
command.Stub((c) => c.Dispose());
// Setup repetitive stub for the command parameters
//DbParameter parameter = null;
command.Stub((c) => c.Parameters).Repeat.Any().Return(parameters);
for (int i = 0; i < 12; i++)
{
DbParameter parameter = MockRepository.GenerateStub<DbParameter>();
target.factory.Stub((f) => f.CreateParameter()).Return(parameter);
parameters.Stub((p) => p.Add(parameter)).Do(new HandleParameterDelegate(HandleParameter));
}
}
示例7: Close
public static void Close(DbConnection conn)
{
conn.Close();
conn.Dispose();
}
示例8: btnOpen_Click
private void btnOpen_Click(object sender, EventArgs e)
{
btnClose_Click(sender, null);
// main.
string sFactory = cboProvider.Text.Trim();
try {
m_provider = DbProviderFactories.GetFactory(sFactory);
}
catch (Exception ex) {
OutLog(ex.Message);
MessageBox.Show(this, ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (null == m_provider) return;
try {
// DbConnection.
try {
OutLog("Create DbConnection:");
m_conn = m_provider.CreateConnection();
OutLog(string.Format("\t{0}", m_conn.GetType().ToString()));
}
catch (Exception ex) {
OutLog(ex.Message);
MessageBox.Show(this, ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
try {
m_conn.Dispose();
}
catch (Exception ex2) {
OutLog(ex2.ToString());
}
m_conn = null;
return;
}
if (null == m_conn) return;
// open.
try {
m_conn.ConnectionString = txtConnstr.Text.Trim();
m_conn.Open();
}
catch (Exception ex) {
OutLog(ex.Message);
MessageBox.Show(this, ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
try {
m_conn.Dispose();
}
catch (Exception ex2) {
OutLog(ex2.ToString());
}
m_conn = null;
return;
}
}
finally {
if (null == m_conn) {
m_provider = null;
}
}
// done.
bool isconn = true;
btnOpen.Enabled = !isconn;
btnClose.Enabled = isconn;
btnExec.Enabled = isconn;
cboProvider.Enabled = !isconn;
txtConnstr.ReadOnly = isconn;
OutLog("Db opened.");
// show tables.
DataTable dt = m_conn.GetSchema("Tables");
grdTable.DataSource = dt;
m_CurTableName = null;
tbcInput.SelectedIndex = 1;
}
示例9: CloseConnection
private void CloseConnection(DbConnection dbConn)
{
try
{
if (dbConn != null && dbConn.State == ConnectionState.Open)
{
dbConn.Close();
dbConn.Dispose();
}
}
catch (Exception ex)
{
_logger.Error("Error closing DB connection: " + ex.Message);
}
}
示例10: DisposeConnection
/// <summary>
/// Disposes the given database connection created by this instance.
/// </summary>
/// <param name="connection">The database connection to dispose.</param>
public virtual void DisposeConnection(DbConnection connection)
{
if (connection != null)
{
connection.Dispose();
}
}
示例11: getCommand
public DbCommand getCommand(String sql)
{
DbCommand command = objectCommand();
if (command != null)
{
command.CommandText = sql;
conn = getConnection();
try
{
command.Connection = conn;
if (command.Connection != null &&
(command.Connection.State == ConnectionState.Broken
|| command.Connection.State == ConnectionState.Closed))
{
command.Connection.Open();
}
}
catch (DataException)
{
conn.Dispose();
}
}
else
command.Connection = getConnection();
return command;
}
示例12: ExecuteQuery
protected object ExecuteQuery(DbConnection DbConn, DbTransaction DbTrans, string spName, CommandType CmdType, Hashtable ParamTable, DataManager.ExecuteAction ExecAction)
{
if (this.ValidateCommandText(spName))
{
try
{
if (DbConn.State != ConnectionState.Open)
{
DbConn.Open();
}
DbCommand dbCommand = this.GetDbCommand(spName, DbConn);
dbCommand.Transaction = DbTrans;
try
{
dbCommand.CommandType = CmdType;
if (ParamTable != null)
{
Array hashValue = Utils.GetHashValue(ParamTable);
for (int index = 0; index < hashValue.Length; ++index)
{
dbCommand.Parameters.Add((object)(DbParameter)hashValue.GetValue(index));
}
}
if (ExecAction == DataManager.ExecuteAction.NonQuery)
{
long num = long.Parse(dbCommand.ExecuteNonQuery().ToString());
return (object)num;
}
else
{
object obj = dbCommand.ExecuteScalar();
return obj;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (this.m_CloseConnection)
{
DbConn.Close();
dbCommand.Dispose();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (this.m_CloseConnection)
{
DbConn.Dispose();
}
}
}
else
{
throw new Exception("Command text empty");
}
}
示例13: ExecuteDataset
protected DataSet ExecuteDataset(DbConnection DbConn, DbTransaction DbTrans, string spName, CommandType CmdType, Hashtable ParamTable)
{
if (!this.ValidateCommandText(spName))
throw new Exception("Command text empty");
try
{
if (DbConn.State != ConnectionState.Open)
{
DbConn.Open();
}
DbCommand dbCommand = this.GetDbCommand(spName, DbConn);
dbCommand.Transaction = DbTrans;
try
{
dbCommand.CommandType = CmdType;
if (m_CommandTimeout > 0)
{
dbCommand.CommandTimeout = m_CommandTimeout;
}
if (ParamTable != null)
{
Array hashValue = Utils.GetHashValue(ParamTable);
for (int index = 0; index < hashValue.Length; ++index)
{
dbCommand.Parameters.Add((object)(DbParameter)hashValue.GetValue(index));
}
}
DbDataAdapter dataAdapter = this.GetDataAdapter();
try
{
DataSet dataSet = new DataSet();
dataAdapter.SelectCommand = dbCommand;
((DataAdapter)dataAdapter).Fill(dataSet);
return dataSet;
}
catch (Exception ex)
{
throw ex;
}
finally
{
dataAdapter.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (this.m_CloseConnection)
{
DbConn.Close();
}
dbCommand.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (this.m_CloseConnection)
{
DbConn.Dispose();
}
}
}
示例14: CloseDisposeConnection
/// <summary>
/// Closes and disposes the given connection instance.
/// </summary>
/// <param name="connection">the connection isntance to be closed and disposed</param>
public static void CloseDisposeConnection(DbConnection connection)
{
if (connection == null)
{
return;
}
try
{
CloseConnection(connection);
connection.Dispose();
}
catch (System.Exception e)
{
throw new PersistenceException("Could not close and dispose connection", e);
}
}
示例15: DisposeConnection
protected void DisposeConnection(DbConnection connection)
{
if (connection != null)
connection.Dispose();
}