Ejemplo n.º 1
0
        protected List<Model> queryBySql(String sql, IRowMapper mapper)
        {
            //经测试没有问题
            SqlConnection conn = null;//数据库连接
            //  SqlDataReader dr = null;
            DataSet ds = null;//数据集
            SqlCommand cmd = null;//
            List<Model> retList = new List<Model>();
            try
            {
                conn = dbConn.getConnection();
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;             
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                ds = new DataSet();
                adp.Fill(ds);

                foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                {
                    Model obj = (Model)mapper.mappingRow(row);
                    retList.Add(obj);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                dbConn.closeConnection(conn);

            }
            return retList;
        }