Ejemplo n.º 1
0
        public IEnumerable<Articles> loadArticlesBywriterID(int writerID)
        {
            SqlConnection loadConnection = new SqlConnection("server=tf-PC\\SQLEXPRESS;database=blogData;uid=admin;pwd=s1y2x3");
            loadConnection.Open();

            string loadString = "SELECT * FROM Articles WHERE writerID=@writerID ORDER BY readtimes DESC";
            SqlCommand loadCommand = new SqlCommand(loadString, loadConnection);
            loadCommand.Parameters.Add(new SqlParameter("writerID", SqlDbType.Int));
            loadCommand.Parameters["writerID"].Value = writerID;
            SqlDataAdapter loadAdapter = new SqlDataAdapter(loadCommand);
            DataSet loadDataSet = new DataSet();
            loadAdapter.Fill(loadDataSet, "Articles");

            Articles[] articles = new Articles[loadDataSet.Tables["Articles"].Rows.Count];

            for (int i = 0; i < loadDataSet.Tables["Articles"].Rows.Count; i++)
            {
                articles[i] = new Articles()
                {
                    categoryID = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[i]["categoryID"]),
                    ID = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[i]["ID"]),
                    publishtime = Convert.ToDateTime(loadDataSet.Tables["Articles"].Rows[i]["publishtime"]),
                    readtimes = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[i]["readtimes"]),
                    title = loadDataSet.Tables["Articles"].Rows[i]["title"].ToString(),
                    writerID = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[i]["writerID"]),
                    content = loadDataSet.Tables["Articles"].Rows[i]["content"].ToString(),
                    description = loadDataSet.Tables["Articles"].Rows[i]["description"].ToString()
                };
            }
            return articles;
        }
Ejemplo n.º 2
0
        public Articles loadArticleByArticleID(int articleID)
        {
            SqlConnection loadConnection = new SqlConnection("server=tf-PC\\SQLEXPRESS;database=blogData;uid=admin;pwd=s1y2x3");

            loadConnection.Open();

            string loadArticleString = "SELECT * FROM Articles WHERE ID=@ArticleID";

            SqlCommand loadCommand1 = new SqlCommand(loadArticleString, loadConnection);
            loadCommand1.Parameters.Add(new SqlParameter("ArticleID", SqlDbType.Int));
            loadCommand1.Parameters["ArticleID"].Value = articleID;
            SqlDataAdapter loadAdapter = new SqlDataAdapter(loadCommand1);
            DataSet loadDataSet = new DataSet();
            loadAdapter.Fill(loadDataSet, "Articles");

            Articles article = new Articles()
            {
                categoryID = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[0]["categoryID"]),
                ID = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[0]["ID"]),
                publishtime = Convert.ToDateTime(loadDataSet.Tables["Articles"].Rows[0]["publishtime"]),
                readtimes = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[0]["readtimes"]),
                title = loadDataSet.Tables["Articles"].Rows[0]["title"].ToString(),
                writerID = Convert.ToInt32(loadDataSet.Tables["Articles"].Rows[0]["writerID"]),
                content = loadDataSet.Tables["Articles"].Rows[0]["content"].ToString(),
                description = loadDataSet.Tables["Articles"].Rows[0]["description"].ToString()
            };

            return article;
        }