Click here to Skip to main content
Licence CPOL
First Posted 3 Jul 2010
Views 4,080
Downloads 52
Bookmarked 0 times

Simply Connect to and Modify Database

By | 3 Jul 2010 | Article
Connect to a database and also be able to modify the table
 
Part of The SQL Zone sponsored by
See Also

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.

// 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)

About the Author

demipaul

Network Administrator

United States United States

Member

Demitrius P. Wheelwright

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[My vote of 1] Not an article or tip PinmvpMark Nischalke2:56 4 Jul '10  
GeneralComments PinmvpMd. Marufuzzaman20:25 3 Jul '10  
GeneralNot an article PinmemberJohn Simmons / outlaw programmer11:52 3 Jul '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 3 Jul 2010
Article Copyright 2010 by demipaul
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid