Вы находитесь на странице: 1из 11

ONLINE connection

Code:

using using using using using using using using using

System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data.OleDb; System.Data.Odbc; System.Data.SqlClient;

public partial class Starting : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //create a conneection object OleDbConnection vconn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; datasource=G:\\Techz\\Training\\WebSites\\MS ACCESS DB\\Demodb1.mdb"); //open the connection vconn.Open(); //For Querry string vquerry = "insert into Emp(EmpId,EmpName) values (101,'Avik')"; //For using the vquerry command OleDbCommand vcomm = new OleDbCommand(vquerry, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Record Saved"; //For closing the connection vconn.Close(); } protected void Button2_Click(object sender, EventArgs e) { //create a conneection object OleDbConnection vconn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; datasource=G:\\Techz\\Training\\WebSites\\MS ACCESS DB\\Demodb1.mdb"); //open the connection vconn.Open(); //For Querry string vquerry = "Delete from Emp where EmpId="; //For using the vquerry command OleDbCommand vcomm = new OleDbCommand(vquerry, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Record Deleted"; //For closing the connection vconn.Close(); }

protected void Button4_Click(object sender, EventArgs e) { //create a connection object OdbcConnection vconn = new OdbcConnection("dsn=Demodb"); //open the connection vconn.Open(); //For Querry string vquerr = "update Employee set Ename='Abhisekh' where Eid=101"; //For using the vquerry command OdbcCommand vcomm = new OdbcCommand(vquerr, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Record Updated"; //For closing the connection vconn.Close(); } protected void Button10_Click(object sender, EventArgs e) { //create a connection object OdbcConnection vconn = new OdbcConnection("dsn=Demodb"); //open the connection vconn.Open(); //For Querry string vquerr = "insert into Employee(Eid,EName) values (103,'Avik')"; //For using the vquerry command OdbcCommand vcomm = new OdbcCommand(vquerr, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Record Saved"; //For closing the connection vconn.Close(); } protected void Button5_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //For Querry string vquerry = "insert into Employee(Eid,Ename) values (104,'Av')"; //For using the vquerry command SqlCommand vcomm = new SqlCommand(vquerry, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Record Saved"; //For closing the connection vconn.Close(); }

protected void Button6_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //For Querry string vquerry = "Delete from Employee where Eid=104"; //For using the vquerry command SqlCommand vcomm = new SqlCommand(vquerry, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Record Deleted"; //For closing the connection vconn.Close(); } protected void Button7_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //For Querry string vquerry = "create table mytemptable(Empno numeric(6), Ename varchar(20))"; //For using the vquerry command SqlCommand vcomm = new SqlCommand(vquerry, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Table Created"; //For closing the connection vconn.Close(); } protected void Button8_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //For Querry string vquerry = "Drop Table mytemptable"; //For using the vquerry command SqlCommand vcomm = new SqlCommand(vquerry, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Table Dropped"; //For closing the connection

vconn.Close(); } protected void Button9_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //For TextBox int veno = Convert.ToInt16(TextBox1.Text); string vename = TextBox2.Text; //For Querry string vquerry = "insert into Employee(Eid,Ename) values (" + veno + ",'" + vename + "')"; //For using the vquerry command SqlCommand vcomm = new SqlCommand(vquerry, vconn); //Executing the querry vcomm.ExecuteNonQuery(); Label1.Text = "Record Inserted Using SQL SERVER"; //For closing the connection vconn.Close(); } protected void Button11_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); int empno=Convert.ToInt16(TextBox3.Text); string empname=TextBox4.Text; string vquerry="insert into Emp(Empno,Empname)values (@Empno,@Ename)"; SqlCommand vcomm = new SqlCommand(vquerry, vconn); vcomm.Parameters.AddWithValue("@Empno", empno); vcomm.Parameters.AddWithValue("@Ename", empname); vcomm.ExecuteNonQuery(); vconn.Close(); } }

OFFLINE connection

Code:
using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data.SqlClient; System.Data;

public partial class Offline_Model_Of_Database : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //DataAdapter SqlDataAdapter vdap = new SqlDataAdapter("select * from emp", vconn); //Dataset DataSet ds = new DataSet(); //Fill the data in dataset using ADAPTER vdap.Fill(ds, "Tablename-emp");

vconn.Close(); //DataTable DataTable dt = ds.Tables["emp"]; //DataRow DataRow dr = dt.NewRow(); //All the new rows value dr["Eno"] = Convert.ToInt16(TextBox1.Text); dr["ename"] = TextBox2.Text; //Adding the rows to the table dt.Rows.Add(dr); //Syncing back with Database if (ds.HasChanges()) { SqlCommandBuilder vcombuild = new SqlCommandBuilder(vdap); if (vconn.State == ConnectionState.Closed) { vconn.Open(); } vdap.Update(ds, "emp"); Label2.Text = "record successfully saved"; vconn.Close(); } } protected void Button2_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //DataAdapter SqlDataAdapter vdap = new SqlDataAdapter("select * from emp", vconn); //Dataset DataSet ds = new DataSet(); //To fill in the schema of the database vdap.FillSchema(ds,SchemaType.Source ); vdap.MissingSchemaAction=MissingSchemaAction.AddWithKey; //Fill the data in dataset using ADAPTER vdap.Fill(ds, "Tablename-emp"); vconn.Close(); //DataTable DataTable dt = ds.Tables["emp"]; //code for deleting DataRowCollection drc = dt.Rows; //Find the row which we want to delete DataRow dr = drc.Find("1002"); if (dr == null) { Label2.Text = "record not found"; } else { dr.Delete(); if (ds.HasChanges()) { SqlCommandBuilder vcombuild = new SqlCommandBuilder(vdap); if (vconn.State == ConnectionState.Closed) {

vconn.Open(); } vdap.Update(ds, "emp"); Label2.Text = "record successfully Deleted"; vconn.Close(); } } } protected void Button3_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //DataAdapter SqlDataAdapter vdap = new SqlDataAdapter("select * from emp", vconn); //Dataset DataSet ds = new DataSet(); //To fill in the schema of the database vdap.FillSchema(ds,SchemaType.Source ); vdap.MissingSchemaAction=MissingSchemaAction.AddWithKey; //Fill the data in dataset using ADAPTER vdap.Fill(ds, "Tablename-emp"); vconn.Close(); //DataTable DataTable dt = ds.Tables["emp"]; //code for Updating DataRowCollection drc = dt.Rows; //Find the row which we want to Update DataRow dr = drc.Find("1002"); if (dr == null) { Label2.Text = "record not found"; } else { dr.BeginEdit(); dr["ename"]="google corporation"; dr.EndEdit(); if (ds.HasChanges()) { SqlCommandBuilder vcombuild = new SqlCommandBuilder(vdap); if (vconn.State == ConnectionState.Closed) { vconn.Open(); } vdap.Update(ds, "emp"); Label2.Text = "record successfully Updated"; vconn.Close(); } } } }

DATARETRIEVAL

Code: using System;


using using using using using using System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data.SqlClient;

public partial class dataretrieval : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { getdata(); DropDownList2.Items.Insert(0, "Select State"); } } private void getdata() { //Sqlconnection creation SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); string vquer = "select sname from state"; SqlCommand vcomm = new SqlCommand(vquer, vconn); SqlDataReader vdr = vcomm.ExecuteReader(); while (vdr.Read()) { DropDownList2.Items.Add(vdr["sname"].ToString()); } vconn.Close(); } protected void Button1_Click(object sender, EventArgs e) { //create a conneection object SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); string vquerry = "select Eid,Ename from Employee"; SqlCommand vcomm1 = new SqlCommand(vquerry, vconn); //SqlCommand vcomm = new SqlCommand(vquerry1, vconn); //Executing the querry SqlDataReader vdr = vcomm1.ExecuteReader(); while (vdr.Read()) { ListBox1.Items.Add(vdr["Eid"].ToString() + "::" + vdr["Ename"].ToString()); } vconn.Close(); } protected void Button2_Click(object sender, EventArgs e) { //create a conneection object

SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); //Logic string vquerry=""; int seljob = DropDownList1.SelectedIndex; switch (seljob) { case 1: vquerry = "select Eno,Ename,job,Salary from job where job='Manager'"; break; case 2: vquerry = "select Eno,Ename,job,Salary from job where job='Clerk'"; break; case 3: vquerry = "select Eno,Ename,job,Salary from job where job='Analyst'"; break; case 4: vquerry = "select Eno,Ename,job,Salary from job where job='Salesman'"; break; } SqlCommand vcomm = new SqlCommand(vquerry, vconn); ListBox2.Items.Clear(); SqlDataReader vdr = vcomm.ExecuteReader(); while (vdr.Read()) { ListBox2.Items.Add(vdr[0].ToString() + " :: " + vdr[1].ToString() + " :: " + vdr[2].ToString() + " :: " + vdr[3].ToString()); } vconn.Close(); } protected void Button3_Click(object sender, EventArgs e) { string vsel = DropDownList2.SelectedItem.Text; getallcity(vsel); } private void getallcity(string vstate) { //Sqlconnection creation SqlConnection vconn = new SqlConnection("server=AVIK\\SQLEXPRESS;database=DemoDb;user id=sa;pwd=123"); //open the connection vconn.Open(); string vquer = "select cname from state where Sname='"+vstate+"'"; SqlCommand vcomm = new SqlCommand(vquer, vconn); SqlDataReader vdr = vcomm.ExecuteReader(); while (vdr.Read()) { ListBox3.Items.Add(vdr["cname"].ToString()); } vconn.Close(); } }

Вам также может понравиться