DotNetMemo.Database 프로젝트
SQL Server 데이터베이스 프로젝트를 만들고 관리하는 방법은 여러 단계를 포함합니다. 여기서는 DotNetMemo 솔루션에 DotNetMemo.Database
라는 이름의 SQL Server 데이터베이스 프로젝트를 만들고, 주소록 관련 테이블 및 저장 프로시저를 만드는 전체 절차를 다룹니다.
이 강의의 소스는 다음 경로에 있습니다.
https://github.com/VisualAcademy/DotNetMemo/tree/main/DotNetMemoCore
프로젝트 생성
- Visual Studio를 열고 "파일" > "새로 만들기" > "프로젝트"를 선택합니다.
- "SQL Server 데이터베이스 프로젝트"를 검색하고 선택한 후 "다음"을 클릭합니다.
- 프로젝트 이름을
DotNetMemo.Database
로 지정하고 위치를 설정한 다음 "만들기"를 클릭합니다.
테이블과 저장 프로시저 활용
본 섹션에서 소개하는 Address
테이블과 관련 저장 프로시저들은, SQL Server를 이용하여 주소록 애플리케이션 데이터베이스를 구축하고 CRUD 작업을 수행하는 방법을 설명하는 SQL Server에서 주소록 애플리케이션 DB 구축 및 CRUD 연습 문서에서 다룬 구문을 기반으로 합니다.
주소록(Address) 관련 테이블 생성
주소록 관련 테이블을 생성하기 위해, 다음 SQL 구문을 사용합니다. 파일 경로: DotNetMemoCore\DotNetMemo.Database\dbo\Tables\Address\Address.sql
-- 주소록 애플리케이션 DB
-- 주소록(Address) 테이블 생성
Create Table dbo.Address
(
Num Int Identity(1, 1) Primary Key Not Null, -- 번호
[Name] NVarChar(25) Not Null, -- 이름
Email NVarChar(100) Null, -- 이메일
Phone NVarChar(15) Null, -- 전화
Mobile NVarChar(15) Null, -- 휴대폰
ZipCode NVarChar(7) Not Null, -- 우편번호
Address NVarChar(150) Null, -- 주소
PostDate SmallDateTime Default(GetDate()) -- 등록일
)
Go
저장 프로시저 생성
주소록 관련 기능을 수행하기 위한 저장 프로시저를 생성합니다. 각각의 기능별로 저장 프로시저 파일을 만들고, 다음과 같이 SQL 구문을 작성합니다.
주소록 입력 (WriteAddress)
파일 경로: DotNetMemoCore\DotNetMemo.Database\dbo\Stored Procedures\Address\WriteAddress.sql
Create Procedure dbo.WriteAddress
(
@Name NVarChar(25),
@Email NVarChar(100),
@Phone NVarChar(15),
@Mobile NVarChar(15),
@ZipCode NVarChar(7),
@Address NVarChar(150)
)
As
Insert Address
(
Name,
Email,
Phone,
Mobile,
ZipCode,
Address,
PostDate
)
Values
(
@Name,
@Email,
@Phone,
@Mobile,
@ZipCode,
@Address,
GetDate()
)
Go
주소록 목록 조회 (ListAddress)
파일 경로: DotNetMemoCore\DotNetMemo.Database\dbo\Stored Procedures\Address\ListAddress.sql
Create Proc dbo.ListAddress
As
Select *
From Address
Order By Num Desc
Go
주소록 상세 조회 (ViewAddress)
파일 경로: DotNetMemoCore\DotNetMemo.Database\dbo\Stored Procedures\Address\ViewAddress.sql
Create Proc dbo.ViewAddress
@Num Int
As
Select *
From Address
Where Num = @Num
Go
주소록 수정 (ModifyAddress)
파일 경로: DotNetMemoCore\DotNetMemo.Database\dbo\Stored Procedures\Address\ModifyAddress.sql
Create Proc dbo.ModifyAddress
@Name NVarChar(25),
@Email NVarChar(100),
@Phone NVarChar(15),
@Mobile NVarChar(15),
@ZipCode NVarChar(7),
@Address NVarChar(150),
@Num Int
As
Begin Transaction
Update Address
Set
Name = @Name,
Email = @Email,
Phone = @Phone,
Mobile = @Mobile,
ZipCode = @ZipCode,
Address = @Address,
PostDate = GetDate()
Where
Num = @Num
Commit Tran
Go
주소록 삭제 (DeleteAddress)
파일 경로: DotNetMemoCore\DotNetMemo.Database\dbo\Stored Procedures\Address\DeleteAddress.sql
Create Procedure dbo.DeleteAddress
@Num Int
As
Begin Tran
Delete Address
Where Num = @Num
Commit Tran
Go
주소록 검색 (SearchAddress)
파일 경로: DotNetMemoCore\DotNetMemo.Database\dbo\Stored Procedures\Address\SearchAddress.sql
Create Proc dbo.SearchAddress
@SearchField NVarChar(25),
@SearchQuery NVarChar(25)
As
Declare @strSql NVarChar(150)
Set @strSql = '
Select *
From Address
Where ' + @SearchField + ' Like ''%' + @SearchQuery + '%'''
Exec (@strSql)
Go
각 저장 프로시저는 주소록 관련 애플리케이션에서 필요한 기본적인 데이터베이스 작업을 수행하도록 설계되었습니다. 이 문서는 프로젝트 생성부터 테이블 및 저장 프로시저의 생성까지 전체적인 과정을 담고 있습니다.