0

My OleDB Class

public oleDB()
        {
            if (string.IsNullOrWhiteSpace(CONNECTION_STRING))
            {
IDSTool.Security.EncryptDecryptData();

                string dbname = "D:\\C# Desktop\\Database\\GoodsManagementMDB1.mdb;";
Source={0};Jet OLEDB:Database Password={1}", dbname, password);
                CONNECTION_STRING = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Persist Security Info=False;", dbname);
                ConnectionString = CONNECTION_STRING;
            }
            else
            {
                ConnectionString = CONNECTION_STRING;
            }
            DbConnection = new OleDbConnection(ConnectionString);
            DbCommand = new OleDbCommand();
            DbCommand.Connection = DbConnection;
        }


public void ExecuteReader()
        {
            try
            {
                DbDataReader = DbCommand.ExecuteReader();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DbCommand.Parameters.Clear();
            }
        }

My AbstractDataAccess

public IDbCommand DbCommand
        {
            get { return dbCommand; }
            set { dbCommand = value; }
        }

public void Close()
        {
            if (DbConnection.State == ConnectionState.Open || DbConnection.State == ConnectionState.Fetching)
                DbConnection.Close();
        }

        #region IDisposable Members

        public void Dispose()
        {
            dbConnection.Close();
            dbConnection.Dispose();
            dbCommand.Dispose();

            if (dbDataReader != null)
            {
                dbDataReader.Close();
                dbDataReader.Dispose();
            }

            if (dbDataAdapter != null)
            {
                dbDataAdapter = null;
            }

            GC.Collect();
            GC.SuppressFinalize(this);
        }

My User Class

public bool CheckLogin(string userID, string userpassword)
        {
            bool result = false;
            using (DatabaseAccess.oleDB oleDB = new DatabaseAccess.oleDB())
            {
                using (oleDB.DbCommand)
                {
                    oleDB.CommandText = @"SELECT * FROM[User] where UserID=@UserID";
                    oleDB.AddParameter("@UserID", System.Data.OleDb.OleDbType.VarChar, userID);
                    oleDB.CommandType = System.Data.CommandType.Text;

                    try
                    {
                        oleDB.Open();
                        oleDB.ExecuteReader();

                        while (oleDB.DbDataReader.Read())
                        {
                            if (userID == oleDB.DbDataReader["UserID"].ToString() && userpassword == Tools.Security.Decrypted(oleDB.DbDataReader["Password"].ToString()))
                                result = true;
                            else
                                result = false;
                        }
                        oleDB.Close();
                        return result;
                    }
                    catch (Exception)
                    {
                        return result;
                        throw;
                    }
                }
            }
                
        }

I use the oledb class as a reference in the User class to connect to the access database, oledb inherits from the AbstratDataAccess class where the AbstratDataAccess class has properties dbconnection, dbcommand etc., I have declared new DbCommand in the oledb class, but why when DbCommand.Dispose(); in Public Void Closer() method an error appears COM object that has been separated from its underlying RCW cannot be used.

  • Are you using Excel or Word etc in your code anywhere? – mjwills Jul 20 '21 at 03:31
  • 1
    `GC.Collect(); GC.SuppressFinalize(this);` is _very weird_ to have in a Dispose method. The `Collect` shouldn't be there, and if you really need it there (and you don't) then you need to reverse the order of those two lines. – mjwills Jul 20 '21 at 03:32
  • Please share the entirety of `AbstractDataAccess`. My guess is that file is problematic since I am guessing you are sharing connections / commands, which is "not a good idea". https://www.codeproject.com/Questions/5263898/How-to-fix-COM-object-that-has-been-separated-from https://www.codeproject.com/Questions/649088/COM-object-that-has-been-separated-from-its-underl – mjwills Jul 20 '21 at 03:33
  • 1
    A better use of connections and commands is to get rid of your abstraction, and use them directly (or use Dapper etc). Then use `using` blocks rather than calling `Close` / `Dispose` manually. – mjwills Jul 20 '21 at 03:35
  • `throw ex;` is just wrong, it will wipe the stack trace. `catch (Exception){return result; throw;}` doesn't make any sense at all. – Charlieface Jul 20 '21 at 15:21

0 Answers0