난이도 : ![]()
![]()
![]()
![]()
(조금씩 어려워져요 ^^)
답변형게시판도 만들자 - 공통클래스 작성
우선, 답변형 게시판을 위해서 App_Code 폴더에,
Library.cs 와 Multiboard.cs를 만든다.
아래는 Multiboard.cs의 소스코드이다.
11 using System.Data.SqlClient;
12
13 public class Multiboard
14 {
15 // 게시물리스트에 사용하는 메서드 DataSet을 돌려준다.
16 public static DataSet SelectContent(string keyword, string value)
17 {
18 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
19 string mainQry = string.Empty, opQry = string.Empty;
20 string orQry = string.Empty;
21
22 opQry = "WHERE " + keyword + " LIKE '%" + value + "%' ";
23 orQry = "ORDER BY Refer DESC, Step ASC ";
24 mainQry = "SELECT " +
25 "BoardId, Writer, Title, Step, Depth, " +
26 "ReadCount, RegDate, DelFlag " +
27 "FROM t_MultiBoard " + opQry + orQry;
28
29 SqlCommand Cmd = new SqlCommand(mainQry, Conn);
30 SqlDataAdapter Adapt = new SqlDataAdapter(Cmd);
31 DataSet Ds = new DataSet();
32 Adapt.Fill(Ds, "t_MultiBoard");
33
34 return Ds;
35 }
36
37 // 게시물 상세보기 메서드
38 public static DataSet GetContent(string boardId)
39 {
40 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
41 string sqlSelQry = "SELECT " +
42 "Title, Writer, RegDate, ReadCount, Content " +
43 "FROM t_MultiBoard " +
44 "WHERE BoardId = @BoardID ";
45 SqlCommand Cmd = new SqlCommand(sqlSelQry, Conn);
46 Cmd.Parameters.Add("@BoardID", SqlDbType.Int).Value = Int32.Parse(boardId);
47 SqlDataAdapter Adapt = new SqlDataAdapter(Cmd);
48 DataSet Ds = new DataSet();
49 Adapt.Fill(Ds, "t_MultiBoard");
50 return Ds;
51 }
52
53 // 게시물 조회수 증가 메서드
54 public static void IncreaseReadCount(string boardId)
55 {
56 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
57 string strUpQry = "UPDATE t_MultiBoard SET " +
58 "ReadCount = ReadCount + 1 " +
59 "WHERE BoardId = @BoardID ";
60 SqlCommand Cmd = new SqlCommand(strUpQry, Conn);
61 Cmd.Parameters.Add("@BoardID", SqlDbType.Int).Value = Int32.Parse(boardId);
62 Conn.Open();
63 Cmd.ExecuteNonQuery();
64 Conn.Close();
65 }
66
67 // 작성자와 동일인인가를 확인하는 메서드
68 public static bool IsWriter(string boardId, string userId)
69 {
70 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
71 String strCntQry = "SELECT COUNT(*) AS cnt " +
72 "FROM t_MultiBoard " +
73 "WHERE BoardID = @BoardID " +
74 "AND Writer = @Writer ";
75 SqlCommand Cmd = new SqlCommand(strCntQry, Conn);
76 Cmd.Parameters.Add("@BoardID", SqlDbType.Int).Value = boardId;
77 Cmd.Parameters.Add("@Writer", SqlDbType.NVarChar).Value = userId;
78 Conn.Open();
79 SqlDataReader Read = Cmd.ExecuteReader();
80 int iCnt = 0;
81 if (Read.Read())
82 iCnt = Convert.ToInt32(Read["cnt"]);
83 Read.Close();
84 Conn.Close();
85
86 if (iCnt >= 1) return true;
87 else return false;
88 }
89
90 // 게시물 작성 메서드
91 public static void InsertContent(string writer, string title, string content)
92 {
93 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
94 string strInsQry = "INSERT INTO t_MultiBoard " +
95 "(Writer, Title, Content, " +
96 "Refer, Step, Depth, " +
97 "ReadCount, DelFlag, RegDate) " +
98 "VALUES " +
99 "(@Writer, @Title, @Content, " +
100 "" + GetNewRef() + ", 0, 0, " +
101 "0, 'N', GetDate()) ";
102 SqlCommand Cmd = new SqlCommand(strInsQry, Conn);
103 Cmd.Parameters.Add("@Writer", SqlDbType.NVarChar).Value = writer;
104 Cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = title;
105 Cmd.Parameters.Add("@Content", SqlDbType.NText).Value = content;
106 Conn.Open();
107 Cmd.ExecuteNonQuery();
108 Conn.Close();
109 }
110
111 private static string GetNewRef()
112 {
113 string strVal = string.Empty;
114 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
115 string strSelQry = "SELECT ISNULL(MAX(Refer), 0) + 1 AS MaxRef " +
116 "FROM t_MultiBoard ";
117 SqlCommand Cmd = new SqlCommand(strSelQry, Conn);
118 Conn.Open();
119 SqlDataReader Read = Cmd.ExecuteReader();
120 if (Read.Read())
121 {
122 strVal = Read["MaxRef"].ToString();
123 }
124 Read.Close();
125 Conn.Close();
126
127 return strVal;
128 }
129
130 // 게시물 수정 메서드
131 public static void UpdateContent(string boardId, string title, string content)
132 {
133 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
134 string strUpQry = "UPDATE t_MultiBoard SET " +
135 "Title = @Title, " +
136 "Content = @Content " +
137 "WHERE BoardId = @BoardId ";
138 SqlCommand Cmd = new SqlCommand(strUpQry, Conn);
139 Cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = title;
140 Cmd.Parameters.Add("@Content", SqlDbType.NText).Value = content;
141 Cmd.Parameters.Add("@BoardId", SqlDbType.Int).Value = Convert.ToInt32(boardId);
142
143 Conn.Open();
144 Cmd.ExecuteNonQuery();
145 Conn.Close();
146 }
147
148 // 답견게시물 삭제 메서드
149 public static void DeleteContent(string boardId)
150 {
151 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
152 // Refer, Step, Depth를 다 읽어온다.
153 string strSelQry = "SELECT Refer, Step, Depth " +
154 "FROM t_MultiBoard " +
155 "WHERE BoardId = @BoardId ";
156 SqlCommand Cmd = new SqlCommand(strSelQry, Conn);
157 Cmd.Parameters.Add("@BoardId", SqlDbType.Int).Value = Int32.Parse(boardId);
158
159 Conn.Open();
160 int refer = 0, step = 0, depth = 0;
161 SqlDataReader Read = Cmd.ExecuteReader();
162 if (Read.Read())
163 {
164 refer = Convert.ToInt32(Read["Refer"]);
165 step = Convert.ToInt32(Read["Step"]);
166 depth = Convert.ToInt32(Read["depth"]);
167 }
168 Read.Close();
169 // 내글을 삭제 및 내 하위의 글들 삭제
170 string strUpQry = "UPDATE t_MultiBoard SET " +
171 "Delflag = 'Y' " +
172 "WHERE BoardId = @BoardId; " +
173 "UPDATE t_MultiBoard SET " +
174 "Delflag = 'Y' " +
175 "WHERE Refer = @Refer " +
176 "AND Step > @Step " +
177 "AND Depth > @Depth ";
178 Cmd.CommandText = strUpQry;
179 Cmd.Parameters.Clear();
180 Cmd.Parameters.Add("@BoardId", SqlDbType.Int).Value = Int32.Parse(boardId);
181 Cmd.Parameters.Add("@Refer", SqlDbType.Int).Value = refer;
182 Cmd.Parameters.Add("@Step", SqlDbType.Int).Value = step;
183 Cmd.Parameters.Add("@Depth", SqlDbType.Int).Value = depth;
184 Cmd.ExecuteNonQuery();
185 Conn.Close();
186 }
187
188 // 게시물 답변 메서드
189 public static void ReplyContent(string boardId, string writer, string title, string content) {
190 SqlConnection Conn = newSqlConnection(ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
191 int refer = 0, step = 0, depth = 0;
192 // Refer, Step, Depth를 다 읽어온다.
193 string strSelQry = "SELECT Refer, Step, Depth " +
194 "FROM t_MultiBoard " +
195 "WHERE BoardId = @BoardId ";
196 SqlCommand Cmd = new SqlCommand(strSelQry, Conn);
197 Cmd.Parameters.Add("@BoardId", SqlDbType.Int).Value = Int32.Parse(boardId);
198
199 Conn.Open();
200 SqlDataReader Read = Cmd.ExecuteReader();
201 if (Read.Read())
202 {
203 refer = Convert.ToInt32(Read["Refer"]);
204 step = Convert.ToInt32(Read["Step"]);
205 depth = Convert.ToInt32(Read["depth"]);
206 }
207 Read.Close();
208 // 동일한 Refer를 가지는 게시물중 원본게시물 Step보다 큰 Step을 가지는 애들은 Step을 1씩 증가시킴.
209 string strUpQry = "UPDATE t_MultiBoard SET " +
210 "Step = Step + 1 " +
211 "WHERE Refer = @Refer " +
212 "AND Step > @Step ";
213 Cmd.CommandText = strUpQry;
214 Cmd.Parameters.Clear();
215 Cmd.Parameters.Add("@Refer", SqlDbType.Int).Value = refer;
216 Cmd.Parameters.Add("@Step", SqlDbType.Int).Value = step;
217 Cmd.ExecuteNonQuery();
218 // 답변게시물저장
219 string strInsQry = "INSERT INTO t_MultiBoard " +
220 "(Writer, Title, Content, " +
221 "Refer, Step, Depth, " +
222 "ReadCount, DelFlag, RegDate) " +
223 "VALUES " +
224 "(@Writer, @Title, @Content, " +
225 "@Refer, @Step, @Depth, " +
226 "0, 'N', GetDate()) ";
227 Cmd.CommandText = strInsQry;
228 Cmd.Parameters.Clear();
229 Cmd.Parameters.Add("@Writer", SqlDbType.NVarChar).Value = writer;
230 Cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = title;
231 Cmd.Parameters.Add("@Content", SqlDbType.NText).Value = content;
232 Cmd.Parameters.Add("@Refer", SqlDbType.Int).Value = refer;
233 Cmd.Parameters.Add("@Step", SqlDbType.Int).Value = (step + 1);
234 Cmd.Parameters.Add("@Depth", SqlDbType.Int).Value = (depth + 1);
235 Cmd.ExecuteNonQuery();
236 Conn.Close();
237 }
238
239 public static bool IsReply(string step)
240 {
241 if (step != "0") return true;
242 else return false;
243 }
244
245 public static string GetDepth(int depth)
246 {
247 string r = string.Empty;
248 for (int i = 0; i < depth; i++)
249 r += " ";
250 return r;
251 }
252
253 public static string GenTitle(string boardId, string title, string delFlag)
254 {
255 if (delFlag == "N")
256 return "<a href='MultiBoard_View.aspx?BoardID=" + boardId + "'>" + title + "</a>";
257 else
258 return "글이 삭제되었습니다";
259 }
260 }
꽤나 많은 소스를 다 적었다. 우선 다음으로 Library.cs 소스코드이다. 공통라이브러리는 메시지창이나 메시지창 후 페이지 이동하는 웹 사이트 어디에서나 쓸 수 있는 공통메서드들을 모아둔 클래스이다. 또한 static으로 작성하여 인스턴스를 생성할 필요도 없이 바로 사용할 수 있다.
11 public class Library
12 {
13 public static void Alert(HttpResponse response, String msg)
14 {
15 String script = "<script type='text/javascript'>alert('" +
16 msg + "');</script>";
17 response.Write(script);
18 }
19
20 public static void Alert(HttpResponse response, String msg, String strUrl)
21 {
22 String script = "<script type='text/javascript'>alert('" +
23 msg + "');location.href='" + strUrl + "';</script>";
24 response.Write(script);
25 }
26
27 public static string SubstrTitle(string title, int size)
28 {
29 return title.Length > size ? title.Substring(0, size) + "..." : title;
30 }
31
32 public static string ConvertDateWithHyphen(string date)
33 {
34 return date.Substring(0, 4) + "-" + date.Substring(4, 2) + "-" + date.Substring(6, 2);
35 }
36 }
Last edited on Feb 03, 2009 18:22 by 페르소나95
'.NET > ASP.NET' 카테고리의 다른 글
| Web Forms 컨트롤 ID 확인 (0) | 2009.02.23 |
|---|---|
| ASP.NET 웹 페이지 스스로 닫기 자바스크립트 에러날때 웹 페이지 닫기 버튼 어트리뷰 (0) | 2009.02.23 |
| ASP.NET 웹 페이지 간에 값 전달 <%@ PreviousPageType VirtualPath="~/AppvLine.aspx" %> (0) | 2009.02.18 |
| 13. 마지막 클래스를 dll로 변환하기 (0) | 2009.02.06 |
| ASP.NET FCK 에디터의 스크립트 함수 제거 하는로직 스크립트 제거 폼태그 제거 Fck Editor fck (0) | 2009.01.14 |
| ASP.NET FCKEdit 에디터 사용법 (0) | 2009.01.14 |
| ASP.NET 웹파트 적용 안될 때 (0) | 2009.01.14 |
| ASP.NET 웹파트 적용하기 (0) | 2009.01.14 |