当前位置: 首页>>代码示例>>C#>>正文


C# DbConnection.Dispose方法代码示例

本文整理汇总了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;
        }
开发者ID:pmsun-bruce,项目名称:DBTool,代码行数:15,代码来源:MysqlHelper.cs

示例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();
			}
		}
开发者ID:Deathspike,项目名称:jouhou,代码行数:24,代码来源:ConnectionPool.cs

示例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();
     }
 }
开发者ID:Oman,项目名称:Maleos,代码行数:12,代码来源:Database+-+EF.cs

示例4: CloseConnection

 public void CloseConnection(DbConnection conn)
 {
     if (conn != null & conn.State != ConnectionState.Closed)
     {
         try
         {
             conn.Close();
             conn.Dispose();
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
开发者ID:tu226,项目名称:Eagle,代码行数:15,代码来源:Database.cs

示例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);
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:100,代码来源:MultipleResultsTest.cs

示例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));
            }
        }
开发者ID:alienwaredream,项目名称:toolsdotnet,代码行数:42,代码来源:DatabaseTraceListenerTest.cs

示例7: Close

 public static void Close(DbConnection conn)
 {
     conn.Close();
     conn.Dispose();
 }
开发者ID:mateuscezar,项目名称:netgore,代码行数:5,代码来源:TestDb.cs

示例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;
 }
开发者ID:zyl910,项目名称:zdbviewcs,代码行数:71,代码来源:ZDbViewCsForm.cs

示例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);
       }
 }
开发者ID:iringtools,项目名称:sp_pid,代码行数:15,代码来源:DBManager.cs

示例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();
     }
 }
开发者ID:ChadBurggraf,项目名称:blue-collar-old,代码行数:11,代码来源:SqlJobStore.cs

示例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;
 }
开发者ID:MonoBrasil,项目名称:CsDO,代码行数:26,代码来源:Drivers.Generic.cs

示例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");
     }
 }
开发者ID:rpujan,项目名称:Aprender,代码行数:64,代码来源:DataManager.cs

示例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();
         }
     }
 }
开发者ID:rpujan,项目名称:Aprender,代码行数:69,代码来源:DataManager.cs

示例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);
            }
        }
开发者ID:FH-Thomas-Herzog,项目名称:SemesterProject1516,代码行数:21,代码来源:DbConnectionFactory.cs

示例15: DisposeConnection

 protected void DisposeConnection(DbConnection connection)
 {
     if (connection != null)
         connection.Dispose();
 }
开发者ID:taspeotis,项目名称:LocalReportsEngine,代码行数:5,代码来源:AdoNetDataSource.cs


注:本文中的System.Data.Common.DbConnection.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。