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.