ADO.NET강의실

시삽: 레드플러스 님 
게시판 이동:
 제목 : 02.SQL서버의Pubs데이터베이스에연결하기.cs(C#)
글번호: 3
작성자: RedPlus
작성일: 2002/04/29 오전 3:04:00
조회수: 4117
파일: 02.SQL서버의Pubs데이터베이스에연결하기.jpg (20 KB) / 전송수: 2403
02.SQL서버의Pubs데이터베이스에연결하기.jpg
using System;
using System.Data;
using System.Data.SqlClient;

namespace SQL서버의Pubs데이터베이스에연결하기
{
    /// <summary>
    /// Class1에 대한 요약 설명입니다.
    /// </summary>
    class ConnectToDatabase
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [STAThread]

        static void Connect()
        {
            //[1] Connection 객체를 선언
            SqlConnection objConnection;
            
            //[2] Connection 객체를 생성
            objConnection = new SqlConnection();

            //[3] 연결 문자열 생성
            string strConnection = "server=localhost;database=pubs;uid=sa;pwd=";

            objConnection.ConnectionString = strConnection;

            //[4] Connection 객체의 Open 메서드를 사용해서 데이터베이스와의 연결을 열어준다.
                try
                {
                    Console.WriteLine("데이터베이스에 연결 중...");
                    objConnection.Open();
                    Console.WriteLine("데이터베이스에 연결 성공");

                    //Connection 객체가 열리면 데이터 작업을 수행한다.
                    Console.WriteLine("=================================");
                    Console.WriteLine("현재 데이터베이스 : " + objConnection.Database);
                    Console.WriteLine("현재 연결 상태 : " + objConnection.State);
                    Console.WriteLine("데이터베이스 버전 : " + objConnection.ServerVersion);
                    Console.WriteLine("=================================");

                    //데이터베이스를 변경합니다.
                    Console.WriteLine("데이터베이스를 변경합니다.");

                    //데이터베이스를 변경하려면 Connection 객체의 상태가
                    //열려있어야 하므로 열려있지 않은 경우는 열어주고
                    //데이터베이스를 변경합니다.
                    if(objConnection.State == ConnectionState.Closed)
                        objConnection.Open();

                    objConnection.ChangeDatabase("Pubs");
                    Console.WriteLine("현재 데이터베이스 : " + objConnection.Database );
                    Console.WriteLine("현재 연결 상태 : " + objConnection.State);
                    Console.WriteLine("=================================");
                }
                catch(Exception e)
                {
                    Console.WriteLine("데이터베이스에 연결 실패");
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }

                //[5] 해당 작업이 종료되었으면 Connection객체의 Close 메서드를
                //사용해서 데이터베이스와의 연결을 닫아준다.
                Console.WriteLine("데이터베이스에 연결 종료중...");
                objConnection.Close();
                Console.WriteLine("데이터베이스에 연결 종료 성공");
        }
        static void Main(string[] args)
        {
            Connect();
        }
    }
}
 
이전 글   다음 글 삭제 수정 답변 글쓰기 리스트

(댓글을 남기려면 로그인이 필요합니다.)

관련 아티클 리스트
  제       목 파일 작성자 작성일 조회
이전글 04.Access데이터베이스에연결하기.aspx(VB) 04.Access데이터베이스에연결하기.jpg(24 KB) RedPlus 2002-05-01 4399
현재글 02.SQL서버의Pubs데이터베이스에연결하기.cs(C#) 02.SQL서버의Pubs데이터베이스에연결하기.jpg(20 KB) RedPlus 2002-04-29 4117
다음글 01.SQL서버의Pubs데이터베이스에연결하기.aspx(VB) 01.SQL서버의Pubs데이터베이스에연결하기.jpg(27 KB) RedPlus 2002-04-29 4202
 
손님 사용자 Anonymous (손님)
로그인 Home