Create DBHepler Common Function

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace DBHelper {  
  6.     class AllFiledProperty {  
  7.         public string Name {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Address {  
  12.             get;  
  13.             set;  
  14.         }  
  15.     }  
  16. }   
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.SqlClient;  
  6. using System.Data;  
  7. namespace DBHelper {  
  8.     class DBHepler //Abstract Class Not For Object Create Only Inherited Class  
  9.     {  
  10.         //Golobal Veriable  
  11.         SqlConnection con; //Object SqlConntion Con  
  12.         int result = 0; //  
  13.         //Class Name Same Sql Connection  
  14.  
  15.         #  
  16.         region SqlConnection  
  17.         public DBHepler() {  
  18.             con = new SqlConnection("Sql Connection string");  
  19.         }#  
  20.         endregion  
  21.  
  22.         # region Create a Paramter(Insert Update Delete)  
  23.         protected SqlParameter CreateParamter(string _paramterName, DbType _dbtype, object _value) {  
  24.             SqlParameter paramter = new SqlParameter();  
  25.             paramter.ParameterName = _paramterName;  
  26.             paramter.DbType = _dbtype;  
  27.             paramter.Value = _value;  
  28.             return paramter;  
  29.         }#  
  30.         endregion  
  31.  
  32.         # region Insert Update Delete Without Paramter  
  33.         //Insert Update Delete Common Method Without Paramter  
  34.         // _SqlQuery Exmp Sqlquery And Storeprocedure Name  
  35.         protected int InsertUpdateDelete(string _SqlQuery, CommandType _commandtype) {  
  36.             try {  
  37.                 SqlCommand cmd = new SqlCommand();  
  38.                 cmd.CommandText = _SqlQuery;  
  39.                 cmd.CommandType = _commandtype;  
  40.                 cmd.Connection = con;  
  41.                 if (con.State != ConnectionState.Open) {  
  42.                     con.Open();  
  43.                 }  
  44.                 return result = cmd.ExecuteNonQuery();  
  45.             } catch (Exception) {  
  46.                 return 0;  
  47.             }  
  48.         }#  
  49.         endregion  
  50.  
  51.         # region Insert Update Delete With Paramter  
  52.         //Method Name Same Without Paramter And Paramter Same Method  
  53.         protected int InsertUpdateDelete(string _SqlQuery, CommandType _commandtype, params SqlParameter[] paramter) {  
  54.             try {  
  55.                 SqlCommand cmd = new SqlCommand();  
  56.                 cmd.CommandText = _SqlQuery;  
  57.                 cmd.CommandType = _commandtype;  
  58.                 cmd.Connection = con;  
  59.                 cmd.Parameters.Clear();  
  60.                 foreach(var item in paramter) {  
  61.                     cmd.Parameters.Add(item);  
  62.                 }  
  63.                 if (con.State != ConnectionState.Open) {  
  64.                     con.Open();  
  65.                 }  
  66.                 return result = cmd.ExecuteNonQuery();  
  67.             } catch (Exception) {  
  68.                 return 0;  
  69.             }  
  70.         }#  
  71.         endregion  
  72.  
  73.         # region Datatable Return GridView Baind Dropdown Bind  
  74.         //return Datatable  
  75.         protected DataTable SelectData(string _SqlQuery, CommandType _commandtype) {  
  76.                 DataTable dt = new DataTable();  
  77.                 SqlDataAdapter da = new SqlDataAdapter();  
  78.                 SqlCommand cmd = new SqlCommand();  
  79.                 try {  
  80.                     cmd.CommandText = _SqlQuery;  
  81.                     cmd.CommandType = _commandtype;  
  82.                     cmd.Connection = con;  
  83.                     da.SelectCommand = cmd;  
  84.                     da.Fill(dt);  
  85.                     return dt;  
  86.                 } catch (Exception) {  
  87.                     throw;  
  88.                 } finally {  
  89.                     dt.Dispose();  
  90.                     da.Dispose();  
  91.                 }#  
  92.                 endregion  
  93.   
  94.             }  
  95.             //Create a New Class BAL  
  96.             //How To Use DBHelper Class  
  97.     }  
  98. }  
BAL Class
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. namespace DBHelper {  
  7.     //inherited Class  
  8.     class BAL: DBHepler {  
  9.         int result = 0;  
  10.         //Insert Without Paramter  
  11.         public int InsertData() {  
  12.             //If Sp Name Pass Then Use CommandType.StoredProcedure  
  13.             //If Sql Query Pass Then Use CommandType.Text  
  14.             result = InsertUpdateDelete("Sp_Name And Sql Query", CommandType.StoredProcedure);  
  15.             //If result Value 1 Insert Data Successfully  
  16.             return result;  
  17.             //If result 0 Insert Failed  
  18.         }  
  19.   
  20.         //Insert Data Paramter  
  21.         public int InsertNewData(AllFiledProperty propertyValue) {  
  22.             //If Sp Name Pass Then Use CommandType.StoredProcedure  
  23.             //If Sql Query Pass Then Use CommandType.Text  
  24.             result = InsertUpdateDelete("Sp_Name And Sql Query", CommandType.StoredProcedure,  
  25.                 CreateParamter("@ParamterName", DbType.String, propertyValue.Name),  
  26.                 CreateParamter("@ParamterName2", DbType.String, propertyValue.Address));  
  27.             //If result Value 1 Insert Data Successfully  
  28.             return result;  
  29.             //If result 0 Insert Failed  
  30.         }  
  31.   
  32.         public DataTable GetAllData() {  
  33.             //If Sp Name Pass Then Use CommandType.StoredProcedure  
  34.             //If Sql Query Pass Then Use CommandType.Text  
  35.             DataTable dt = SelectData("SqlQuery And Sp Name", CommandType.Text);  
  36.             return dt;  
  37.         }  
  38.     }  
  39. }  
My Windows Application Use Button Click Then Save Data 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace DBHelper {  
  10.     public partial class Form1: Form {  
  11.         public Form1() {  
  12.                 InitializeComponent();  
  13.             }  
  14.             //Golobal Veriable  
  15.         BAL BALObj = new BAL();  
  16.         AllFiledProperty objPropertyVal = new AllFiledProperty();  
  17.         private void button1_Click(object sender, EventArgs e) {  
  18.             objPropertyVal.Name = textBox1.Text;  
  19.             objPropertyVal.Address = textBox2.Text;  
  20.             BALObj.InsertNewData(objPropertyVal);  
  21.             MessageBox.Show("Data Save");  
  22.         }  
  23.     }  
  24. }