Blog Of Sem: VISUAL STUDIO C# SQL SERVER CONNECTION HELPER

VISUAL STUDIO C# SQL SERVER CONNECTION HELPER

using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace BMS_PDKS
{
    public class SQLHELPER
    {
        string baglanticumlesi = string.Format("Server={0}; Database={1}; User Id ={2};Password ={3}", CFG_TEMPLATE.CONFIG.LGDBSERVER, CFG_TEMPLATE.CONFIG.LGDBDATABASE, CFG_TEMPLATE.CONFIG.LGDBUSERNAME, CFG_TEMPLATE.CONFIG.LGDBPASSWORD);
        SqlConnection con;
        SqlDataAdapter adtr;
        public SqlConnection baglanti()
        {
            con = new SqlConnection(baglanticumlesi);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            return con;
        }
        //public DataTable veri_getirme(string sqlcumle, DevExpress.XtraGrid.GridControl dtgrd)
        public DataTable GET_B_CAPIFIRM(GridControl dtgrdc,GridView dtgrdv)
        {
            DataTable dt = new DataTable();
            dtgrdv.Columns.Clear();
            try
            {
                using (adtr = new SqlDataAdapter("select * from B_CAPIFIRM", baglanti()))
                { adtr.Fill(dt); dtgrdc.DataSource = dt; }
            }
            catch (System.Exception ex) { MessageBox.Show(ex.Message); }
            return dt;
        }
        public DataTable GET_B_CAPIUSER(GridControl dtgrdc, GridView dtgrdv)
        {
            DataTable dt = new DataTable();
            dtgrdv.Columns.Clear();
            try
            {
                using (adtr = new SqlDataAdapter("select * from B_CAPIUSER", baglanti()))
                { adtr.Fill(dt); dtgrdc.DataSource = dt; }
            }
            catch (System.Exception ex) { MessageBox.Show(ex.Message); }
            return dt;
        }

    }
}
--

DAHA SONRA ANA FORMDA DEVEXPRESS GRIDVIEW YARAT VE ŞU ŞEKİLDE ÇAĞIR:
 
        SQLHELPER sqlhelper = new SQLHELPER();

        private void button1_Click(object sender, EventArgs e)
        {
            sqlhelper.GET_B_CAPIFIRM( gridControl1,gridView1);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            sqlhelper.GET_B_CAPIUSER(gridControl1, gridView1);
        }