如果让你写一个类,能同时对SQL Server 2000和Oracle数据库进行操作(与具体数据库无关的跨数据库操作的类),怎样写?能详细说明一下吗?

解决方案 »

  1.   

    1 使用orm
    2 写一个基类idbo,ado.net控件全部使用相应的接口来写,SQL Server 2000和Oracle分别继承这个基类idbo
      

  2.   

    对不起,我没有说清楚:他让我写一个能对数据库(SQL Server 2000和Oracle)具体操作的类——可以对表中的记录进行查询、添加、修改、删除。让我具体写,我不知道该怎样写。请帮我一下吧!
      

  3.   

    ADO.NET现有一个, Sql Server 与 OLEDB{Access} 共用的, 你试着看看吧.public enum ProviderType : int {SqlClient = 0, OLEDB = 1} public class ProviderFactory 
    { public ProviderFactory(ProviderType provider) 
    {
    _pType = provider;
    _initClass();
    } public ProviderFactory() 
    {
    _initClass();
    } private ProviderType _pType = ProviderType.SqlClient;
    private bool _pTypeSet = false;
    private Type[] _conType, _comType, _parmType, _daType; private void _initClass() 
    {
    _conType = new Type[2];
    _comType = new Type[2];
    _parmType = new Type[2];
    _daType = new Type[2]; // Initialize the types for the providers
    _conType[(int)ProviderType.SqlClient] = typeof(SqlConnection);
    _conType[(int)ProviderType.OLEDB] = typeof(OleDbConnection);
    _comType[(int)ProviderType.SqlClient] = typeof(SqlCommand);
    _comType[(int)ProviderType.OLEDB] = typeof(OleDbCommand);
    _parmType[(int)ProviderType.SqlClient] = typeof(SqlParameter);
    _parmType[(int)ProviderType.OLEDB] = typeof(OleDbParameter);
    _daType[(int)ProviderType.SqlClient] = typeof(SqlDataAdapter);
    _daType[(int)ProviderType.OLEDB] = typeof(OleDbDataAdapter);
    } public ProviderType Provider 
    {
    get 
    {
    return _pType;
    }
    set 
    {
    if (_pTypeSet) 
    {
    throw new ReadOnlyException("Provider already set to " + _pType.ToString()); 
    }
    else 
    {
    _pType = value;
    _pTypeSet = true;
    }
    }
    } public IDbDataAdapter CreateDataAdapterb(string commandText, IDbConnection connection)
    {
    IDataAdapter d;
    IDbDataAdapter da; d = (IDataAdapter)Activator.CreateInstance(_daType[(int)_pType], false);
    da = (IDbDataAdapter)d;
    da.SelectCommand = this.CreateCommand(commandText, connection);
    return da;
    } public IDbDataAdapter CreateDataAdapterb(IDbCommand commandText) 
    {
    IDataAdapter d;
    IDbDataAdapter da; d = (IDataAdapter)Activator.CreateInstance(_daType[(int)_pType], false);
    da = (IDbDataAdapter)d;
    da.SelectCommand = commandText;
    return da;
    } public IDataAdapter CreateDataAdapter(string commandText, IDbConnection connection) 
    {
    IDataAdapter d;
    IDbDataAdapter da; d = (IDataAdapter)Activator.CreateInstance(_daType[(int)_pType], false);
    da = (IDbDataAdapter)d;
    da.SelectCommand = this.CreateCommand(commandText, connection);
    return d;
    } public IDataAdapter CreateDataAdapter(IDbCommand commandText) 
    {
    IDataAdapter d;
    IDbDataAdapter da; d = (IDataAdapter)Activator.CreateInstance(_daType[(int)_pType], false);
    da = (IDbDataAdapter)d;
    da.SelectCommand = commandText;
    return d;
    } public IDataParameter CreateParameter(string paramName, DbType paramType) 
    {
    IDataParameter p;
    p = (IDataParameter)Activator.CreateInstance(_parmType[(int)_pType], false);
    p.ParameterName = paramName;
    p.DbType = paramType;
    return p; 
    } public IDataParameter CreateParameter(string paramName, DbType paramType, Object value) 
    {
    IDataParameter p;
    p = (IDataParameter)Activator.CreateInstance(_parmType[(int)_pType], false);
    p.ParameterName = paramName;
    p.DbType = paramType;
    p.Value = value;
    return p;
    } public IDbConnection CreateConnection(string connect) 
    {
    IDbConnection c;
    c = (IDbConnection)Activator.CreateInstance(_conType[(int)_pType], false);
    c.ConnectionString = connect;
    return c;
    } public IDbCommand CreateCommand(string cmdText) 
    {
    IDbCommand c;
    c = (IDbCommand)Activator.CreateInstance(_comType[(int)_pType], false);
    c.CommandText = cmdText;
    return c;
    } public IDbCommand CreateCommand(string cmdText, IDbConnection connection) 
    {
    IDbCommand c;
    c = (IDbCommand)Activator.CreateInstance(_comType[(int)_pType], false);
    c.CommandText = cmdText;
    c.Connection = connection;
    return c;
    } public DataSet mkFillDataSet(IDbCommand commandText, string DataTableName, int FillStartRecord, int FillMaxRecord)
    {
    DataSet dsTemp = new DataSet();
    if (this.Provider == ProviderType.SqlClient)
    {
    SqlDataAdapter sqlDA = new SqlDataAdapter(commandText.CommandText, (SqlConnection)commandText.Connection);
    sqlDA.Fill(dsTemp, FillStartRecord, FillMaxRecord, DataTableName);
    }
    else if (this.Provider == ProviderType.OLEDB)
    {
    OleDbDataAdapter oleDA = new OleDbDataAdapter(commandText.CommandText, (OleDbConnection)commandText.Connection);
    oleDA.Fill(dsTemp, FillStartRecord, FillMaxRecord, DataTableName);
    }
    else
    {
    DataTable dtNull = new DataTable(DataTableName);
    dsTemp.Tables.Add(dtNull);
    }
    return dsTemp;
    }
    }