ADO.NET By Hanumantha Rao.N MCA By Hanumantha Rao.N MCA.

download ADO.NET By Hanumantha Rao.N MCA By Hanumantha Rao.N MCA.

If you can't read please download the document

Transcript of ADO.NET By Hanumantha Rao.N MCA By Hanumantha Rao.N MCA.

  • Slide 1
  • ADO.NET By Hanumantha Rao.N MCA By Hanumantha Rao.N MCA
  • Slide 2
  • ADO.NET Active Data Objects.NET It is very important for a developer to have good idea about how application can connect to databases as this becomes the base for working with any remote data sources. ADO.NET of.NET provides many different options/styles for communicating with DBs Active Data Objects.NET It is very important for a developer to have good idea about how application can connect to databases as this becomes the base for working with any remote data sources. ADO.NET of.NET provides many different options/styles for communicating with DBs
  • Slide 3
  • Architecture
  • Slide 4
  • C#.NET ADO.NET
  • Slide 5
  • Student Database Project
  • Slide 6
  • Start Visual Studio Start All Programs Microsoft Visual Studio 2008 Microsoft Visual Studio
  • Slide 7
  • Starting New C#.NET Project File New Project
  • Slide 8
  • Setting Path
  • Slide 9
  • Designing Required Components 6 Labels 2 TextBoxes 2 Combo Boxes 1 DateTime Picker 11 Buttons 1 Picture Box 1 openFileDialog Required Components 6 Labels 2 TextBoxes 2 Combo Boxes 1 DateTime Picker 11 Buttons 1 Picture Box 1 openFileDialog
  • Slide 10
  • Labels Take 6 Labels and change its Text Property as 1. Newtons Institue of Engineering 2. Roll No 3. S Name 4. DOB 5. Gender 6. Branch Take 6 Labels and change its Text Property as 1. Newtons Institue of Engineering 2. Roll No 3. S Name 4. DOB 5. Gender 6. Branch
  • Slide 11
  • TextBoxes Insert 2 Text boxes and Change its Names as TextBox1 Tbrno TextBox2 tbsname Insert 2 Text boxes and Change its Names as TextBox1 Tbrno TextBox2 tbsname
  • Slide 12
  • ComboBoxes Insert 2 ComboBoxes and Change its Names as ComboBox1 cmbgender ComboBox2 cmbbranch Select ComboBox2 Items Property MCA MBA CSE IT EEE ECE Select ComboBox2 Items Property MCA MBA CSE IT EEE ECE Select ComboBox1 Items Property Male Female Select ComboBox1 Items Property Male Female
  • Slide 13
  • DateTimePicker Insert 1 DateTimePicker Component Change its Name Property DOB Format Custom
  • Slide 14
  • Picture Box Insert PicutureBox Component Set SizeMode Zoom Now Insert 11 Buttons And Change Text Property as Insert PicutureBox Component Set SizeMode Zoom Now Insert 11 Buttons And Change Text Property as InsertUpdateDelete New Exit| >>>|BrowseSearch InsertUpdateDelete New Exit| >>>|BrowseSearch
  • Slide 15
  • Adding a Reference: Goto Project Menu Add Reference select 'Microsoft.VisualBasic' from.NET tab. Inorder to use this we have to include the namespace: using Microsoft.VisualBasic Inorder to use OleDb Connection include the namespace: using System.Data.OleDb Inorder to use FileStream or MemoryStream we have to include the namespace: using System.IO. Goto Project Menu Add Reference select 'Microsoft.VisualBasic' from.NET tab. Inorder to use this we have to include the namespace: using Microsoft.VisualBasic Inorder to use OleDb Connection include the namespace: using System.Data.OleDb Inorder to use FileStream or MemoryStream we have to include the namespace: using System.IO.
  • Slide 16
  • Creating MSAccess Database Start Programs MSOffice MSAccess File New Database NIEStd.accdb Save This Database in Your Project Location Ex: D:\Hanu\NIEStd.accdb Now Click on Create Button Start Programs MSOffice MSAccess File New Database NIEStd.accdb Save This Database in Your Project Location Ex: D:\Hanu\NIEStd.accdb Now Click on Create Button
  • Slide 17
  • Creating a Table Select Database Right Click Select DesignView
  • Slide 18
  • Getting Connecting string Path Now open you Notepad and click on Save As button. Name then student.udl. Change save type "ALL FILES".
  • Slide 19
  • Create Udl
  • Slide 20
  • j Now double click on student. udl file. A wizard will start like this
  • Slide 21
  • Select Database Click Provider TAB, select Microsoft Jet 4.0 OLE DB (denoted by black arrow) then click next. Now click "Select or enter a database name" and select the desire database then click open.
  • Slide 22
  • j Now click on test connection and click OK Now edit this UDL file with note pad and copy link as shown below Now click on test connection and click OK Now edit this UDL file with note pad and copy link as shown below
  • Slide 23
  • Finding Path
  • Slide 24
  • Code For Form public partial class Form1 : Form { public Form1() { InitializeComponent(); } OleDbConnection con; OleDbCommand cmd; OleDbDataAdapter adapter; DataSet ds; int rno = 0; MemoryStream ms; byte[] photo_aray; public partial class Form1 : Form { public Form1() { InitializeComponent(); } OleDbConnection con; OleDbCommand cmd; OleDbDataAdapter adapter; DataSet ds; int rno = 0; MemoryStream ms; byte[] photo_aray;
  • Slide 25
  • Browse Button openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"; DialogResult res = openFileDialog1.ShowDialog(); if (res == DialogResult.OK) { pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); } openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"; DialogResult res = openFileDialog1.ShowDialog(); if (res == DialogResult.OK) { pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); }
  • Slide 26
  • Form Load Code con=new OleDbConnection(@"Provider=Microsoft.A CE.OLEDB.12.0;Data Source=D:\Hanu\niestd.accdb;Persist Security Info=False"); tbrno.Enabled = false; loaddata(); showdata();
  • Slide 27 table name in stud.accdb/stud.mdb file ad"> table name in stud.accdb/stud.mdb file adapter.Fill(ds, "niestd"); ds.Tables[0].Constraints.Add("pk_sno", ds.Tables[0].Columns[0], true); } void loaddata() { adapter =new OleDbDataAdapter("select * from niestd",con); ds = new DataSet( );//student-> table name in stud.accdb/stud.mdb file adapter.Fill(ds, "niestd"); ds.Tables[0].Constraints.Add("pk_sno", ds.Tables[0].Columns[0], true); }"> table name in stud.accdb/stud.mdb file ad" title="void loaddata() { adapter =new OleDbDataAdapter("select * from niestd",con); ds = new DataSet( );//student-> table name in stud.accdb/stud.mdb file ad">
  • void loaddata() { adapter =new OleDbDataAdapter("select * from niestd",con); ds = new DataSet( );//student-> table name in stud.accdb/stud.mdb file adapter.Fill(ds, "niestd"); ds.Tables[0].Constraints.Add("pk_sno", ds.Tables[0].Columns[0], true); } void loaddata() { adapter =new OleDbDataAdapter("select * from niestd",con); ds = new DataSet( );//student-> table name in stud.accdb/stud.mdb file adapter.Fill(ds, "niestd"); ds.Tables[0].Constraints.Add("pk_sno", ds.Tables[0].Columns[0], true); }
  • Slide 28
  • void showdata() { tbrno.Enabled = false; tbrno.Text= ds.Tables[0].Rows[rno][0].ToString(); tbsname.Text= ds.Tables[0].Rows[rno][1].ToString(); dob.Text= ds.Tables[0].Rows[rno][2].ToString(); cmbgender.Text=ds.Tables[0].Rows[rno][3].ToString(); cmbbranch.Text=ds.Tables[0].Rows[rno][4].ToString(); pictureBox1.Image = null; if (ds.Tables[0].Rows[rno][5] != System.DBNull.Value) { photo_aray = (byte[])ds.Tables[0].Rows[rno][5]; MemoryStream ms = new MemoryStream(photo_aray); pictureBox1.Image = Image.FromStream(ms); } void showdata() { tbrno.Enabled = false; tbrno.Text= ds.Tables[0].Rows[rno][0].ToString(); tbsname.Text= ds.Tables[0].Rows[rno][1].ToString(); dob.Text= ds.Tables[0].Rows[rno][2].ToString(); cmbgender.Text=ds.Tables[0].Rows[rno][3].ToString(); cmbbranch.Text=ds.Tables[0].Rows[rno][4].ToString(); pictureBox1.Image = null; if (ds.Tables[0].Rows[rno][5] != System.DBNull.Value) { photo_aray = (byte[])ds.Tables[0].Rows[rno][5]; MemoryStream ms = new MemoryStream(photo_aray); pictureBox1.Image = Image.FromStream(ms); }
  • Slide 29 0) { MessageBox.Show("record inserted"); loaddata(); rno++; } else MessageBox.Show("insertion failed"); cmd = new OleDbCommand("insert into niestd(rollno,sname,dob,gender,branch,photo) values('" + tbrno.Text + "','" + tbsname.Text + "','" + dob.Text + "','" + cmbgender.Text + "','" + cmbbranch.Text + "',@photo)", con); conv_photo(); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); if (n > 0) { MessageBox.Show("record inserted"); loaddata(); rno++; } else MessageBox.Show("insertion failed"); Insert Button">
  • cmd = new OleDbCommand("insert into niestd(rollno,sname,dob,gender,branch,photo) values('" + tbrno.Text + "','" + tbsname.Text + "','" + dob.Text + "','" + cmbgender.Text + "','" + cmbbranch.Text + "',@photo)", con); conv_photo(); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); if (n > 0) { MessageBox.Show("record inserted"); loaddata(); rno++; } else MessageBox.Show("insertion failed"); cmd = new OleDbCommand("insert into niestd(rollno,sname,dob,gender,branch,photo) values('" + tbrno.Text + "','" + tbsname.Text + "','" + dob.Text + "','" + cmbgender.Text + "','" + cmbbranch.Text + "',@photo)", con); conv_photo(); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); if (n > 0) { MessageBox.Show("record inserted"); loaddata(); rno++; } else MessageBox.Show("insertion failed"); Insert Button
  • Slide 30
  • void conv_photo() { if (pictureBox1.Image != null) { ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); byte[] photo_aray = new byte[ms.Length]; ms.Position = 0; ms.Read(photo_aray, 0, photo_aray.Length); cmd.Parameters.AddWithValue("@photo", photo_aray); } Convert Image
  • Slide 31 0) { MessageBox.Show("Record Updated"); loaddata(); } else MessageBox.Show("Updation Failed");">
  • UpDate Button cmd = new OleDbCommand("update niestd set sname='" + tbsname.Text + "', dob='" + dob.Text + "',gender='" +cmbgender.Text +"',branch='" +cmbbranch.Text + "', photo=@photo where rollno='" + tbrno.Text+"'", con); conv_photo(); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); if (n > 0) { MessageBox.Show("Record Updated"); loaddata(); } else MessageBox.Show("Updation Failed");
  • Slide 32 0) { MessageBox.Show("Record Deleted"); loaddata( ); rno = 0; showdata( ); } else MessageBox.Show("Deletion failed"); cmd = new OleDbCommand("delete from niestd where rollno=" + tbrno.Text+ ,con); con.Open( ); int n = cmd.ExecuteNonQuery( ); con.Close( ); if (n > 0) { MessageBox.Show("Record Deleted"); loaddata( ); rno = 0; showdata( ); } else MessageBox.Show("Deletion failed");">
  • Delete Button cmd = new OleDbCommand("delete from niestd where rollno=" + tbrno.Text+ ,con); con.Open( ); int n = cmd.ExecuteNonQuery( ); con.Close( ); if (n > 0) { MessageBox.Show("Record Deleted"); loaddata( ); rno = 0; showdata( ); } else MessageBox.Show("Deletion failed"); cmd = new OleDbCommand("delete from niestd where rollno=" + tbrno.Text+ ,con); con.Open( ); int n = cmd.ExecuteNonQuery( ); con.Close( ); if (n > 0) { MessageBox.Show("Record Deleted"); loaddata( ); rno = 0; showdata( ); } else MessageBox.Show("Deletion failed");
  • Slide 33 0) { rno = 0; showdata(); MessageBox.Show("First Record"); } else MessageBox.Show("no records"); }"> 0) { rno = 0; showdata(); MessageBox.Show("First ">
  • First Button private void btnfirst_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { rno = 0; showdata(); MessageBox.Show("First Record"); } else MessageBox.Show("no records"); } private void btnfirst_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { rno = 0; showdata(); MessageBox.Show("First Record"); } else MessageBox.Show("no records"); }
  • Slide 34 0) { if (rno > 0) { rno--; showdata(); } else MessageBox.Show("First Record"); } else MessageBox.Show("no records"); } Previous Button"> 0) { if (rno > 0) { rno--; showdata(); } else MessageBox.Show("">
  • private void btnprev_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { if (rno > 0) { rno--; showdata(); } else MessageBox.Show("First Record"); } else MessageBox.Show("no records"); } private void btnprev_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { if (rno > 0) { rno--; showdata(); } else MessageBox.Show("First Record"); } else MessageBox.Show("no records"); } Previous Button
  • Slide 35 0) { if (rno < ds.Tables[0].Rows.Count - 1) { rno++; showdata(); } else MessageBox.Show("Last Record"); } else MessageBox.Show("no records"); } Next Button">
  • private void btnnext_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { if (rno < ds.Tables[0].Rows.Count - 1) { rno++; showdata(); } else MessageBox.Show("Last Record"); } else MessageBox.Show("no records"); } private void btnnext_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { if (rno < ds.Tables[0].Rows.Count - 1) { rno++; showdata(); } else MessageBox.Show("Last Record"); } else MessageBox.Show("no records"); } Next Button
  • Slide 36 0) { rno = ds.Tables[0].Rows.Count - 1; showdata(); MessageBox.Show("Last Record"); } else MessageBox.Show("no records"); } Last Button Code">
  • private void btnlast_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { rno = ds.Tables[0].Rows.Count - 1; showdata(); MessageBox.Show("Last Record"); } else MessageBox.Show("no records"); } private void btnlast_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { rno = ds.Tables[0].Rows.Count - 1; showdata(); MessageBox.Show("Last Record"); } else MessageBox.Show("no records"); } Last Button Code
  • Slide 37
  • Clear Button Code private void btnclear_Click(object sender, EventArgs e) { tbrno.Text = tbsname.Text = " "; tbrno.Enabled = true; cmbbranch.Text = cmbgender.Text = " "; pictureBox1.Image = null; } private void btnclear_Click(object sender, EventArgs e) { tbrno.Text = tbsname.Text = " "; tbrno.Enabled = true; cmbbranch.Text = cmbgender.Text = " "; pictureBox1.Image = null; }
  • Slide 38
  • Exit Button private void btnexit_Click(object sender, EventArgs e) { this.Close(); } private void btnexit_Click(object sender, EventArgs e) { this.Close(); }
  • Slide 39
  • Executing Project Now Goto Debug Start Debug OR PressF5
  • Slide 40
  • Thankyou By Hanumantha Rao.NMCA Venkataiah.M MCA HanimiReddy. A MCA By Hanumantha Rao.NMCA Venkataiah.M MCA HanimiReddy. A MCA www.hanutechvision.blogspot.in www.hanutechvision.jimdo.com www.hanutechvision.blogspot.in www.hanutechvision.jimdo.com Contact