class name : Connection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; //for sql data connection;
using System.Data; // for data set data table ;
using System.Windows.Forms; // for taking a refrence of controls in a class file. like combobox;
//this class is make for universal sql connnection in the project;
namespace WFA2
{
class Connection
{
public static SqlConnection conn()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;User ID=sa");
return con;
}
public static DataSet getData(String str) // this connection architecture .... no need to open connection // to display data
{
SqlConnection con = Connection.conn();
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public static void AllPer(String str) // this is not this connection architecture // to inserting data
{
SqlConnection con = Connection.conn();
SqlCommand cmd = new SqlCommand(str,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Using class for accessing connection
// this will show the data from table in grid viewer
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = Connection.getData("Select * from infoCustomer");
dataGridView1.DataSource = ds.Tables[0];
}
private void button2_Click(object sender, EventArgs e)
{
Connection.AllPer("insert into infoCustomer values('" + comboBox1.Text + "','"+txtAge .Text +"','"+txtSalary .Text+"')");
MessageBox.Show("Record Inserted");
}
Adding the items from data table into combobox on run time
Make a class for FillBox method
public static void FillBox(ref ComboBox cmb, string str) //using System.Windows.Forms; here
{
DataSet ds = Connection.getData(str);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cmb.Items.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
}
}
On Form Load Code
private void Form1_Load(object sender, EventArgs e)
{
Connection.FillBox(ref comboBox1, "select name from infoCustomer");
}
Note: "Connection.FillBox " is used regarding to Connection.cs class file...you can change the word "Connection" if your class file name is different.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; //for sql data connection;
using System.Data; // for data set data table ;
using System.Windows.Forms; // for taking a refrence of controls in a class file. like combobox;
//this class is make for universal sql connnection in the project;
namespace WFA2
{
class Connection
{
public static SqlConnection conn()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;User ID=sa");
return con;
}
public static DataSet getData(String str) // this connection architecture .... no need to open connection // to display data
{
SqlConnection con = Connection.conn();
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public static void AllPer(String str) // this is not this connection architecture // to inserting data
{
SqlConnection con = Connection.conn();
SqlCommand cmd = new SqlCommand(str,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Using class for accessing connection
// this will show the data from table in grid viewer
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = Connection.getData("Select * from infoCustomer");
dataGridView1.DataSource = ds.Tables[0];
}
Note : Take a button named it button1 and take a dataGridView1 control from toolbox;
// this will insert the data in table
private void button2_Click(object sender, EventArgs e)
{
Connection.AllPer("insert into infoCustomer values('" + comboBox1.Text + "','"+txtAge .Text +"','"+txtSalary .Text+"')");
MessageBox.Show("Record Inserted");
}
Adding the items from data table into combobox on run time
Make a class for FillBox method
public static void FillBox(ref ComboBox cmb, string str) //using System.Windows.Forms; here
{
DataSet ds = Connection.getData(str);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cmb.Items.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
}
}
On Form Load Code
private void Form1_Load(object sender, EventArgs e)
{
Connection.FillBox(ref comboBox1, "select name from infoCustomer");
}
Note: "Connection.FillBox " is used regarding to Connection.cs class file...you can change the word "Connection" if your class file name is different.
No comments:
Post a Comment