Click here to Skip to main content
15,891,943 members
Articles / Programming Languages / C#
Tip/Trick

Simply Connect to and Modify Database

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
3 Jul 2010CPOL 15.6K   134   1   3
Connect to a database and also be able to modify the table

Introduction

I've searched all over the internet for a simple tutorial on how to connect to a database, and also modify it. All I found were tutorials that were far too complex, and just had way to much code. Here I offer a tutorial with only the code needed to connect and manipulate a database.

Using the Code

There is no exception handling within the code. That part will be left up to the ones using the tutorial for their own benefit. I just offer descriptions on how to complete certain processes.

C#
// LOCATION OF THE DATABASE

private static string accessConn = 
  @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\HPC\Documents\MyDatabase.mdb";

// DEPENDING ON WHAT YOU WOULD LIKE TO DO WITH THE TABLE.
private static string accessSelect = "SELECT * FROM Table1";

// DATABASE OBJECTS
private static OleDbConnection conn;
private static OleDbCommand comm;
private static OleDbDataAdapter adapter;
private static DataSet ds;
private static DataTable dt;

// CONNECT TO THE DATABASE
conn = new OleDbConnection(accessConn);
conn.Open();

// CREATE THE COMMAND OBJECT
comm = new OleDbCommand(accessSelect, conn);

// CREATE THE DATABASE ADAPTER
adapter = new OleDbDataAdapter(accessSelect, conn);

// MAKE SURE THE ADAPTER CAN HANDLE ALL QUERY COMMANDS (SELECT, DELETE, ECT.)
new OleDbCommandBuilder(adapter);

// CREATE DATASET OBJECT
ds = new DataSet();

// FILL THE DATASET
adapter.Fill(ds, "Table1");

// CREATE A DATATABLE
DataTable dt = ds.Tables["Table1"];

// CREATE A NEW DATAROW AND ASSIGN VALUES TO THE DESIRED COLUMNS
DataRow dr = dt.NewRow();
dr["Column1"] = "TestInsertMethod";

// ADD THE NEW ROW TO THE DATATABLE
dt.Rows.Add(dr);

// UPDATE THE ADAPTER AND/OR UPDATE THE DATABASE
adapter.Update(dt);

// CLOSE THE CONNECTION
conn.Close();

Points of Interest

When creating the Command Object, make sure to add all other command types as well if you wish to do more than to add to the datatable.

History

  • 07/03/2010 - Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Network Administrator
United States United States
Demitrius P. Wheelwright

Comments and Discussions

 
General[My vote of 1] Not an article or tip Pin
Not Active4-Jul-10 2:56
mentorNot Active4-Jul-10 2:56 
GeneralComments Pin
Md. Marufuzzaman3-Jul-10 20:25
professionalMd. Marufuzzaman3-Jul-10 20:25 
GeneralNot an article Pin
#realJSOP3-Jul-10 11:52
mve#realJSOP3-Jul-10 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.