Example #1
0
        /// <summary>
        /// 静态方法,获取表中所有数据
        /// </summary>
        /// <returns></returns>
        static public List <DataEntity> Get()
        {
            SqlEntity sqlEntity = new SqlEntity();

            if (sqlEntity.Cnt == null)
            {
                return(null);
            }
            else
            {
                string sql = "select * from " + tablename + ";";
                try {
                    MySqlCommand cmd    = new MySqlCommand(sql, sqlEntity.Cnt);
                    var          reader = cmd.ExecuteReader();
                    if (reader == null)
                    {
                        sqlEntity.close();
                        return(null);
                    }
                    List <DataEntity> dataList = new List <DataEntity>();
                    while (reader.Read())
                    {
                        DataEntity dataEntity = new DataEntity();
                        dataEntity.userid   = reader.GetString("userid");
                        dataEntity.username = reader.GetString("username");
                        dataEntity.sex      = (int)reader.GetDouble("sex");
                        dataEntity.grade    = (int)reader.GetDouble("grade");
                        dataEntity.chinese  = reader.GetDouble("chinese");
                        dataEntity.math     = reader.GetDouble("math");
                        dataEntity.english  = reader.GetDouble("english");
                        if (!reader.IsDBNull(7))
                        {
                            dataEntity.remark = reader.GetString("remark");
                        }
                        else
                        {
                            dataEntity.remark = "";
                        }
                        dataList.Add(dataEntity);
                    }
                    sqlEntity.close();
                    return(dataList);
                }
                catch {
                    sqlEntity.close();
                    return(null);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 静态方法,根据id获取对象
        /// </summary>
        /// <param name="userid"></param>
        /// <returns>查询所得的datainfo对象</returns>
        static public DataEntity GetByID(string userid)
        {
            SqlEntity sqlEntity = new SqlEntity();

            if (sqlEntity.Cnt == null)
            {
                return(null);
            }

            else
            {
                string sql = "select * from " + tablename + " where userid='" + userid + "';";
                try {
                    MySqlCommand cmd    = new MySqlCommand(sql, sqlEntity.Cnt);
                    var          reader = cmd.ExecuteReader();
                    if (reader == null)
                    {
                        sqlEntity.close();
                        return(null);
                    }
                    DataEntity dataEntity = new DataEntity();
                    while (reader.Read())
                    {
                        dataEntity.userid   = reader.GetString("userid");
                        dataEntity.username = reader.GetString("username");
                        dataEntity.sex      = reader.GetInt16("sex");
                        dataEntity.grade    = reader.GetInt16("grade");
                        dataEntity.chinese  = reader.GetDouble("chinese");
                        dataEntity.math     = reader.GetDouble("math");
                        dataEntity.english  = reader.GetDouble("english");
                        if (!reader.IsDBNull(6))
                        {
                            dataEntity.remark = reader.GetString("remark");
                        }
                        else
                        {
                            dataEntity.remark = "";
                        }
                    }
                    sqlEntity.close();
                    return(dataEntity);
                }
                catch {
                    sqlEntity.close();
                    return(null);
                }
            }
        }