- 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
- {
-
- SqlConnection con;
- int result = 0;
-
-
- #
- 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
-
-
- 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
-
- 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
-
- 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
-
- }
-
-
- }
- }
BAL Class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- namespace DBHelper {
-
- class BAL: DBHepler {
- int result = 0;
-
- public int InsertData() {
-
-
- result = InsertUpdateDelete("Sp_Name And Sql Query", CommandType.StoredProcedure);
-
- return result;
-
- }
-
-
- public int InsertNewData(AllFiledProperty propertyValue) {
-
-
- result = InsertUpdateDelete("Sp_Name And Sql Query", CommandType.StoredProcedure,
- CreateParamter("@ParamterName", DbType.String, propertyValue.Name),
- CreateParamter("@ParamterName2", DbType.String, propertyValue.Address));
-
- return result;
-
- }
-
- public DataTable GetAllData() {
-
-
- 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();
- }
-
- 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");
- }
- }
- }