Example #1
0
        /// <summary>
        /// 执行查询操作
        /// </summary>
        public void OrtherQuery()
        {
            SqlDb db = new SQLiteDb(_connString);

            db.IsShowSqlToConsole = _isShowSqlToConsole;
            var dbFactory  = db.DbBuilder.DbFactory;
            var dbOperator = dbFactory.GetDbParamOperator();
            var dbParams   = new List <IDbDataParameter>();
            var sql        = "select * from UserInfo where UserId=4";
            var paramSql   = string.Format("select * from UserInfo where UserId={0}UserId", dbOperator);

            //自定义查询,返回单条数据(无参)
            UserInfo data;

            using (var dataReader = db.ExecuteReaderWithObjParam(sql, null))
            {
                data = db.DataReaderToEntity <UserInfo>(dataReader);
            }
            using (var dataReader = db.ExecuteReader(sql, null))
            {
                data = db.DataReaderToEntity <UserInfo>(dataReader);
            }

            //自定义查询,返回单条数据(有参)
            using (var dataReader = db.ExecuteReaderWithObjParam(paramSql, new { UserId = 4 }))
            {
                data = db.DataReaderToEntity <UserInfo>(dataReader);
            }
            dbParams = new List <IDbDataParameter>();
            dbParams.Add(dbFactory.GetDbParam("UserId", 4));
            using (var dataReader = db.ExecuteReader(paramSql, dbParams))
            {
                data = db.DataReaderToEntity <UserInfo>(dataReader);
            }

            //自定义查询,返回多条数据(无参)
            sql = "select * from UserInfo where UserId>2";
            List <UserInfo> dataList;

            using (var dataReader = db.ExecuteReaderWithObjParam(sql, null))
            {
                dataList = db.DataReaderToEntityList <UserInfo>(dataReader);
            }
            using (var dataReader = db.ExecuteReader(sql, null))
            {
                dataList = db.DataReaderToEntityList <UserInfo>(dataReader);
            }

            //自定义查询,返回多条数据(有参)
            paramSql = string.Format("select * from UserInfo where UserId>{0}UserId", dbOperator);
            using (var dataReader = db.ExecuteReaderWithObjParam(paramSql, new { UserId = 2 }))
            {
                dataList = db.DataReaderToEntityList <UserInfo>(dataReader);
            }
            dbParams = new List <IDbDataParameter>();
            dbParams.Add(dbFactory.GetDbParam("UserId", 2));
            using (var dataReader = db.ExecuteReader(paramSql, dbParams))
            {
                dataList = db.DataReaderToEntityList <UserInfo>(dataReader);
            }
        }