数据库层抽象工厂更新1——多表内连接条件查询操作

类别:编程语言 点击:0 评论:0 推荐:

针对在Select中返回单表DataSet的情况,对该方法进行了重载,采用最常用的InnerJoin连接返回多表数据

相关资料链接:
数据库层抽象工厂解决方案归档[不断升级版] 
数据库层抽象工厂更新1——多表内连接条件查询操作 

    /// <summary>
    /// 多表内连接条件查询操作
    /// </summary>
    /// <param name="strTableName">表名组</param>
    /// <param name="strColumn">列名组</param>
    /// <param name="strCondition">条件</param>
    /// <returns>条件查询结果</returns>
    public static DataSet Select(string[] strTableName, string[] strColumn, string strCondition)
    {
        DataSet ds = new DataSet();
        Factory factory = Factory.GetInstance();
        AbstractDbFactory abstractDbFactory = factory.CreateInstance();
        IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
        concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
        concreteDbConn.Open();
        IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
        IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
        concreteDbCommand.Connection = concreteDbConn;
        concreteDbCommand.Transaction = concreteDbTrans;
        IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter();
        try
        {
            string strSql = "SELECT ";
            for(int i = 0; i < strColumn.Length - 1; i++)
            {
                strSql += (strColumn[i] + ", ");
            }
            strSql += (strColumn[strColumn.Length - 1] + " FROM ");

            for(int i = 0; i < strTableName.Length - 1; i++)
            {
                strSql += (strTableName[i] + " Join ");
            }
            strSql += (strTableName[strTableName.Length - 1] + " ON " + strCondition);

            concreteDbCommand.CommandText = strSql;
            concreteDbAdapter.SelectCommand = concreteDbCommand;    
            concreteDbAdapter.Fill(ds);
            concreteDbTrans.Commit();
        }
        catch
        {
            concreteDbTrans.Rollback();
            ds.Clear();
            throw;
        }
        finally
        {
            concreteDbConn.Close();
        }
        return ds;
    }

本文地址:http://com.8s8s.com/it/it26715.htm