Sample access db connection string

Databases > MS Access
Here are some sample codes for connecting with Access database. In these Examples, you need to change the access database file path and name to your own file path and name.
 
  • Classic ASP connects to MS Access database
ADODB 

set conn = Server.CreateObject("ADODB.Connection")
conn.Provider = "Microsoft.ACE.OLEDB.12.0"
conn.Open Server.MapPath("../db/diveo_version.accdb")
set rs=Server.CreateObject("ADODB.recordset")
<%
' Set the database file path and name
db_file = "h:\root\home\fdfggff***-001\www\db\GtSEPdb.mdb"

' Create the ADO connection string
conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db_file

' Connect to the database
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open conn_str

' Test the connection
If Err.Number <> 0 Then
  Response.Write "Connection failed: " & Err.Description
Else
  Response.Write "Connected successfully"
End If

' Perform a query
query = "SELECT * FROM your_table"
Set rs = conn.Execute(query)

' Process the results
While Not rs.EOF
  ' Do something with the data
  rs.MoveNext
Wend

' Close the connection
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
%>
 
  • PHP connects to MS Access database
<?php
$mdbFilename=$_SERVER["DOCUMENT_ROOT"] . "/housing/admin/123/testy.mdb"; 
echo"data".$mdbFilename;
$user=" ";
$password=" ";
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
if($conn)
{
echo "connected";
}
else(!$conn)
  {
    exit("Connection Failed: " . $conn);
}
?>
// Set the database file path and name
$db_file = $_SERVER["DOCUMENT_ROOT"] . "/housing/admin/123/testy.mdb";

// Create the ODBC connection string
$conn_str = "odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$db_file";

// Connect to the database
$conn = new PDO($conn_str);

// Test the connection
if (!$conn) {
  die("Connection failed: " . print_r(odbc_error(), true));
} else {
  echo "Connected successfully";
}

// Perform a query
$query = "SELECT * FROM your_table";
$result = $conn->query($query);

// Process the results
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
  // Do something with the data
}

// Close the connection
$conn = null;
 
  • ASP.NET connects to MS Access database
<connectionStrings>
    <add name="DatabaseGtSEPdb.accdb1" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=h:\root\home\fdfggff***-001\www\db\GtSEPdb.accdb;Jet OLEDB:Database Password=Gt01;Jet OLEDB:Database Locking Mode=1;Mode=16" providerName="System.Data.OleDb" />
  </connectionStrings>
  •  You can use the System.Data.OleDb namespace. Connect to an Access database using OleDb in ASP.NET:      
// Set the database file path and name
string dbFile = @"h:\root\home\fdfggff***-001\www\db\GtSEPdb.mdb";

// Create the OleDb connection string
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile;

// Connect to the database
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();

// Test the connection
if (conn.State == ConnectionState.Open)
{
  Console.WriteLine("Connected successfully");
}
else
{
  Console.WriteLine("Connection failed");
}

// Perform a query
string query = "SELECT * FROM your_table";
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataReader reader = cmd.ExecuteReader();

// Process the results
while (reader.Read())
{
  // Do something with the data
}

// Close the connection
conn.Close();
 
Note: 
 
Microsoft.Jet.OLEDB.4.0 and Microsoft.Jet.OLEDB.12.0 are both database providers used in Microsoft technologies, but they are different in terms of their functionality and compatibility.
Microsoft.Jet.OLEDB.4.0 is a legacy database provider that was included with older versions of Microsoft Access, such as Access 97 and Access 2000. It is based on the Microsoft Jet Database Engine and is designed to work with Microsoft Access databases in the .mdb file format. It is no longer supported by Microsoft and has been replaced by newer database providers.
Microsoft.Jet.OLEDB.12.0, on the other hand, is a more recent database provider that is included with newer versions of Microsoft Access, such as Access 2007 and Access 2010. It is also based on the Microsoft Jet Database Engine but is designed to work with Access databases in the newer .accdb file format. It provides improved functionality and performance compared to the older Jet providers.
In summary, the main difference between Microsoft.Jet.OLEDB.4.0 and Microsoft.Jet.OLEDB.12.0 is their compatibility with different versions of Microsoft Access and their support for different file formats. The newer provider, Microsoft.Jet.OLEDB.12.0, is recommended for use with newer versions of Microsoft Access and databases in the .accdb file format.