A simple BBS demo including(CRUD) - 2
2. Create an ASP.NET website named "BBS".
3. Add a connection string in web.config as bellow.
<add name="BBSConnectionString" connectionString="Data Source=localhost;Initial Catalog=BBS;User ID=;Password="
providerName="System.Data.SqlClient" />
4. Create a class named "BLL" in the website. Ofcource it will be made in App_Code folder. Code is as bellow.
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for BLL
/// </summary>
public class BLL
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string writer;
public string Writer
{
get { return writer; }
set { writer = value; }
}
private DateTime writeDate;
public DateTime WriteDate
{
get { return writeDate; }
set { writeDate = value; }
}
private string content;
public string Content
{
get { return content; }
set { content = value; }
}
}
=========================================================================================
5. Create a class named "DAL" in the website. Ofcource it will be made in App_Code folder. Code is as bellow.
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
public void InseertData(BLL bbs)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
string cmdText = "insert BBS (Title,Writer,Content,WriteDate) values (@Title,@Writer,@Content,@WriteDate)";
con.Open();
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 200).Value = bbs.Title;
cmd.Parameters.Add("@Writer", SqlDbType.NVarChar, 50).Value = bbs.Writer;
cmd.Parameters.Add("@Content", SqlDbType.NVarChar, 3000).Value = bbs.Content;
cmd.Parameters.Add("@WriteDate", SqlDbType.DateTime).Value = Convert.ToDateTime(bbs.WriteDate);
cmd.ExecuteNonQuery();
con.Close();
}
public BLL GetDataByID(int id)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
string cmdText = "select * from BBS where id='" + id + "'";
SqlCommand cmd = new SqlCommand(cmdText, con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
BLL bbs = new BLL();
if (sdr.Read())
{
bbs.Title = Convert.ToString(sdr["Title"]);
bbs.Writer = Convert.ToString(sdr["Writer"]);
bbs.Content = Convert.ToString(sdr["Content"]);
bbs.WriteDate = Convert.ToDateTime(sdr["WriteDate"]);
}
sdr.Close();
con.Close();
return bbs;
}
public void DeleteDataByID(int id)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
string cmdText = "delete BBS where id='" + id + "'";
con.Open();
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.ExecuteNonQuery();
con.Close();
}
public void UpdateDataByID(BLL bbs, int id)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
string cmdText = "update bbs set Title=@Title,Writer=@Writer,WriteDate=@WriteDate,Content=@Content where id='" + id + "'";
con.Open();
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 200).Value = bbs.Title;
cmd.Parameters.Add("@Writer", SqlDbType.NVarChar, 50).Value = bbs.Writer;
cmd.Parameters.Add("@WriteDate", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@Content", SqlDbType.NVarChar, 3000).Value = bbs.Content;
cmd.ExecuteNonQuery();
con.Close();
}
public void CreateNumericPager(GridView gv, GridViewRow PagerRow)
{
//모든 컨트롤의 Visible 속성에 false를 설정하는 코드이다. 일단 모든 컨트롤들을 감춘후 코드에서 필요한 컨트롤만 다시 활성화 한다.
Control btn;
for (int i = 1; i <= 5; i++)
{
btn = PagerRow.Cells[0].FindControl("btnNum" + i);
btn.Visible = false;
}
Button btnPrevList = (Button)PagerRow.Cells[0].FindControl("btnPrevList");
btnPrevList.Visible = false;
Button btnFirst = (Button)PagerRow.Cells[0].FindControl("btnFirst");
btnFirst.Visible = false;
Literal lFirst = (Literal)PagerRow.Cells[0].FindControl("lFirst");
lFirst.Visible = false;
Literal lLast = (Literal)PagerRow.Cells[0].FindControl("lLast");
lLast.Visible = false;
Button btnLast = (Button)PagerRow.Cells[0].FindControl("btnLast");
btnLast.Visible = false;
Button btnNextList = (Button)PagerRow.Cells[0].FindControl("btnNextList");
btnNextList.Visible = false;
//현재 페이지와 총 페이지를 기준으로 페이지 이동 버튼의 CommandArgument에 설정할 변수들을 준비하는 코드.
int pageCount = gv.PageCount;
int pageIndex = gv.PageIndex + 1;
int pageButtonCount = gv.PagerSettings.PageButtonCount;
int lastPage = pageButtonCount;
if (pageCount < lastPage)
lastPage = pageCount;
int first = 1;
int last = lastPage;
if (pageIndex > last)
{
first = gv.PageIndex / pageButtonCount * pageButtonCount + 1;
last = first + pageButtonCount - 1;
if (last > pageCount)
last = pageCount;
if (last - first + 1 < pageButtonCount)
first = Math.Max(1, last - pageButtonCount + 1);
}
//First 버튼과 <이전4페이지> 버튼이 출력될 필요가 있다면 버튼의 Visible 속성에 true를 설정하고 <이전4페이지> 버튼의 CommandArgument
//속성에 이동할 페이지 번호를 설정한다.
if (first != 1)
{
btnPrevList.Visible = true;
int t = first - 1;
btnPrevList.CommandArgument = t.ToString();
btnPrevList.Text = "이전" + pageButtonCount + "페이지";
btnFirst.Visible = true;
lFirst.Visible = true;
}
//출력되여야 할 페이지 번호 버튼들의 Visible 속성에 true 를 설정하고 CommandArgument 속성에 이동할 페이지 번호를 설정한다.
//그리고 현재 선택된 페이지는 Enabled에 false를 설정해서 클릭할수 없게 만든다.
int count = 1;
for (int i = first; i <= last; i++, count++)
{
string str = i.ToString();
Button numButton = (Button)PagerRow.Cells[0].FindControl("btnNum" + count);
numButton.Visible = true;
if (i == pageIndex)
{
numButton.Text = str;
numButton.Enabled = false;
}
else
{
numButton.Text = str;
numButton.CommandArgument = str;
}
}
//Last 버튼과 <다음4페이지> 버튼이 출력될 필요가 있다면 버튼의 Visible속성에 true를 설정하고, <다음4페이지> 버튼의 CommandArgument 속성에
//이동할 페이지 번호를 설정한다.
if (pageCount > last)
{
btnNextList.Visible = true;
int t = last + 1;
btnNextList.CommandArgument = t.ToString();
btnNextList.Text = "다음" + pageButtonCount + "페이지";
btnLast.Visible = true;
lLast.Visible = true;
btnLast.Text = pageCount.ToString();
}
}
}