부부부부부 부부부부부 부부부 Department of Industrial Engineering at PNU. -1- Copyright © 2008 Chang-bong Kim 하하하하하하 ASP.NET & MS SQL ASP.NET & MS SQL Server Server 부부 : 부 부 부 부부 : 200893192 Review: 0 부부 : 2009.5.13 부부
부산대학교 산업공학과 대학원
Department of Industrial Engineering at PNU.-1-Copyright © 2008 Chang-bong Kim
하병현교수님
ASP.NET & MS SQL ASP.NET & MS SQL ServerServer
이름 : 김 창 봉 학번 : 200893192
Review: 0날짜 : 2009.5.13
표지
Department of Industrial Engineering at PNU.-2-Copyright © 2008 Chang-bong Kim
ContentsContents
Microsoft .NET Framework Microsoft Visual Studio .NET Tool
Visual Basic .NET Visual C++ C# .NET Etc…
Microsoft ASP.NET WebMatrix WebDeveloper
Microsoft .NET Basic Language Visual Basic .NET C# .NET
Department of Industrial Engineering at PNU.-3-Copyright © 2008 Chang-bong Kim
ContentsContents
ASP.NET with Visual Studio .NET *.asp -> *.aspx Web Form?
• Server Control
Add Code to Web Form Event Procedure Code Behind (*.aspx.vb / *.aspx.cs) Page Treatment Event Error Treatment (Tracing/Debugging) Validation Controls User Controls Database Connection ADO -> ADO.NET
• DataSet• DataReader• Access to Stored Procedure of SQL Server Using ADO.NET
Department of Industrial Engineering at PNU.-4-Copyright © 2008 Chang-bong Kim
ContentsContents
GuestBook & Web D/B Design D/B Create User to login D/B Design Web Form Coding Etc…
Department of Industrial Engineering at PNU.-5-Copyright © 2008 Chang-bong Kim
Introduction to .NET FrameworkIntroduction to .NET Framework.NET Framework?
.NET Framework .NET Building Block Services .NET Enterprise Servers Visual Studio.NET Windows.NET
Department of Industrial Engineering at PNU.-6-Copyright © 2008 Chang-bong Kim
Introduction to .NET FrameworkIntroduction to .NET FrameworkFramework, Languages and Development Tools
Department of Industrial Engineering at PNU.-7-Copyright © 2008 Chang-bong Kim
Introduction to .NET FrameworkIntroduction to .NET FrameworkAdvantages
Support to Web Standard HTTP/XML/SOAP/XSLT etc
Use any Language C# VB.NET C++ Etc (FORTRAM, COBOL…)
Easy to Develop Visual Studio .NET
Department of Industrial Engineering at PNU.-8-Copyright © 2008 Chang-bong Kim
Introduction to .NET FrameworkIntroduction to .NET FrameworkDevelopment Languages
C/C++
Internet
Web
GUI
Age of
PC
Large
Host
Env.
Web
Service
Department of Industrial Engineering at PNU.-9-Copyright © 2008 Chang-bong Kim
Introduction to .NET FrameworkIntroduction to .NET FrameworkEnterprise Environment based on .NET
Department of Industrial Engineering at PNU.-10-Copyright © 2008 Chang-bong Kim
Introduction to ASP.NETIntroduction to ASP.NETWhat’s ASP.NET?
ASP(Active Server Page)ASP.NET *.asp *.aspx Interpret Interpret + Compile
Windows Style Programming (Drag & Drop Controls)
Web Forms for ASP.NET Web Page
All of the .NET Language are useable for ASP.NET Web Application Program Visual Basic .NET , C#
Support XML Web Service (Information exchange in a distributed environment)
Department of Industrial Engineering at PNU.-11-Copyright © 2008 Chang-bong Kim
Introduction to ASP.NETIntroduction to ASP.NETASP.NET Web Application Program
Web Forms(web page) or .aspx page
Code Behind Page (old *.asp is Interpret language)
Web.config File (environment composition file)
Global.aspx File (application level event treatment) Web Services
The Core of ASP.NET Use Client Make-up Service
Caching Speed-up Web Page (after first request)
Database Connectivity
Department of Industrial Engineering at PNU.-12-Copyright © 2008 Chang-bong Kim
Introduction to ASP.NETIntroduction to ASP.NETASP.NET Execution Model
Interpret
Department of Industrial Engineering at PNU.-13-Copyright © 2008 Chang-bong Kim
Languages based on .NETLanguages based on .NETSupport Multi-Language on .NET
.NET Framework supports Multi-Language Microsoft Visual J#.NET Microsoft Jscript .NET COBOL Pascal C Etc
Advantages of Multi-Language Support Code Reuse One and same API access for all language of .NET Possible to use all of the .NET functions even though use one
language
Department of Industrial Engineering at PNU.-14-Copyright © 2008 Chang-bong Kim
Languages based on .NETLanguages based on .NETCLR(Common Languages Runtime)
All of the .NET Languages use just one Runtime (same as gate and layer)
Thread and Memory Management Solution for the DLL Version problems Components of the CLR
Class Loader (meta data) MSIL to Native Compilers (C#, C++ and VB Native
Compiler) Code Manager Garbage Collector (release class and data memory if not use) Security Engine Debug Engine Type Checker (check variable type) Exception Manager Thread Support COM marshaler .NET Framework Class Library Support
Department of Industrial Engineering at PNU.-15-Copyright © 2008 Chang-bong Kim
Languages based on .NETLanguages based on .NETCompile and Execution based on CLR
HTML
JIT Compiler
Department of Industrial Engineering at PNU.-16-Copyright © 2008 Chang-bong Kim
Languages based on .NETLanguages based on .NETWhat’s Namespace?
A collection for related to classes
To prevent naming such things
Use with Visual Basic.NET Import System.Data.SqlClient
Use with C# Using System.Data.SqlClient
Department of Industrial Engineering at PNU.-17-Copyright © 2008 Chang-bong Kim
Introduction to Visual Studio.NETIntroduction to Visual Studio.NETNecessity of the Visual Studio.NET
Use just one IDE (integrated development environment) for many language and project
Use many programming language for one PROJECT (ex, web site)
Use one solution for many project
Integration of web browser
Support debugging
Support user defined Interface VB, C# ..
Department of Industrial Engineering at PNU.-18-Copyright © 2008 Chang-bong Kim
Introduction to Visual Studio.NETIntroduction to Visual Studio.NETCreate First Page (demo)
New Project Visual C# Projcet (use ASP.NET template)
Check solution files & IIS (inetmgr)
Department of Industrial Engineering at PNU.-19-Copyright © 2008 Chang-bong Kim
Information of ASP.NETInformation of ASP.NETASP.NET Basic Skills
Client Script HTML CSS JavaScript
XSLT (Extensible Stylesheet Language Transformation)
ASP
Windows Xp/ .NET Skills
SQL Server
Department of Industrial Engineering at PNU.-20-Copyright © 2008 Chang-bong Kim
Information of ASP.NETInformation of ASP.NETRelated Site
www.asp.net
msdn.microsoft.com
www.gotdonet.com
www.devpia.com
Department of Industrial Engineering at PNU.-21-Copyright © 2008 Chang-bong Kim
Development of Web ApplicationDevelopment of Web ApplicationSteps for development of web application
Create Project
Design Interface
Coding
Test & Debugging
Release
Distribution
Web A
pplic
ati
on D
esi
gn
Department of Industrial Engineering at PNU.-22-Copyright © 2008 Chang-bong Kim Department of Industrial Engineering at PNU.-22-Copyright © 2008 Chang-Bong, Kim
GuestBook D/BGuestBook D/BLayout of GuestBook
Department of Industrial Engineering at PNU.-23-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BDesign GuestBook D/B
Create New DB (ASP_NET) Create User (ID: cbkim, Password: solution) Create Table
create table dbo.GuestBook(
Num Int Identity(1,1) Primary key not null, Name varchar(25) not null,Email Varchar(50) null,Content Text not null,PostDate DateTime Default GetDate(),PostIP VarChar(15) not null,Password varchar(20) not null
)
Insert DataInsert GuestBook(Name, Email, Content, PostIP, Password)Values('ChangBong', '[email protected]', 'Come back',
'127,12,1,1', '1234')
QuerySelect * From GuestBook
Department of Industrial Engineering at PNU.-24-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BUse Procedure
Create Procedure for Data saveCreate Procedure dbo.sp_GuestBookInsert
@Name VarChar(25), @Email varchar(50),@Content Text, @PostIp varchar(15), @Password varchar(20)
AsInsert GuestBook(Name, Email, Content, PostIP, Password)Values(@Name,@Email, @Content,@PostIP,@Password)
Department of Industrial Engineering at PNU.-25-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BUse Procedure
Create Procedure for Data saveCreate Procedure dbo.sp_GuestBookListAS
Select Num, Name, Email, Content, PostIP From GuestBook Order By Num Desc
Test Procedure1.sp_GuestBookList2.Exec sp_GuestBookList //use Exec command3.Execute sp_GuestBookList
Department of Industrial Engineering at PNU.-26-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BDesign Web Forms
Department of Industrial Engineering at PNU.-27-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BD/B Access using DataReader
Write Namespace using System.Data.SqlClient;
Write Page_Load Function if(!Page.IsPostBack){ ReadData(); //data output method }
Write DB access codeprivate void ReadData(){
SqlConnection objCon=new SqlConnection(); objCon.ConnectionString="server=(local); database=ASP_NET; User ID=cbkim; Password=solution";objCon.Open();
SqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon; objCmd.CommandText= "Select * From GuestBook Order By Num Desc";
SqlDataReader objDr=objCmd.ExecuteReader();
this.dgGuestBook.DataSource=objDr; this.dgGuestBook.DataBind(); objCon.Close();
}
Department of Industrial Engineering at PNU.-28-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BQuery Result in I/E
Department of Industrial Engineering at PNU.-29-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BD/B Access using DataSet
private void ReadData(){
SqlConnection objCon=new SqlConnection(); objCon.ConnectionString="server=(local); database=ASP_NET; User ID=cbkim;
Password=solution";objCon.Open();
SqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon; objCmd.CommandText= "Select * From GuestBook Order By Num
Desc";
// Use DataSetobjCmd.CommandType=CommandType.Text; SqlDataAdapter objDa=new SqlDataAdapter(); objDa.SelectCommand=objCmd; DataSet objDs=new DataSet(); objDa.Fill(objDs, "GuestBook"); this.dgGuestBook.DataSource=objDs; this.dgGuestBook.DataBind();
}
Department of Industrial Engineering at PNU.-30-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BD/B Access using Stored Procedure
private void ReadData(){
SqlConnection objCon=new SqlConnection(); objCon.ConnectionString="server=(local); database=ASP_NET; User ID=cbkim;
Password=solution";objCon.Open();
SqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon; objCmd.CommandText= "sp_GuestBookList"; objCmd.CommandType=CommandType.StoredProcedure;
SqlDataAdapter objDa=new SqlDataAdapter(); objDa.SelectCommand=objCmd; DataSet objDs=new DataSet(); objDa.Fill(objDs, "GuestBook"); this.dgGuestBook.DataSource=objDs; this.dgGuestBook.DataBind();
}
Department of Industrial Engineering at PNU.-31-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BD/B Access using Inline SQL
private void btnSubmit_Click(object sender, System.EventArgs e){
SqlConnection objCon = new SqlConnection(); objCon.ConnectionString="server=(local); database=ASP_NET; User ID=cbkim;
Password=solution";objCon.Open();
SqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon;
//" + txtName.Text +" objCmd.CommandText= "Insert GuestBook(Name, Email, Content, PostIP, Password) Values('" +
txtName.Text +"', '" + txtEmail.Text +"', '" + txtContent.Text +"', '" + Request.UserHostAddress +"', '" + txtPassword.Text +"')";
objCmd.CommandType= CommandType.Text;
objCmd.ExecuteNonQuery();
objCon.Close(); ReadData();
}
Double Submit Button and add following codes Not easy to read code
Department of Industrial Engineering at PNU.-32-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BThe Result of Input Data using Inline SQL
Department of Industrial Engineering at PNU.-33-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BAdd Validation Check Code
Department of Industrial Engineering at PNU.-34-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BUsing SQL Parameter for D/B Input
private void btnSubmit_Click(object sender, System.EventArgs e){
SqlConnection objCon = new SqlConnection(); objCon.ConnectionString=
"server=(local); database=ASP_NET; User ID=cbkim; Password=solution";objCon.Open();
SqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon;
objCmd.CommandText="Insert GuestBook(Name, Email, Content, PostIP, Password) Values(@Name,@Email, @Content,@PostIP,@Password)";
objCmd.CommandType= CommandType.Text;
objCmd.Parameters.Add("@Name", SqlDbType.VarChar, 25); objCmd.Parameters.Add("@Email", SqlDbType.VarChar, 25); objCmd.Parameters.Add("@Content", SqlDbType.Text); objCmd.Parameters.Add("@PostIP", SqlDbType.VarChar, 15); objCmd.Parameters.Add("@Password", SqlDbType.VarChar, 20);
objCmd.Parameters["@Name"].Value=this.txtName.Text; objCmd.Parameters["@Email"].Value=this.txtEmail.Text; objCmd.Parameters["@Content"].Value=this.txtContent.Text; objCmd.Parameters["@PostIP"].Value=Request.UserHostAddress; objCmd.Parameters["@Password"].Value=this.txtPassword.Text;
objCmd.ExecuteNonQuery();
objCon.Close(); ReadData();
}
Department of Industrial Engineering at PNU.-35-Copyright © 2008 Chang-bong Kim Department of Industrial Engineering at PNU.-35-Copyright © 2008 Chang-Bong, Kim
GuestBook D/BGuestBook D/BUsing Stored Procedure for D/B Input
private void btnSubmit_Click(object sender, System.EventArgs e){
SqlConnection objCon = new SqlConnection(); objCon.ConnectionString=
"server=(local); database=ASP_NET; User ID=cbkim; Password=solution";objCon.Open();
SqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon;
objCmd.CommandText= "sp_GuestBookInsert"; objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.Add("@Name", SqlDbType.VarChar, 25); objCmd.Parameters.Add("@Email", SqlDbType.VarChar, 25); objCmd.Parameters.Add("@Content", SqlDbType.Text); objCmd.Parameters.Add("@PostIP", SqlDbType.VarChar, 15); objCmd.Parameters.Add("@Password", SqlDbType.VarChar, 20);
objCmd.Parameters["@Name"].Value=this.txtName.Text; objCmd.Parameters["@Email"].Value=this.txtEmail.Text; objCmd.Parameters["@Content"].Value=this.txtContent.Text.Replace("\r\n", "<br>");objCmd.Parameters["@PostIP"].Value=Request.UserHostAddress; objCmd.Parameters["@Password"].Value=this.txtPassword.Text;
objCmd.ExecuteNonQuery();
objCon.Close(); ReadData();
}
Department of Industrial Engineering at PNU.-36-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BAdd Page Index to GuestBook
Change Attributes AllowPaging=True PageSize=5 Mode=NumaricPages (In PagerStyle)
Add Event “PageIndexChanged”
Add Codeprivate void dgGuestBook_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e){
this.dgGuestBook.CurrentPageIndex=e.NewPageIndex; ReadData();
}
Department of Industrial Engineering at PNU.-37-Copyright © 2008 Chang-bong Kim
GuestBook D/BGuestBook D/BResult of the Page Index
Department of Industrial Engineering at PNU.-38-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BConcept
Write.aspxWrite.aspx
Delete.aspxDelete.aspx
Mosidy.aspxMosidy.aspx
MS SQL Server 2005
MS SQL Server 2005
List.aspxList.aspx
View.aspxView.aspx
Department of Industrial Engineering at PNU.-39-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BDesign Web D/B
Create New DB (web) Create User (ID: web, Password: web12345) Create Table
Create Table dbo.Basic(
Num Int Identity(1,1) Primary key not null, Name varchar(25) not null,Email Varchar(100) null,Title varchar(150) not null,PostDate DateTime Default GetDate() not null,PostIP VarChar(15) not null,Content Text not null,Password varchar(20) not null,ReadCount Int Default 0, Encoding varchar(10) not null,Homepage varchar(100) null, ModifyData Datetime null, MidifyIP varchar(15) null
)
Department of Industrial Engineering at PNU.-40-Copyright © 2008 Chang-bong Kim
Insert Data• Insert Basic (Name, Email, Title, PostIP, Content, Password, Encoding,
Homepage)• Values ('kim', '[email protected]', 'test kim', '123.123.13.1', 'context',
'1234', 'text','http://pusan.ac.kr');
Query TestSelect * From Basic
Web D/BWeb D/BDesign Web D/B
Department of Industrial Engineering at PNU.-41-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BDesign Web Form
Department of Industrial Engineering at PNU.-42-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BWrite Source Code – Write.aspx
Add Namespace using System.Data.SqlClient;
Add to Page_Load Functionif(!Page.IsPostBack)
{ArrayList ar=new ArrayList(); ar.Add("Text"); ar.Add("Html"); lstEncoding.DataSource=ar; lstEncoding.DataBind();
}
Department of Industrial Engineering at PNU.-43-Copyright © 2008 Chang-bong Kim Department of Industrial Engineering at PNU.-43-Copyright © 2008 Chang-Bong, Kim
private void btnSave_Click(object sender, System.EventArgs e){
SqlConnection objCon=new SqlConnection(); objCon.ConnectionString=
"server=(local); database=Web; User ID=web; Password=web12345";objCon.Open();
SqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon;
//Inline SQL : " + variable +"objCmd.CommandText= "Insert Basic (Name, Email, Title, PostIP, Content,
Password, Encoding, Homepage) Values ('" + txtName.Text + "', '" + txtEmail.Text + "', '" + txtTitle.Text + "', '" + Request.UserHostAddress + "', '" + this.txtContent.Text +"', '" + txtPassword.Text +"', '" + this.lstEncoding.SelectedItem.Value +"', '" + txtHomepage.Text +"'); "; //Insert command
objCmd.CommandType=CommandType.Text; objCmd.ExecuteNonQuery(); objCon.Close();
//Response.Redirect("./List.aspx");
}
Web D/BWeb D/BWrite Source Code – Write.aspx
Department of Industrial Engineering at PNU.-44-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BAdd Data Grid
Department of Industrial Engineering at PNU.-45-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BWrite Source Code-List.aspx
private void Page_Load(object sender, System.EventArgs e){
if(!Page.IsPostBack){
ReadData(); //data output method}
}
private void ReadData(){
//connectionSqlConnection objCon=new SqlConnection(); objCon.ConnectionString=
"server=(local); database=Web; User ID=web; Password=web12345";objCon.Open();
//commandSqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon; objCmd.CommandText= "Select * From Basic Order By Num Desc"; objCmd.CommandType=CommandType.Text;
//Data Adapter + Data SetSqlDataAdapter objDa=new SqlDataAdapter(); objDa.SelectCommand=objCmd; DataSet objDs=new DataSet(); objDa.Fill(objDs, "Basic");
//outputthis.DataGrid1.DataSource=objDs.Tables["Basic"].DefaultView;this.DataGrid1.DataBind();
}
Department of Industrial Engineering at PNU.-46-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BWrite Source Code-List.aspx
private void btnWrite_Click(object sender, System.EventArgs e)
{Response.Redirect("./Write.aspx");
}
Department of Industrial Engineering at PNU.-47-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BModify Html Code for List Link-List.aspx
.
.
.
BackColor="White" CellPadding="4" AutoGenerateColumns="False">
<Columns><asp:HyperLinkColumn DataNavigateUrlField="Num" DataNavigateUrlFormatString="View.aspx?Num={0}" DataTextField="Title“ HeaderText="Title"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="Name" HeaderText="Writer"></asp:BoundColumn><asp:BoundColumn DataField="PostDate" HeaderText="Date" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>
<asp:BoundColumn DataField="ReadCount" HeaderText="Counter"></asp:BoundColumn></Columns>
Department of Industrial Engineering at PNU.-48-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BAdd code to View.aspx
using System.Data.SqlClient;
protected string strNum="";
private void Page_Load(object sender, System.EventArgs e){
strNum=Request.QueryString["Num"]; //index valueif(strNum==null){
Response.Redirect("./List.aspx"); }else{
if(!Page.IsPostBack)ReadData();
}}
private void btnList_Click(object sender, System.EventArgs e) {
Response.Redirect("./List.aspx"); }
Department of Industrial Engineering at PNU.-49-Copyright © 2008 Chang-bong Kim
private void ReadData(){
//connectionSqlConnection objCon=new SqlConnection(); objCon.ConnectionString=
"server=(local); database=Web; User ID=web; Password=web12345";objCon.Open();
//commandSqlCommand objCmd = new SqlCommand(); objCmd.Connection=objCon;
//add read countobjCmd.CommandText="Update Basic Set ReadCount=ReadCount+1 Where Num="+strNum;objCmd.CommandType=CommandType.Text; objCmd.ExecuteNonQuery();
//Query for readobjCmd.CommandText= "Select * From Basic Where Num=" + strNum; objCmd.CommandType=CommandType.Text;
//data readerSqlDataReader objDr=objCmd.ExecuteReader();
//outputif(objDr.Read()){
this.lblTitle.Text=objDr["Title"].ToString(); this.lblNum.Text=objDr["Num"].ToString(); this.lblName.Text=objDr["Name"].ToString(); this.lblEmail.Text=objDr["Email"].ToString(); this.lblPostDate.Text=objDr["PostDate"].ToString(); this.lblReadCount.Text=objDr["ReadCount"].ToString(); this.lblContent.Text=objDr["Content"].ToString().Replace("\r\n", "<br>");
}
//closeobjDr.Close(); objCon.Close(); objCon=null;
}
Web D/BWeb D/BAdd code to View.aspx
Department of Industrial Engineering at PNU.-50-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BThe result of View.aspx
List.aspxList.aspx
View.aspxView.aspx
Department of Industrial Engineering at PNU.-51-Copyright © 2008 Chang-bong Kim
Web D/BWeb D/BDemo…
List.aspx Write.aspx View.aspx Modify.aspx Delete.aspx Search.aspx