Using Ms Access as a database for Storing and retrieving values
Step 1:
First create new database in Ms Access and name it as your wish.Create a table in Ms Access database
Write a store procedure in Ms Access for inserting values
Select the Create tab in the toolbar at the top of the screen. Then click on the Query Design button under the Other group.
Next click on Queries tab in Show table and click close
Step 1:
First create new database in Ms Access and name it as your wish.Create a table in Ms Access database
Write a store procedure in Ms Access for inserting values
Select the Create tab in the toolbar at the top of the screen. Then click on the Query Design button under the Other group.
Next click on Queries tab in Show table and click close
You will find a Query1 tab right click on it and select sqlview
Write a query for inserting values in it and save it with the name Insertproc
Step 2:
New Project -> Window form application
Form[design].cs looks like this
Form1[design].cs:
Write the following code in Form1.cs
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections;
namespace conctiontoaccess
{
public partial class Form1 : Form
{
OleDbConnection con=new
OleDbConnection (@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\dev6\Documents\Accessdb.accdb");
public Form1()
{
InitializeComponent();
bind();
}
private void
button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new
OleDbCommand("insertproc",con);
cmd.Parameters.Add("@empname",txtname.Text);
cmd.Parameters.Add("@cmpny",
txtcmpny.Text);
cmd.Parameters.Add("@loc",
txtloc.Text);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
int result=cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("inserted successfully");
}
bind();
}
private void bind()
{
OleDbDataAdapter da = new
OleDbDataAdapter("select
* from test", con);
DataTable dt = new
DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
Demo:
No comments:
Post a Comment