ADO.NET

40
ADO.NET By Hanumantha Rao.N MCA

description

ADO.NET. By Hanumantha Rao.N MCA. ADO.NET. Active Data Objects .NET - PowerPoint PPT Presentation

Transcript of ADO.NET

Page 1: ADO.NET

ADO.NET

By Hanumantha Rao.N

MCA

Page 2: ADO.NET

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 DB’s

Page 3: ADO.NET

Architecture

Page 4: ADO.NET

C#.NET ADO.NET

Page 5: ADO.NET

Student Database Project

Page 6: ADO.NET

Start Visual StudioStart All Programs Microsoft Visual Studio 2008 Microsoft Visual Studio

Page 7: ADO.NET

Starting New C#.NET Project

Page 8: ADO.NET

Setting Path

Page 9: ADO.NET

Designing

Required Components• 6 Labels• 2 TextBoxes• 2 Combo Boxes• 1 DateTime Picker• 11 Buttons• 1 Picture Box• 1 openFileDialog

Page 10: ADO.NET

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

Page 11: ADO.NET

TextBoxes

• Insert 2 Text boxes and Change its Names as

• TextBox1 Tbrno• TextBox2 tbsname

Page 12: ADO.NET

ComboBoxes

• Insert 2 ComboBoxes and Change its Names as• ComboBox1 cmbgender• ComboBox2 cmbbranch Select

ComboBox2 Items Property MCAMBACSEITEEEECE

Select ComboBox1 Items Property

MaleFemale

Page 13: ADO.NET

DateTimePicker

• Insert 1 DateTimePicker Component• Change its Name Property DOB–Format Custom

Page 14: ADO.NET

Picture Box

• Insert PicutureBox Component• Set SizeMode Zoom• Now Insert 11 Buttons And Change Text

Property as

Insert Update DeleteNew Exit |<< << >> >>| Browse Search

Page 15: ADO.NET

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’.

Page 16: ADO.NET

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

Page 17: ADO.NET

Creating a Table

• Select Database Right Click Select DesignView

Page 18: ADO.NET

Getting Connecting string Path

• Now open you Notepad and click on Save As button. Name then student.udl. Change save type "ALL FILES".

Page 19: ADO.NET

Create Udl

Page 20: ADO.NET

j• Now double click on

student. udl file. A wizard will start like this

Page 21: ADO.NET

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.

Page 22: ADO.NET

j

Now click on test connection and click OK Now edit this UDL file with note pad and copy link as shown below

Page 23: ADO.NET

Finding Path

Page 24: ADO.NET

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;

Page 25: ADO.NET

Browse Button

openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"; DialogResult res = openFileDialog1.ShowDialog(); if (res == DialogResult.OK) { pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); }

Page 26: ADO.NET

Form Load Code

• con=new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Hanu\niestd.accdb;Persist Security Info=False");

tbrno.Enabled = false;

loaddata();

showdata();

Page 27: ADO.NET

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); }

Page 28: ADO.NET

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); } }

Page 29: ADO.NET

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

Page 30: ADO.NET

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

Page 31: ADO.NET

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");

Page 32: ADO.NET

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");

Page 33: ADO.NET

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");

• }

Page 34: ADO.NET

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

Page 35: ADO.NET

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

Page 36: ADO.NET

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

Page 37: ADO.NET

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;

}

Page 38: ADO.NET

Exit Button

• private void btnexit_Click(object sender, EventArgs e)

• {• this.Close();• }

Page 39: ADO.NET

Executing Project

• Now Goto Debug Start Debug OR

• Press F5

Page 40: ADO.NET

Thankyou

• By

• Hanumantha Rao.N MCA• Venkataiah.M MCA• HanimiReddy. A MCA

www.hanutechvision.blogspot.inwww.venky-venky4ever.blogspot.in

Contact