Learn programming language

How To Use Access Database With c# (Step by Step Guide)

This is a step by step connect to access database with c# tutorial. C# is one of the programming language of the 21st century, with cool features that gives you varieties and dynamism in this modern world. And this tutorial aim explaining how to use access database with c# connection string.

access database with c#

Of the numbers of databases you can use c# with, MS Access database is one of them that gives you easy way of packaging apps and deploying it to your client. c# connect to access database programmatically in various ways but here is mine.

 

How to connect to access database in visual studio 2010.

Just create a class (Right click on your project and click on add class)

Note System.web must be added as a reference. (Go to your project references and right click and then click on add reference. Another window pop up with tabs. On your .Net tabs search for System.web and click ok button to add the reference. )

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

using System.Data.OleDb;

using System.Xml.Serialization;

using System.IO;

using System.Web;

using System.Web.UI;

 

 

namespace Project1

{

class class1

{

private OleDbConnection con = null;

private OleDbCommand command = null;

private OleDbDataAdapter dataAdapter = null;

public String dbaFile = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\royal\\Documents\\Visual Studio 2008\\Projects\\Project1\\Project1\\bin\\Debug\\bin\\photo.mdb”;

Note: if you are using .accdb then the connection string will be 12.0;

public String CONNECTION = “”;

 

 

// INITIALIZE AND CONFIGURE DATABASE

public void InitiateDatabaseConfiguration()

{

 

try

{

//Initialize OLEDB.

con = null;

con = new OleDbConnection(dbaFile);

}

catch (Exception)

{

MessageBox.Show(“Unable to Connect to the Database.”, “”, MessageBoxButtons.OK);

}

}

 

 

public object[] LoadDataArrayByQuery(String Query)

{

 

InitiateDatabaseConfiguration();

 

DataSet dataSet = new DataSet();

 

try

{

//Initialize OleDbCommand to Execute SQL Query

command = new OleDbCommand(Query, con); // Execute ‘query’ from con instance using

// OleDbCommand

 

 

//Initialize OleDbDataAdapter to retrieve and Populate Dataset with the Data in the

// OleDbCommand

dataAdapter = new OleDbDataAdapter(command);

 

// Before Filling the Dataset from the dataAdapter

// open the OleDbConnection ‘con’ first

if (con.State == ConnectionState.Closed)

{

con.Open();

}

 

 

 

// Then Fill the dataset with the data in the dataAdapter

dataAdapter.Fill(dataSet);

 

con.Close();

}

catch (Exception)

{

//”Unable to Load the DataSet.”

//MessageBox.Show(e.Message, “”, MessageBoxButtons.OK, MessageBoxIcon.Error);

}

 

//Return the Dataset Object

try

{

return dataSet.Tables[0].Rows[0].ItemArray;

}

catch (Exception)

{

return null;

}

 

}

}

}

Now you have created a class1 inside your project. Then go back to your form1 (login form) and double click on the button to enter the following code.

Inside your:

public partial class LoginForm : Form

{

Class1 api;

public LoginForm()

{

InitializeComponent();

api = new Class1();

}

 

private void Login_Click(object sender, EventArgs e)

{

String chk = “SELECT * FROM Administrator WHERE Usname='” + username.Text + “‘ AND Pawd='” + password.Text + “‘”;

object[] data = api.LoadDataArrayByQuery(chk);

if (data == null)

{

MessageBox.Show(“Invalid Login. Try again.”, “Administrator”, MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

 

MessageBox.Show(“Welcome ” + data[2].ToString() + “.”, “Administrator”, MessageBoxButtons.OK, MessageBoxIcon.Information);

this.Hide();

new MainPage().Show();

}

}

}

Thanks for your patience. Please contact me for any question.

Recommended: How use access database with Java

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button