Hi,
Your code is correct and working fine....
The problem is when you are in
Debug Mode or
Release mode...
Database file created automatically when the program is running.. and the data you have inserted is stored in that folder..inside
BIN
Please check the Debug folder inside Bin folder, you will fin the Access database file with your inserted data...
Below the working code...for windows/ desktop application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFDS
{
public partial class AccessDB : Form
{
OleDbConnection conn = new OleDbConnection();
String connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Data\\BookStore.accdb;Persist Security Info=True";
public AccessDB()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("Insert into product(title,descrtion,type,vendor,price,isbn) values(@title,@descrtion,@type,@vendor,@price,@isbn)", conn);
cmd.Parameters.AddWithValue("@title", textBox1.Text);
cmd.Parameters.AddWithValue("@descrtion", textBox2.Text);
cmd.Parameters.AddWithValue("@type", textBox3.Text);
cmd.Parameters.AddWithValue("@vendor", textBox4.Text);
cmd.Parameters.AddWithValue("@price", textBox5.Text);
cmd.Parameters.AddWithValue("@isbn", textBox6.Text);
int rows = cmd.ExecuteNonQuery();
if (rows != 0)
{
MessageBox.Show("Inserted");
}
else
{
MessageBox.Show("Insertion failure");
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
string sql = "SELECT * FROM product";
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
adapter.Fill(ds);
int dd = ds.Tables[0].Rows.Count;
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
private void AccessDB_Load(object sender, EventArgs e)
{
conn.ConnectionString = connection;
}
}
}
The database file is stored in the
Bin\Debug or
BIN\Release directory
Thanks,
Ullas Krishnann