954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help for a Newbie

Please help, I have copied some code to connect to a MSAccess database. However when I try to run it I get a Security Exception message.
Weird thing is, in the IDE I can use database explorer to find, open and test the connection to the database.
The database is stored on a shard network drive and unfortunatley I cannot access my C: drive

using System;
using System.Data.OleDb;

class OleDbTest{

public static void Main()
{
//create the database connection
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb");

//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("select * from tbl_users", aConnection);
try
	{
		aConnection.Open();
		
		//create the datareader object to connect to table
		OleDbDataReader aReader = aCommand.ExecuteReader();
		Console.WriteLine("This is the returned data from emp_test table");
		
		//Iterate throuth the database
		while(aReader.Read())
		{
			Console.WriteLine(aReader.GetInt32(0).ToString());
		}
	
		//close the reader 
		aReader.Close();
		
		//close the connection Its important.
		aConnection.Close();
	}
	
	//Some usual exception handling
	catch(OleDbException e)
	{
		Console.WriteLine("Error: {0}", e.Errors[0].Message);
	}
	}
}
Hangfire
Junior Poster in Training
62 posts since Mar 2009
Reputation Points: 14
Solved Threads: 12
 

Tell us what the exception is.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

In wich MSAcces version are you develop??, couse if it's in 2007 your connection it's wrong, if it´s not then you may probably have to add Trusted connection to the connection string =).

danielernesto
Newbie Poster
16 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 
In wich MSAcces version are you develop??, couse if it's in 2007 your connection it's wrong, if it´s not then you may probably have to add Trusted connection to the connection string =).

I think he nailed this on the head, your connection string is probably wrong. If you connected to it in the IDE then it offered you to store the connection string where you could reference it as an application setting, but you're manually providing a connection string.

Take a closer look at the connection string generated from the IDE...

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Thank you for the replies - had to wait until I got back to work to reply.

The exception is

Exception System.Security.SecurityException was thrown in debuggee:
Request for the permission of type 'System.Data.OleDb.OleDbPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

OpenConnection()
Open()
Main() - \\boffil01.group.net\lv14942\SharpDevelop Projects\textconn\Program.cs:17,3


Unfortunatley I'm stuck with Access97 at the moment, I will try to find out about adding Trusted to the connection. Thanks for the tip.

I tried to find out where the connection string was from the Database browser, that was the reason for checking it out, but I can't seem to find it - any pointers?

Once again thank you, this has been driving me nuts for a couple of weeks.

Hangfire
Junior Poster in Training
62 posts since Mar 2009
Reputation Points: 14
Solved Threads: 12
 

These are the connection strings i've tried so far...

//OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb");

//OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb;Jet OLEDB:System Database=system.mdw;");

//OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb;Jet OLEDB:System Database=system.mdw;Trusted_Connection=yes");

OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb;Jet OLEDB:System Database=system.mdw;Trusted=yes");
Hangfire
Junior Poster in Training
62 posts since Mar 2009
Reputation Points: 14
Solved Threads: 12
 

Try this:

public static string BuildAccessConnectionString(string Filename, string Username, string Password, string DatabasePassword)
    {
      return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';User Id={1};Password={2};Jet OLEDB:Database Password={3};",
                                   Filename.Replace("'", "''"),
                                   Username,
                                   Password,
                                   DatabasePassword);
    }


Call it with

string connStr = BuildAccessConnectionString(@"H:\Initiatives4.mdb", string.Empty, string.Empty, string.Empty);
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Thanks for the help Scott, unfortunatley i get these errors, might be something I'm doing wrong.

Argument '2': cannot convert from 'string' to 'System.Data.OleDb.OleDbConnection'

The best overloaded method match for 'System.Data.OleDb.OleDbCommand.OleDbCommand(string, System.Data.OleDb.OleDbConnection)' has some invalid arguments 

'string' does not contain a definition for 'Open' 

'string' does not contain a definition for 'Close'


connStr.Open() is highlighted,
Have tried changing the string to OleDbConnection, but then I get Cannot implicitly convert type 'string' to 'System.Data.OleDb.OleDbConnection'

Hangfire
Junior Poster in Training
62 posts since Mar 2009
Reputation Points: 14
Solved Threads: 12
 

Ignore my last

Realised I needed to add...
OleDbConnection aConnection = new OleDbConnection(connStr);

Unfortunatley still get the same security message, is this something to do with full trust?

Hangfire
Junior Poster in Training
62 posts since Mar 2009
Reputation Points: 14
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You