can not locate database file

G

gamall

hi
i use visual studio 2005 c# to access database (access 2007 enterprise
edition). i copied the following code from a book. However i get an
error in runtime. that the program can not locat the "filename.mdb" .
however my database file name is " filename.accdb"
how do i fix this problem? i am running this program on vista home
edition.
thanks
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string oledbConnectionString = "Provider =
Microsoft.ACE.OLEDB.12.0;Data Source=" +
@"C:\Users\gamalt\Documents\" + "Database2.accdb;";
string queryString = (" select id from Database2.table1");

using (OleDbConnection connection = new OleDbConnection
(oledbConnectionString))
{
OleDbCommand command = new OleDbCommand(queryString,
connection);

connection.Open();
OleDbDataReader reader = command.ExecuteReader(); // i
get the error here

while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
reader.Close();
}


}
}
}
 
D

Douglas J. Steele

Don't know about C#, but the @ in the connection string would be incorrect
in VBA.
 
S

Stefan Hoffmann

hi,
i use visual studio 2005 c# to access database (access 2007 enterprise
edition). i copied the following code from a book. However i get an
error in runtime. that the program can not locat the "filename.mdb" .
however my database file name is " filename.accdb"
Take a look at connectionstrings.com for correct connectin strings.
string oledbConnectionString = "Provider =
Microsoft.ACE.OLEDB.12.0;Data Source=" +
@"C:\Users\gamalt\Documents\" + "Database2.accdb;";
Don't mix normalstring literals and verbatim ones. Simply escape the \\.
connection.Open();
OleDbDataReader reader = command.ExecuteReader(); // i
get the error here
Funny, you should get the error in the line above. Try something like this:

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// see http://connectionstrings.com/access-2007
string connectionString =
"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" +
"C:\\Users\\gamalt\\Documents\\Database2.accdb;";

OleDbConnection connection =
new OleDbConnection(connectionString);

try
{
connection.Open();
ReadFromConnection(connection);
connection.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}

static void ReadFromConnection(OleDbConnection connection)
{
string queryString = (" select id from Database2.table1");
OleDbCommand command = new OleDbCommand(queryString, connection);
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
reader.Close()
}
}
}


mfG
--> stefan <--
 
D

David W. Fenton

:
i use visual studio 2005 c# to access database (access 2007
enterprise edition). i copied the following code from a book.
However i get an error in runtime. that the program can not locat
the "filename.mdb" . however my database file name is "
filename.accdb" how do i fix this problem? i am running this
program on vista home edition.

I think the error is referring to the SQL string:
string queryString = (" select id from Database2.table1");

You don't need to specify the database name in front of the table
name -- this should suffice:

string queryString = (" select id from table1");

When you specify it the way you did, ACE/Jet assumes it's an MDB
file, apparently -- but you don't need it at all, since your connect
string fully identifies the source database.
 
J

James A. Fortune

Stefan said:
string queryString = (" select id from Database2.table1");

Stefan,

Would it be possible/preferable to use LINQ instead?

James A. Fortune
(e-mail address removed)
 
S

Stefan Hoffmann

hi James,
Would it be possible/preferable to use LINQ instead?
Of course, it's possible. If it's preferable depends on your
needs/actual problem. But these questions are really better asked in

microsoft.public.dotnet.languages.csharp




mfG
--> stefan <--
 
L

LCL

gamall said:
hi
i use visual studio 2005 c# to access database (access 2007 enterprise
edition). i copied the following code from a book. However i get an
error in runtime. that the program can not locat the "filename.mdb" .
however my database file name is " filename.accdb"
how do i fix this problem? i am running this program on vista home
edition.
thanks
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string oledbConnectionString = "Provider =
Microsoft.ACE.OLEDB.12.0;Data Source=" +
@"C:\Users\gamalt\Documents\" + "Database2.accdb;";
string queryString = (" select id from Database2.table1");

using (OleDbConnection connection = new OleDbConnection
(oledbConnectionString))
{
OleDbCommand command = new OleDbCommand(queryString,
connection);

connection.Open();
OleDbDataReader reader = command.ExecuteReader(); // i
get the error here

while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
reader.Close();
}


}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top