English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need to connect my databasae (MS Access) with ASP.NET 1.1 using ADO.net , a website explains how to establish such a connection but the problem is

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Customers", conn
%>

I don't know where should I paste this code?

2006-06-08 02:48:02 · 2 answers · asked by raflawy 3 in Computers & Internet Programming & Design

2 answers

Hi, your connection string is correct but your missing some parts..

Take a look at the below code. I took out what I use in my ADO.NET library for this example..

I have included a summary here:
http://www.m0interactive.com/archives/2006/06/13/connect_to_a/


Code:
=========================================
try
{
string database = "PersonDatabase.mdb";
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + database;
OleDbConnection dbConnection = new OleDbConnection(connectionString);

string query = "SELECT FirstName FROM PersonTable WHERE PersonID = 1";
OleDbCommand command = new OleDbCommand(query, dbConnection);
string FirstName = "";
dbConnection.Open();
OleDbDataReader reader = command.ExecuteReader();

if (reader.Read())
FirstName = reader.GetString(0).ToString();
else
FirstName = "Empty";

dbConnection.Close();

Console.WriteLine(FirstName);
}
catch (Exception e)
{
return e.ToString();
}
========================

Good Luck

2006-06-13 19:01:38 · answer #1 · answered by ? 6 · 1 0

Even the connection string stated you is correct but it will confuse you while programming. I'll tell you one alternative, just try to implement it. In your asp.net application, double click on form (.aspx page) then you will be presented (.aspx.cs page) which is considered as code behind for that page and here .cs represent c# programming (it would be .vb if you are using vb.net as the code behind)

if you are using C#, add the namespace at the top of the .cs file
using System.Data.OleDb (to use OLEDB dataprovider)

Then write this statement before the Page_Load(...) event to declare a connection variable
OleDbConnection con=new OleDbConnection();

Here may be one of the following:
(where mydb.mdb is my access database, for your program write the complete path for your database.)

For Standard security:
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;User Id=admin;Password=;"


With password:
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet OLEDB:Database Password=MyDbPassword;"

(Note: Don't confuse to use @ it is used to avoid escape sequences like \n, \t and so on..... Just forget about that one)

Try it. All the Best
(For any connection string refer the site www.connectionstrings.com)

2006-06-08 10:40:05 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers