using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe; // SQL Server Compact 3.5 사용시 추가하시오

namespace Genealogy
{
    public class DB_Management
    {
        #region DB 전역변수 선언
        private string connentionString = @"Data Source=C:\Documents and Settings\Administrator\My Documents\DB.SDF;" +
                "Password=1234;";

        private SqlCeConnection con = null;
        private SqlCeCommand cmd = null;
        private SqlCeTransaction trn = null;

        public Boolean ExecuteQueryFlag;
        #endregion
       
        #region 생성자(초기화
        public DB_Management()
        {
            // 데이터베이스 연결
            SqlCeConnection con = new SqlCeConnection(connentionString);
            // 데이터베이스 커맨드 생성
            SqlCeCommand cmd = new SqlCeCommand();
            // 트랜잭션 생성
            SqlCeTransaction trn = con.BeginTransaction();
            ExecuteQueryFlag = false;
        }
        #endregion

        #region DB 연결
        public void DB_Connection()
        {
            con.Open();
        }
        #endregion

        #region Command Connection
        private void CommConnection(string Commandtext)
        {
            // 커맨드에 커넥션을 연결
            cmd.Connection = con;

            cmd.Transaction = trn;

            // 쿼리 생성 : Insert 쿼리
            cmd.CommandText = Commandtext;

            // 쿼리 실행
            cmd.ExecuteNonQuery();
        }
        #endregion
      
        #region Insert
        public void DB_Record_Insert(string TableID, string RecordName)
        {
            string InsertRecordName = "INSERT INTO" + TableID + "VALUES('" +
                                      RecordName + "')";
           
            CommConnection(InsertRecordName);

            if(ExecuteQueryFlag == true)
            {
                // 커밋
                trn.Commit();
            }
            else
            {
                // 롤백
                trn.Rollback();
            }

            con.Close();
           
        }
        #endregion

        #region Select
        public void DB_Record_Select(int Indexnumber, params string[] args)
        {
            string SelectRecordName = string.Empty;
            
            if (Indexnumber < 0)
            {
                // select 쿼리로 변경
                SelectRecordName = "SELECT * FROM" + args[0];
            }
            con.Close();
        }
        #endregion
    }
}

비번은 마음에 드는걸로 바꾸면 될듯 하다.
commit과 rollback이 되기때문에 compact db를 자주 사용하는 편이다. 꽤 쓸만하다.

+ Recent posts