コード例 #1
0
        public static List <Uploads> GetLatestData()
        {
            List <Uploads> uploads = new List <Uploads>();
            // 1) Create the connection
            SqlConnection connection = new SqlConnection();

            connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;

            // 2) Open the connection
            connection.Open();

            // 3) Execute query
            SqlCommand command = new SqlCommand();

            command.Connection  = connection;
            command.CommandText = string.Format("select top 2 * from Uploads");

            SqlDataReader dataReader = command.ExecuteReader();

            while (dataReader.Read())
            {
                Uploads upload = new Uploads();
                upload.Id            = Convert.ToInt32(dataReader["Id"]);
                upload.Title         = Convert.ToString(dataReader["Title"]);
                upload.Path          = Convert.ToString(dataReader["Path"]);
                upload.UploadsTypeId = Convert.ToInt32(dataReader["UploadsTypeId"]);
                upload.TopicsId      = Convert.ToInt32(dataReader["TopicsId"]);

                if (dataReader["UserName"] != DBNull.Value)
                {
                    upload.UserName = Convert.ToString(dataReader["UserName"]);
                }

                upload.ThumbNail   = Convert.ToString(dataReader["ThumbNail"]);
                upload.UploadsType = UploadsTypeDA.GetSingle(upload.UploadsTypeId);
                upload.Topics      = TopicsDA.GetSingle(upload.TopicsId);
                upload.IsApproved  = Convert.ToBoolean(dataReader["IsApproved"]);
                upload.CreatedOn   = Convert.ToString(dataReader["CreatedOn"]);
                upload.IsDeleted   = Convert.ToBoolean(dataReader["IsDeleted"]);

                uploads.Add(upload);
            }
            // 4) Close the connection
            connection.Close();


            return(uploads);
        }
コード例 #2
0
ファイル: NewsDA.cs プロジェクト: 0code0/ASP.NET-C-sharp
        public static List <News> GetAllByCategory(int CategoryId)
        {
            List <News> newss = new List <News>();
            // 1) Create the connection
            SqlConnection connection = new SqlConnection();

            connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;

            // 2) Open the connection
            connection.Open();

            // 3) Execute query
            SqlCommand command = new SqlCommand();

            command.Connection  = connection;
            command.CommandText = string.Format("select * from News where categoryid='{0}'", CategoryId);

            SqlDataReader dataReader = command.ExecuteReader();

            while (dataReader.Read())
            {
                News news = new News();
                news.Id           = Convert.ToInt32(dataReader["Id"]);
                news.CategoryId   = Convert.ToInt32(dataReader["CategoryId"]);
                news.UploadTypeId = Convert.ToInt32(dataReader["UploadTypeId"]);
                news.TextArea     = Convert.ToString(dataReader["TextArea"]);
                news.ImagePath    = Convert.ToString(dataReader["ImagePath"]);
                news.HeadLines    = Convert.ToString(dataReader["HeadLines"]);
                news.NewsCategory = NewsCategoryDA.GetSingle(news.CategoryId);
                news.UploadsType  = UploadsTypeDA.GetSingle(news.UploadTypeId);
                if (dataReader["CreatedOn"] != DBNull.Value)
                {
                    news.CreatedOn = Convert.ToString(dataReader["CreatedOn"]);
                }
                news.IsDeleted = Convert.ToBoolean(dataReader["IsDeleted"]);


                newss.Add(news);
            }
            // 4) Close the connection
            connection.Close();


            return(newss);
        }