using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DBHelper {
class AllFiledProperty {
public string Name {
get;
set;
}
public string Address {
get;
set;
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DBHelper {
class DBHepler //Abstract Class Not For Object Create Only Inherited Class
{
//Golobal Veriable
SqlConnection con; //Object SqlConntion Con
int result = 0; //
//Class Name Same Sql Connection
#
region SqlConnection
public DBHepler() {
con = new SqlConnection("Sql Connection string");
}#
endregion
# region Create a Paramter(Insert Update Delete)
protected SqlParameter CreateParamter(string _paramterName, DbType _dbtype, object _value) {
SqlParameter paramter = new SqlParameter();
paramter.ParameterName = _paramterName;
paramter.DbType = _dbtype;
paramter.Value = _value;
return paramter;
}#
endregion
# region Insert Update Delete Without Paramter
//Insert Update Delete Common Method Without Paramter
// _SqlQuery Exmp Sqlquery And Storeprocedure Name
protected int InsertUpdateDelete(string _SqlQuery, CommandType _commandtype) {
try {
SqlCommand cmd = new SqlCommand();
cmd.CommandText = _SqlQuery;
cmd.CommandType = _commandtype;
cmd.Connection = con;
if (con.State != ConnectionState.Open) {
con.Open();
}
return result = cmd.ExecuteNonQuery();
} catch (Exception) {
return 0;
}
}#
endregion
# region Insert Update Delete With Paramter
//Method Name Same Without Paramter And Paramter Same Method
protected int InsertUpdateDelete(string _SqlQuery, CommandType _commandtype, params SqlParameter[] paramter) {
try {
SqlCommand cmd = new SqlCommand();
cmd.CommandText = _SqlQuery;
cmd.CommandType = _commandtype;
cmd.Connection = con;
cmd.Parameters.Clear();
foreach(var item in paramter) {
cmd.Parameters.Add(item);
}
if (con.State != ConnectionState.Open) {
con.Open();
}
return result = cmd.ExecuteNonQuery();
} catch (Exception) {
return 0;
}
}#
endregion
# region Datatable Return GridView Baind Dropdown Bind
//return Datatable
protected DataTable SelectData(string _SqlQuery, CommandType _commandtype) {
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
try {
cmd.CommandText = _SqlQuery;
cmd.CommandType = _commandtype;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
} catch (Exception) {
throw;
} finally {
dt.Dispose();
da.Dispose();
}#
endregion
}
//Create a New Class BAL
//How To Use DBHelper Class
}
}BAL Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DBHelper {
//inherited Class
class BAL: DBHepler {
int result = 0;
//Insert Without Paramter
public int InsertData() {
//If Sp Name Pass Then Use CommandType.StoredProcedure
//If Sql Query Pass Then Use CommandType.Text
result = InsertUpdateDelete("Sp_Name And Sql Query", CommandType.StoredProcedure);
//If result Value 1 Insert Data Successfully
return result;
//If result 0 Insert Failed
}
//Insert Data Paramter
public int InsertNewData(AllFiledProperty propertyValue) {
//If Sp Name Pass Then Use CommandType.StoredProcedure
//If Sql Query Pass Then Use CommandType.Text
result = InsertUpdateDelete("Sp_Name And Sql Query", CommandType.StoredProcedure,
CreateParamter("@ParamterName", DbType.String, propertyValue.Name),
CreateParamter("@ParamterName2", DbType.String, propertyValue.Address));
//If result Value 1 Insert Data Successfully
return result;
//If result 0 Insert Failed
}
public DataTable GetAllData() {
//If Sp Name Pass Then Use CommandType.StoredProcedure
//If Sql Query Pass Then Use CommandType.Text
DataTable dt = SelectData("SqlQuery And Sp Name", CommandType.Text);
return dt;
}
}
}My Windows Application Use Button Click Then Save Data
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DBHelper {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
//Golobal Veriable
BAL BALObj = new BAL();
AllFiledProperty objPropertyVal = new AllFiledProperty();
private void button1_Click(object sender, EventArgs e) {
objPropertyVal.Name = textBox1.Text;
objPropertyVal.Address = textBox2.Text;
BALObj.InsertNewData(objPropertyVal);
MessageBox.Show("Data Save");
}
}
}