コード例 #1
0
        //SSMS类->和处理直接与数据库相连的公共方法有关的类.

        /// <summary>
        /// 通过文章id获得文章
        /// </summary>
        public static Article GetArticleById(int id)
        {
            string        sql = "SELECT * FROM news WHERE nID = " + id.ToString();
            int           authorId;
            SqlDataReader reader = SSMS.GetReader(sql);

            if (reader.Read())
            {
                Article article = new Article();
                article.NID      = (int)reader["nID"];
                article.NTitle   = (string)reader["ntitle"];
                article.NContent = (string)reader["ncontent"];
                article.NDate    = (DateTime)reader["ndate"];
                authorId         = (int)reader["id"];
                article.Nkey     = (string)reader["nkey"];
                article.Aname    = (string)reader["aname"];
                reader.Close();
                article.Author = UserServer.GetUserById(authorId);
                return(article);
            }
            else
            {
                reader.Close();
                return(null);
            }
        }
コード例 #2
0
        /// <summary>
        /// 通过sql获取
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        private static List <Article> GetArticlesBySql(string sql)
        {
            List <Article> list  = new List <Article>();
            DataTable      table = SSMS.GetDataSet(sql);

            foreach (DataRow row in table.Rows)
            {
                Article article = new Article();

                article.NID      = (int)row["nID"];
                article.NTitle   = (string)row["ntitle"];
                article.NContent = (string)row["ncontent"];
                article.NDate    = (DateTime)row["ndate"];
                article.Nkey     = (string)row["nkey"];
                article.Aname    = (string)row["aname"];
                article.Author   = UserServer.GetUserById((int)row["id"]); //FK
                list.Add(article);
            }
            return(list);
        }