ADO.NET
description
Transcript of ADO.NET
ADO.NET
By Hanumantha Rao.N
MCA
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
Architecture
C#.NET ADO.NET
Student Database Project
Start Visual StudioStart All Programs Microsoft Visual Studio 2008 Microsoft Visual Studio
Starting New C#.NET Project
Setting Path
Designing
Required Components• 6 Labels• 2 TextBoxes• 2 Combo Boxes• 1 DateTime Picker• 11 Buttons• 1 Picture Box• 1 openFileDialog
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
TextBoxes
• Insert 2 Text boxes and Change its Names as
• TextBox1 Tbrno• TextBox2 tbsname
ComboBoxes
• Insert 2 ComboBoxes and Change its Names as• ComboBox1 cmbgender• ComboBox2 cmbbranch Select
ComboBox2 Items Property MCAMBACSEITEEEECE
Select ComboBox1 Items Property
MaleFemale
DateTimePicker
• Insert 1 DateTimePicker Component• Change its Name Property DOB–Format Custom
Picture Box
• Insert PicutureBox Component• Set SizeMode Zoom• Now Insert 11 Buttons And Change Text
Property as
Insert Update DeleteNew Exit |<< << >> >>| Browse Search
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’.
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
Creating a Table
• Select Database Right Click Select DesignView
Getting Connecting string Path
• Now open you Notepad and click on Save As button. Name then student.udl. Change save type "ALL FILES".
Create Udl
j• Now double click on
student. udl file. A wizard will start like this
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.
j
Now click on test connection and click OK Now edit this UDL file with note pad and copy link as shown below
Finding Path
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;
Browse Button
openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"; DialogResult res = openFileDialog1.ShowDialog(); if (res == DialogResult.OK) { pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); }
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();
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 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); } }
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
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
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");
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");
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 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
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
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
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;
}
Exit Button
• private void btnexit_Click(object sender, EventArgs e)
• {• this.Close();• }
Executing Project
• Now Goto Debug Start Debug OR
• Press F5
Thankyou
• By
• Hanumantha Rao.N MCA• Venkataiah.M MCA• HanimiReddy. A MCA
www.hanutechvision.blogspot.inwww.venky-venky4ever.blogspot.in
Contact