==> [FrmSqlConnection.aspx] 소스 및 디자인


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmSqlConnection.aspx.cs" Inherits="FrmSqlConnection" %>

 

<!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="btnConnection" runat="server" Text="연결"

            onclick="btnConnection_Click" />

        <br />

        <asp:Label ID="lblDisplay" runat="server" Text="" ForeColor="Red"></asp:Label>

   

    </div>

    </form>

</body>

</html>

 








=====================================================================================




==> [FrmSqlConnection.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; //[0]

 

public partial class FrmSqlConnection : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void btnConnection_Click(object sender, EventArgs e)

    {

        ////[1] SqlConnection 클래스의 인스턴스 생성

        //SqlConnection con = new SqlConnection();

       

        ////[2] ConnectionString 속성 지정

        //con.ConnectionString = "server=.;database=Market;uid=Market;pwd=6750440;";

 

        ////[3] Open() 메서드로 DB 접속

        //con.Open();

 

        ////[!] 필요한 처리 : CRUD

        //this.lblDisplay.Text = "DB에 접속되었습니다.";

 

        ////[4] Close() 메서드로 DB 접속 해제

        //con.Close();

 

 

 

        // 위의 주석처리한 코드와 똑같은 코드(방법만 다르게 했음)

        SqlConnection con = new SqlConnection(

            "Data Source=(local);Initial Catalog=Market;" // 연결문자열을 잘못타이핑해서 나는 에러가 많으니 조심!

            + "User ID=Market;Password=6750440;"); // 생성자 사용

        con.Open();

        lblDisplay.Text = "연결 완료";

        con.Close();

    }

}

 

 

 

// SqlConnection 클래스 - SQL Server 데이터베이스에 대한 열린 연결을 나타냅니다. 이 클래스는 상속될 수 없습니다.

 

 


=====================================================================================




[실행결과]

--> 아래 그림의 웹페이지에서 "연결"버튼을 누르면, 레이블에 빨간글씨로 "연결 완료"라고 나온다.





'.NET프로그래밍 > ADO.NET' 카테고리의 다른 글

6. SqlDataReader 클래스  (0) 2009.09.24
5. SqlCommand 클래스  (0) 2009.09.24
4. SqlException 클래스  (0) 2009.09.24
3. SqlConnectionStringBuilder 클래스  (0) 2009.09.24
1. ADO.NET시작  (0) 2009.09.24
Posted by holland14
: