Lecture_10 (Working with DB_2)
Lecture_10 (Working with DB_2)
Ahmed Alnasheri
Objectives
To use ADO.NET's disconnected object model to store
2
Programming with ADO
Providers: In last lecture, we mentioned that we can use
ADO in two different ways:
Use ADO to connect to the data base directly, or
Use ADO disconnected object to load data into memory
4
Example 1
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace InsertUpdateDeleteDemoDB
{
public partial class Form1 : Form
{
private const string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BOOKS.mdb;";
readonly OleDbConnection con = new OleDbConnection(conString);
OleDbCommand cmd;
OleDbDataAdapter adapter;
readonly DataTable dt = new DataTable();
5
Example 1
public Form1()
{
InitializeComponent();
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[2].Name = "State";
//SELECTION MODE
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
}
6
Example 1
private void retrieve()
{
dataGridView1.Rows.Clear();
//SQL STATEMENT
String sql = "SELECT * FROM tbl_Record order by ID";
cmd = new OleDbCommand(sql, con);
try
{
con.Open();
adapter = new OleDbDataAdapter(cmd);
adapter.Fill(dt);
//LOOP THROUGH DATATABLE
foreach (DataRow row in dt.Rows)
{
populate(row[0].ToString(), row[1].ToString(), row[2].ToString());
}
con.Close();
//CLEAR DATATABLE
dt.Rows.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
7
Example 1
private void clearTxts()
{
txt_Name.Text = "";
txt_State.Text = "";
txt_ID.Text = "";
}
8
Example 1
private void btn_Update_Click(object sender, EventArgs e)
{
string sql = "UPDATE tbl_Record SET Name='" + txt_Name.Text + "',State ='" + txt_State.Text + "' WHERE ID=" + txt_ID.Text + "";
cmd = new OleDbCommand(sql, con);
//REFRESH DATA
retrieve();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
9
Example 1
private void btn_Delete_Click(object sender, EventArgs e)
{
String sql = "DELETE FROM tbl_record WHERE ID=" + txt_ID.Text + "";
cmd = new OleDbCommand(sql, con);
10
Example 1
private void btn_Clare_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
clearTxts();
}
}
} // end Class Form1
} // End namespace class
11
Example 2 ( Data Source = SmallPub.mdf )
12
Example 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication18
{
public partial class Form1 : Form
{
string connectionString = "Data Source =
(LocalDB)\\v11.0;AttachDbFilename=C:\\csharp_prog\\database\\WindowsFormsApplication15\\WindowsFormsApplication15\\Smal
lPub.mdf;Integrated Security=True;Connect Timeout=30;";
public Form1()
{
InitializeComponent();
Bind();
}
13
Example 2
private void Clear()
{
txtCat.Text = string.Empty;
txtDesc.Text = string.Empty;
}
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Category", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
14
Example 2
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (txtCat.Text != "")
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert Into Category(Categoryid,Description) Values (@Catid,@Descr)", con);
cmd.Parameters.AddWithValue("Catid", txtCat.Text);
cmd.Parameters.AddWithValue("Descr", txtDesc.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inserted sucessfully");
Bind();
Clear();
}
else
{
MessageBox.Show("Category Id Can not be Empty");
}
}
catch (Exception err)
{
MessageBox.Show("Duplicate !!!!!!");
}
}
15
Example 2
private void btnEdit_Click(object sender, EventArgs e)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
txtCat.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
txtDesc.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
txtCat.Enabled = false;
txtDesc.Focus();
}
con.Open();
SqlCommand cmd = new SqlCommand("Update Category set categoryid= @Catid, Description = @Descr
Where(Categoryid=@CatID)", con);
cmd.Parameters.AddWithValue("@CatID", txtCat.Text);
cmd.Parameters.AddWithValue("@Descr", txtDesc.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("updated......");
con.Close();
Bind();
Clear();
txtCat.Enabled = true;
}
16
Example 2
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand delcmd = new SqlCommand();
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
17
Example 3 (Master Detail Form)
18
Example 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
19
Example 3
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'smallPubDataSet.Book' table. You can move, or remove it, as needed.
this.categoryTableAdapter.Fill(this.smallPubDataSet.Category);
this.bookTableAdapter.Fill(this.smallPubDataSet.Book, Convert.ToInt32(categoryIdTextBox.Text));
// TODO: This line of code loads data into the 'smallPubDataSet.Category' table. You can move, or remove it, as needed.
}
20
The End
21