
Connecting To a Database
The hardest part of connecting to a database is remembering
how to set up the command. Here are some examples.
Access
Connection to an Access database is through Jet’s OLE DB
interface.
using System.Data:
using System.Data.OleDb;
OleDbConnection dbConnection = new OleDbConnection();
dbConnection.ConnectionString = “Provider=Microsoft.JET.OLEDB.4.0;data “ +
@”source=c:\db\example.mdb”
dbConnection.Open();
// do something...
// Finished with the database.
dbConnection.Close();
SQL Server
Connection to a SQL Server database is through its own data
provider.
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
SqlConnection dbConnection = new SqlConnection( “server=(local); uid=sa; pwd=;
database=example”);
dbConnection.Open();
// do something...
// Finished with the database.
dbConnection.Close();


|