잠토의 잠망경

C_Sharp에서 Oracle 이용하기 본문

공부/C sharp

C_Sharp에서 Oracle 이용하기

잠수함토끼 2007. 11. 29. 21:53

아래 source는 오라클 사이트의 tutorial 을 보고 따라 해봤습니다.

//http://www.oracle.com/technology/sample_code/tech/windows/odpnet/howto/connect/index.html
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;


namespace Oracle04
{
    class MainApp
    {
        public static void Main()
        {
            OracleConnection con = new OracleConnection();
            con.ConnectionString = "Data Source= ORCL;" + " User ID=sysman;" + "Password=sksmscjswo; ";

            // OracleConnection con = new OracleConnection("Data Source= ORCL;" + " User ID=sysman;" + "Password=sksmscjswo; ");
            // 위에 있는 두 줄과 같은 문장이다.
            //  tansnames.ora 에 존재하는 ORCL의 service_name 을 참조하여 접속하고
            // longin을 하게 하는 설정 부분이다.

            try
            {
                con.Open(); // 실질적인 오라클 접속 부분이다.
                Console.WriteLine("connection to Oracle database");
                Console.WriteLine(" ");

                string cmdQuery = "select * from student"; // 오라클에서 내가 할 질의어이다.

                OracleCommand cmd = new OracleCommand(cmdQuery); // 오라클의 명령어 클래스를 만든다.
                cmd.Connection = con;    // 명령어 클래스에 이 명령어를 사용할 오라클 주소를 설정한다.
                cmd.CommandType = CommandType.Text; // 명령어의 타입을 정한다.

                OracleDataReader reader = cmd.ExecuteReader(); // 오라클에서 데이터를 읽오 올 수 있도록 클래스 생성

                while (reader.Read()) // 이부분을 통해서 읽어 오게 되고 읽어 오는게 없을 시 자도 끝나게 된다.
                {
                    Console.WriteLine(reader.GetString(0) +" "+ reader.GetString(1)+" "+reader.GetString(2) );
                    // name01         name02         name03       속성부분
                    // ___________________________________
                    // GetString(0) GetString(1)  GetString(2)  
                    //
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
               // cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
    }
}

Comments