ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C#에서 데이터베이스 접속시 사용되는 공통 클래스 소스
    예전 글들/.NET, C# 2010. 8. 12. 10:25
    반응형


    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;            // SQL 서버 사용하려면 필요.

    /// <summary>
    /// dbconn의 요약 설명입니다.
    /// </summary>
    public class DBConn
    {
        // SQL 서버 이름을 지정합니다.
        string source = @"Data Source = localhost\\sqlexpress; Initial Catalog=data; Integrated Security=True";
        SqlConnection conn;

        // 데이터베이스 열기
        public void Open()
        {
            conn = new SqlConnection(source);
            conn.Open();
        }

        // 데이터베이스 닫기
        public void Close()
        {
            conn.Close();
        }

        // SQL 문을 실행
        public void ExecuteSQL(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
        }

        // SQL 문을 실행하고, SqlDataReader 객체를 리턴
        public SqlDataReader ExecuteReader(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            return cmd.ExecuteReader();
        }

        // SQL 문을 실행하고, DataSet 객체를 리턴합니다.
        // 다른 테이블을 가져올 경우는 아래와 같은 함수를 하나 더 만들면 됨
        public DataSet GetUserList()
        {
            string sql = "select * from member";

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(sql, conn);

            DataSet ds = new DataSet();
            adapter.Fill(ds);
            // binding은 없음
            return ds;
        }

        public bool IsRegistered(string userid)
        {
            return true;
        }

        public DataSet GetDataSet(string sql)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(sql, conn);

            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }

        //public string Autherticate(string userid, string password)
        //{
        //    SqlCommand cmd = new SqlCommand("sp_UserLogin", conn);

        //    // Mark the Command as a SPROC
        //    cmd.CommandType = CommandType.StoredProcedure;

        //    SqlParameter param;

        //    // Add Parameters to SPROC
        //    param = new S
        //}

        public DBConn()
        {

        }
    }

    반응형

    댓글

Designed by Tistory.