C# 连接数据库……sqlserver

来源:互联网 发布:淘宝如何查拍a发b 编辑:程序博客网 时间:2024/06/06 01:07
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace TestC
{
    /// <summary>
    /// DBAccess の概要の説明です。
    /// </summary>
    public class DBAccess
    {
        protected SqlConnection Conn;
        protected DataSet DS;
        protected SqlDataAdapter SDA;
        protected SqlCommand SC;
        protected SqlDataReader SDR;
        public DBAccess()
        {
            //
            // TODO: コンストラクタ ロジックをここに追加してください。
            //
        }
        public void Open()
        {
            try
            {
                //string ConnStr="";
                Conn=new SqlConnection(@"server=10.69.0.3/BKUPEXEC;uid=ntt;pwd=ntt;database=CallCenter");
                    //Conn=new SqlConnection(ConnStr);
                    Conn.Open();
            }
            catch(Exception e)
            {
                WriteMessage(e.Message.ToString(),true,true);

            }

        }
        public void Close()
        {
            try
            {
                // System.Data.ConnectionState
                //System.Data.ConnectionState
                if(Conn.State ==ConnectionState.Open  )
                    Conn.Close();
            }
            catch(Exception e)
            {
                WriteMessage(e.Message.ToString(),true,true);
            }

        }
 
        public void WriteMessage(string strMsg,bool Back,bool End)
        {   

                MessageBox.Show(strMsg);
        }
        public void Dispose()
        {
            if(DS!=null)
                DS.Dispose();
            if(SDA!=null)
                SDA.Dispose();
            if(SC!=null)
                SC.Dispose();
            if(SDR!=null)
            {
                if(!SDR.IsClosed)
                    SDR.Close();
            }
        } 
        public void Fill(string strSql)
        {
            try
            { 
                Open();
                SDA=new SqlDataAdapter(strSql,Conn);
                DS=new DataSet();
                SDA.Fill(DS);
            }    
            catch(Exception e)
            {
                WriteMessage(e.Message.ToString().Trim(),true,true);
            }
            finally
            {
                Close();
                Dispose();
            }
        }
        public void Fill(string TableName,string strSql)
        {
            try
            {
                Open();
                SDA=new SqlDataAdapter(strSql,Conn);
                DS=new DataSet();
                SDA.Fill(DS,TableName);
            }
            catch(Exception e)
            {
                WriteMessage(e.Message.ToString().Trim(),true,true);
            }
            finally
            {
                Close();
                Dispose();
            }
        }
        public DataSet getDataOut()
        {
            return DS;
        }
        //get one row
        public DataRow GetRecord(string strSql)
        {   
            DataRow       DR=null;
            try
            { 
                Open();
                SDA=new SqlDataAdapter(strSql,Conn);
                DS=new DataSet();
                SDA.Fill(DS);
                if(DS.Tables[0].Rows.Count>0)
                {
                    DR=DS.Tables[0].Rows[0];
                }
                return DR;
            }
            catch(SqlException e)
            {    
                WriteMessage(e.Message.ToString(),true,true);
                return DR;
            }
            finally
            { 
                Close();
                Dispose();
            }
        }
        //get row count
        public int GetRowCount(string strSql)
        {  
            Fill(strSql);
            try
            {   
                int count=DS.Tables[0].Rows.Count;
                return count;
            }
            catch
            {   
                return    0;
            }
            finally
            {   
                Close();
                Dispose();
            }  
        }
    }
}
 
原创粉丝点击