0% found this document useful (0 votes)
67 views

MS Access Database To Csharp

This C# code defines methods for connecting an Access database to an application and performing CRUD operations on different tables in the database. It includes methods for adding student, attendance, and class data to tables, as well as listing data from tables and populating it in list views with alternating row colors. The methods open a connection to an Access database, build SQL queries with parameters, execute queries, and handle exceptions.

Uploaded by

Mubashar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

MS Access Database To Csharp

This C# code defines methods for connecting an Access database to an application and performing CRUD operations on different tables in the database. It includes methods for adding student, attendance, and class data to tables, as well as listing data from tables and populating it in list views with alternating row colors. The methods open a connection to an Access database, build SQL queries with parameters, execute queries, and handle exceptions.

Uploaded by

Mubashar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C# Code for Ms Access Database link with application

using System;

using System.Collections.Generic;

using System.Data.OleDb;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace IslamiaySchool.Classes

class AddtoDataBase

OleDbConnection con = new


OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=.\IslamiyaSchool.accdb");

public void AddStudent(StudentInfo add)

// StudentInfo add =new StudentInfo();


OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = "INSERT INTO StudentInfo([RollNO],[StName], [StFname],


[DOB],[CNIC],[FCelNO], [Address],[ClassName], [AddmDate], [Gender]) VALUES (@rollno,
@stname, @stfname,@dob, @cnic, @fcelno,@address, @classname, @addmdate, @gender)";

cmd.Parameters.AddWithValue("@rollno", add.RollNo);

cmd.Parameters.AddWithValue("@stname", add.SName);

cmd.Parameters.AddWithValue("@stfname", add.SFname);

cmd.Parameters.AddWithValue("@dob", add.DOb);

cmd.Parameters.AddWithValue("@cnic", add.Cnic);

cmd.Parameters.AddWithValue("@fcelno", add.FCelNo);

cmd.Parameters.AddWithValue("@address", add.Address);

cmd.Parameters.AddWithValue("@classname", add.ClassName);

cmd.Parameters.AddWithValue("@addmdate", add.AddmDate);

cmd.Parameters.AddWithValue("@gender", add.Gender);

try

con.Open();

cmd.ExecuteNonQuery();

con.Close();

catch (Exception ex)


{

MessageBox.Show(ex.Message);

public void Addattendence(AddStdAttendence add)

// StudentInfo add =new StudentInfo();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = "INSERT INTO


StdAttendence([RollNO],[StName],[AttDate],[Class],[Attendence]) VALUES (@rollno,
@stname,@attdate,@class,@attn)";

cmd.Parameters.AddWithValue("@rollno", add.rollno);

cmd.Parameters.AddWithValue("@stname", add.name);

cmd.Parameters.AddWithValue("@attdate", add.atndate);

cmd.Parameters.AddWithValue("@class", add.classname);

cmd.Parameters.AddWithValue("@attn", add.Attendence);

try

con.Open();

cmd.ExecuteNonQuery();
con.Close();

catch (Exception ex)

MessageBox.Show(ex.Message);

public void Addclass(AddeClass add)

// StudentInfo add =new StudentInfo();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = "INSERT INTO Class([ClassName]) VALUES (@name)";

cmd.Parameters.AddWithValue("@name", add.clsname);

try

con.Open();

cmd.ExecuteNonQuery();

con.Close();

}
catch (Exception ex)

MessageBox.Show(ex.Message);

public void list_DataView(string strSQL, ListView myList, string A)

myList.Items.Clear();

try

OleDbCommand cmd = new OleDbCommand(strSQL, con);

con.Open();

OleDbDataReader dr = cmd.ExecuteReader();

while (dr.Read())

ListViewItem lItem = new ListViewItem();

for (int i = 0; i < dr.FieldCount; i++)

lItem.SubItems[0].Text=A;
lItem.SubItems.Add(dr[i].ToString());

myList.Items.Add(lItem);

int rCount = myList.Items.Count;

if (rCount % 2 == 1)

//lItem.BackColor = Color.WhiteSmoke;

lItem.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(208)))),
((int)(((byte)(234)))), ((int)(((byte)(189)))));

else

lItem.BackColor = Color.White;

con.Close();
cmd.Dispose();

dr.Close();

catch (Exception exp)

MessageBox.Show(exp.Message);

public void list_DataView(string strSQL, ListView myList )

myList.Items.Clear();

try

OleDbCommand cmd = new OleDbCommand(strSQL, con);

con.Open();

OleDbDataReader dr = cmd.ExecuteReader();

while (dr.Read())

ListViewItem lItem = new ListViewItem(dr[0].ToString());

for (int i = 1; i < dr.FieldCount; i++)


{

lItem.SubItems.Add(dr[i].ToString());

myList.Items.Add(lItem);

int rCount = myList.Items.Count;

if (rCount % 2 == 1)

//lItem.BackColor = Color.WhiteSmoke;

lItem.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(208)))),
((int)(((byte)(234)))), ((int)(((byte)(189)))));

else

lItem.BackColor = Color.White;

}
}

con.Close();

cmd.Dispose();

dr.Close();

catch (Exception exp)

MessageBox.Show(exp.Message);

con.Close();

You might also like