C# & ASP.NET

OLEDB 로 DBASE (.DBF) 연결하여 테이블 생성 및 SELCET 쿼리 날려 DataSet,DataTable로 가져오기

조심이 2009. 6. 30. 17:30
반응형

DBF 파일을 생성하여 사용할 일이 생겼다. OLEDB 로 연결하여 테이블(table)을 생성하고
데이터를 입력하고 나중에 데이터셋(DataSet) 이나 데이터테이블(DataTable)로 가져다 써야 한다.
간단하고 약간 허접한 느낌이 있지만 몇가지 예를 들어 놓았다 ^^

using System.Data;
using System.Data.OleDb; 

----------------------------------------------------------

 private static OleDbConnection m_pConn;  
 private static OleDbCommand Comm = null;


string path=경로(예:c:\)

//커넥션 스트링
string oleDbConnStr =
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @path+ ";Extended Properties=dBASE IV;";


   if( ConnectDB(oleDbConnStr )){
       
        string 생성쿼리="create table TEST(NAME TEXT(15), ..........)";   //varchar나 char 가 아닌 text 다 주의하자.
        ExcuteQuery(생성쿼리);

       string 삽입쿼리="insert into test(name) values('aaa')";
        ExcuteQuery(삽입쿼리);

       string 셀렉트쿼리 ="select * from test";
       DataSet ds = GetDataSet(셀렉트쿼리);

       DataTable = GetTable(셀렉트쿼리);

    DBClose();
  }else{
   //연결실패
   } 

---------------------------------- OLEDB 연결 ----------------------------------------
            public bool ConnectDB(String sConStr)
            {
                m_sConStr = sConStr;
                bool bResult = false;
                try
                {
                    m_pConn = new OleDbConnection(sConStr);
                    m_pConn.Open();
                    if (m_pConn.State == ConnectionState.Open)
                    {
                        bResult = true;
                    }
                    else
                    {
                      
                        bResult = false;
                    }
                }
                catch (Exception e)
                {
                    bResult = false;
                   MessageBox.Show(e.Message);     //예외 메세지             
                }

                return bResult;
            }
---------------------------------- OLEDB 연결 끊기----------------------------------------

  public void DBClose()
            {
                try
                {
                    m_pConn.Close();
                }
                catch (Exception e)
                {
                         MessageBox.Show(e.Message);     //예외 메세지                  }
            }



---------------------------------------실행..생성,수정,삭제,삽입 등------------------------------

            public int ExcuteQuery(string sQuery)
            {
                int iResultCnt = 0;  //반영된 행의 곗수
                try
                {
                   
                    OleDbCommand command = new OleDbCommand(sQuery);
                    command.Connection = m_pConn;
                    iResultCnt = command.ExecuteNonQuery();
                   
                }
                catch (Exception e)
                {
                    if ((m_pConn.State == ConnectionState.Closed) || (m_pConn.State == ConnectionState.Broken))
                    {
                        //디비 커넥션 연결 안됌
                    }
                    MessageBox.Show(e.Message);     //예외 메세지       
                }
           
                return iResultCnt;
            }

---------------------------셀렉트.....데이터 셋 DataSet--------------------------------------

  public DataSet GetDataSet(String sQuery)
            {
                DataSet pDataset = new DataSet();

                try
                {
                 
                    OleDbDataAdapter pAdapter = new OleDbDataAdapter();
                    pAdapter.SelectCommand = new OleDbCommand(sQuery, m_pConn);
                    pAdapter.Fill(pDataset);
                }
                catch (Exception e)
                {
                    if ((m_pConn.State == ConnectionState.Closed) || (m_pConn.State == ConnectionState.Broken))
                    {
                        //디비 커넥션 연결 안됌
                    }
                    MessageBox.Show(e.Message);     //예외 메세지       
                }
              
                return pDataset;
            }

------------------------------------셀렉트.....데이터 테이블 DataTable-----------------------------
         public DataTable GetTable(String sQuery)
            {
                DataTable pTable = new DataTable();
                try
                {
                  
                    OleDbDataAdapter pAdapter = new OleDbDataAdapter();
                    pAdapter.SelectCommand = new OleDbCommand(sQuery, m_pConn);
                    pAdapter.Fill(pTable);
                }
                catch (Exception e)
                {
                    if ((m_pConn.State == ConnectionState.Closed) || (m_pConn.State == ConnectionState.Broken))
                    {
                        MessageBox.Show("DB Disconnected. Please Connect DB!", "DB Disconnected");
                    }
                              
                      MessageBox.Show(e.Message);     //예외 메세지       

      }

             
                return pTable;
            }

반응형