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;
namespace WindowsFormsApplication
{
public partial class EmployeeInfo : Form
{
OleDbDataAdapter accountsTableAdapter = new OleDbDataAdapter();
DataSet accountsDataSet = new DataSet();
public EmployeeInfo()
{
InitializeComponent();
}
private void EmployeeInfo_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'msaccessDataSet.EmployeeInfo' table. You can move, or remove it, as needed.
this.employeeInfoTableAdapter.Fill(this.msaccessDataSet.EmployeeInfo);
LoadData();
}
public void LoadData()
{
string connetionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
string sql = "SELECT * FROM EmployeeInfo";
OleDbConnection connection = new OleDbConnection(connetionString);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "EmployeeInfo");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "EmployeeInfo";
}
private void btnInsert_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
String name = txtName.Text;
int Age = Convert.ToInt32(txtAge.Text);
String Qualification = txtQualification.Text;
string Date = Convert.ToString(dateTimePicker1.Value.ToShortDateString());
string qry = "INSERT INTO EmployeeInfo([Name],[Qualification],[Age],[Date]) Values(@Name,@Qualification,@Age,@Date)";
Console.Write(qry);
OleDbCommand cmd = new OleDbCommand(qry);
cmd.Connection = conn;
conn.Open();
if (conn.State == ConnectionState.Open)
{
cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = name;
cmd.Parameters.Add("@Qualification", OleDbType.VarChar).Value = Qualification;
cmd.Parameters.Add("@Age", OleDbType.Integer).Value = Age;
cmd.Parameters.Add("@Date", OleDbType.VarChar).Value = Date;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Data Inserted");
conn.Close();
LoadData();
//to clear the text field (name)
txtName.Text = "";
txtQualification.Text = "";
txtAge.Text = "";
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Source);
conn.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
}
private void Close_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnDelete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(item.Index);
}
}
}
}This is a simple program to access data from server using C#.