Hi everyone,
please i am writing a program in C# for connecting to a microsoft access database using visual studio 2005 and ADO.NET. i want to be able to use Dataset to update,delete,insert and edit records in the database.please how do i go about this.I already have aform with a list box,a text box and four buttons for inserting,updating,deleting, and inserting records but i havent been able to get the connection to the database. i am a little bit confused if have to use a connection string and a dataAdapter to get a connection to the database everytime i want to perform each action like updating, deleting e.t.c and please how do i write the cade for the update,delete,edit and insert statements as i am a little bit new to ADO.NET and c# platform.below is the code i have written so far but it dosent work. can anyone please help me in modifying/correcting the codes.the the name of my access database is BookCSharp with a table name Books.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
namespace DBConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string dbconnection ="Provider=Microsoft.Jet.OLEDB.4.0;" +@"data source=BookCSharp.mdb";
string dbcommand = "INSERT into Books (Title, Pages, AuthorBook) " + " VALUES ('C# good','300',OmosJigga)";
OleDbDataAdapter DataAdapterTest = new OleDbDataAdapter (dbcommand, dbconnection);
DataSet BooksDataSet = new DataSet();
DataAdapterTest. Fill(BooksDataSet);
DataTable dtBooks = BooksDataSet.Tables[0];
}
private void button2_Click(object sender, EventArgs e)
{
string dbconnection2 = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=BookCSharp.mdb";
string command2 = "UPDATE Books SET Title = 'Omos' WHERE Publisher = 'O,Rielly'";
OleDbDataAdapter Data2 = new OleDbDataAdapter(command2,dbconnection2);
DataSet b = new DataSet();
Data2.Fill(b);
}
private void button3_Click(object sender, EventArgs e)
{
string dbconnection1 = "Provider=Microsoft.Jet.OLEDB.4.0;" +@"data source=BookCSharp.mdb";
string command1 = "Delete from Books where (Title = ? AND Pages = ? AND AuthorKey = ? AND Publisher = ?)";
OleDbDataAdapter Data1 = new OleDbDataAdapter(command1,dbconnection1);
DataSet a = new DataSet();
Data1.Fill(a);
DataTable aBook = a.Tables[0];
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
thanks.