방명록(GuestBook) 기본 코드
Asp.NET 2008. 10. 16. 22:07 |방명록 기본 코드
이름, 이메일, 컨텐츠, 비밀번호, 입력날짜, 입력 IP로 구성된 심플 방명록이다.
DB에 Insert 하는 것과 Select로 GridView에 추가 하는 내용이 포함되어 있다.
DB 쿼리 기본 내용
--방명록(GuestBook) 테이블 설계
Create Table GuestBook
(
Num Int Identity(1,1) Not Null Primary Key,
[Name] VarChar(25) Not Null,
Email VarChar(50) Null,
Content VarChar(400) Not Null,
[Password] VarChar(20) Not Null,
PostDate SmallDateTime Default(GetDate()),
PostIp VarChar(15) Not Null
)
GO
-- 입력 예시
Insert GuestBook Values('홍길동','g@g.com','방명록입니다.','1234',GetDate(),'127.0.0.1')
-- 출력 예시
Select * From GuestBook Order By Num Desc
-- 세부출력 예시
Select * From GuestBook Where Name = '홍길동'
-- 수정 예시
Begin Tran
Update GuestBook
Set Name = '백두산', Email = 'b@b.com'
Where Name = '홍길동' AND Email = 'g@g.com'
--RollBack Tran
Commit Tran
-- 삭제 예시
Delete GuestBook Where Name = '홍길동'
-- 검색 예시
Select * From GuestBook
Where Name Like '%두%' or Email Like '%g%'
cs 기본 파일
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;
public partial class GuestBook_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ReadData();
}
private void ReadData()
{
// 변수
string strSql =
"Select * From GuestBook Order By Num Desc";
// 커넥션 객체
SqlConnection objCon = new SqlConnection();
objCon.ConnectionString =
"Password=gsi7091;Persist Security Info=True;User ID=sa;Initial Catalog=MemoDB;Data Source=GSI\\SQLEXPRESS";
objCon.Open();
// 커멘드 객체
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
objCmd.CommandText = strSql;// "Insert GuestBook Values('홍길동','g@g.com','방명록입니다.','1234',GetDate(),'127.0.0.1')";
objCmd.CommandType = CommandType.Text;
SqlDataReader objDr = objCmd.ExecuteReader();
this.dgGuest.DataSource = objDr;
this.dgGuest.DataBind();
// 종료
//objCon.Close();
}
protected void btnWrite_Click(object sender, EventArgs e)
{
btnWrite.Enabled = false;
// 변수
string strName = this.txtName.Text;
string strEmail = this.txtEmail.Text;
string strContent = this.txtContent.Text;
string strPassword = this.txtPassword.Text;
string strPostIP = Request.UserHostAddress;
string strSql =
"Insert GuestBook Values('"
+ strName + "','"
+ strEmail + "','"
+ strContent + "','"
+ strPassword + "',GetDate(),'"
+ strPostIP + "')";
// 커넥션 객체
SqlConnection objCon = new SqlConnection();
objCon.ConnectionString =
"Password=gsi7091;Persist Security Info=True;User ID=sa;Initial Catalog=MemoDB;Data Source=GSI\\SQLEXPRESS";
objCon.Open();
// 커멘드 객체
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
objCmd.CommandText = strSql;// "Insert GuestBook Values('홍길동','g@g.com','방명록입니다.','1234',GetDate(),'127.0.0.1')";
objCmd.CommandType = CommandType.Text;
objCmd.ExecuteNonQuery();
// 종료
objCon.Close();
//ReadData();
Response.Redirect("./Default.aspx");
}
}