4. SqlException 클래스
.NET프로그래밍/ADO.NET 2009. 9. 24. 10:43 |
==> [FrmSqlException.aspx] 소스 및 디자인
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmSqlException.aspx.cs" Inherits="FrmSqlException" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnConnect" runat="server" Text="연결"
onclick="btnConnect_Click" />
<asp:Label ID="lblError" runat="server" Text="" ForeColor="Red"></asp:Label>
</div>
</form>
</body>
</html>
=====================================================================================
==> [FrmSqlException.aspx.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data; //[0]
public partial class FrmSqlException : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnConnect_Click(object sender, EventArgs e)
{
// 커넥션 스트링 자리를 SqlConnectionStringBuilder로 처리
SqlConnection con = new SqlConnection(
(new SqlConnectionStringBuilder("server=.;database=Market;uid=Market;pwd=675044;")).ConnectionString); // 잘못된 연결문자열 지정 후 에러 발생...
try // try ~ catch절을 통해 Error발생시키면서 정상종료 시킴.
{
con.Open();
lblError.Text = "연결완료";
}
catch (SqlException se) //[!] Sql관련 에러(메세지)를 담을 수 있는 그릇
{
lblError.Text = se.Message;
}
finally
{
if (con.State == ConnectionState.Open) // 연결이 되어있다면 연결종료 // ConnectionState -> "열거형"
{
con.Close(); // 에러가 나든 안나든 종료
}
}
}
}
// SqlException 클래스 - SQL Server 가 경고나 오류를 반환하면 throw되는 예외입니다. 이 클래스는 상속될 수 없습니다.
// ConnectionState 열거형 - 데이터 소스에 대한 현재 연결 상태를 설명합니다. 이 열거형에는 멤버 값를 비트로 조합할 수 있는 FlagsAttribute 특성이 있습니다.
=====================================================================================
[실행결과]
'.NET프로그래밍 > ADO.NET' 카테고리의 다른 글
6. SqlDataReader 클래스 (0) | 2009.09.24 |
---|---|
5. SqlCommand 클래스 (0) | 2009.09.24 |
3. SqlConnectionStringBuilder 클래스 (0) | 2009.09.24 |
2. SqlConnection 클래스 (0) | 2009.09.24 |
1. ADO.NET시작 (0) | 2009.09.24 |