943,878 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1458
  • C# RSS
Mar 9th, 2009
0

Help for a Newbie

Expand Post »
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

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Data.OleDb;
  3.  
  4. class OleDbTest{
  5.  
  6. public static void Main()
  7. {
  8. //create the database connection
  9. OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb");
  10.  
  11. //create the command object and store the sql query
  12. OleDbCommand aCommand = new OleDbCommand("select * from tbl_users", aConnection);
  13. try
  14. {
  15. aConnection.Open();
  16.  
  17. //create the datareader object to connect to table
  18. OleDbDataReader aReader = aCommand.ExecuteReader();
  19. Console.WriteLine("This is the returned data from emp_test table");
  20.  
  21. //Iterate throuth the database
  22. while(aReader.Read())
  23. {
  24. Console.WriteLine(aReader.GetInt32(0).ToString());
  25. }
  26.  
  27. //close the reader
  28. aReader.Close();
  29.  
  30. //close the connection Its important.
  31. aConnection.Close();
  32. }
  33.  
  34. //Some usual exception handling
  35. catch(OleDbException e)
  36. {
  37. Console.WriteLine("Error: {0}", e.Errors[0].Message);
  38. }
  39. }
  40. }
Similar Threads
Reputation Points: 14
Solved Threads: 12
Junior Poster in Training
Hangfire is offline Offline
62 posts
since Mar 2009
Mar 9th, 2009
1

Re: Help for a Newbie

Tell us what the exception is.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 9th, 2009
0

Re: Help for a Newbie

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 =).
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007
Mar 9th, 2009
0

Re: Help for a Newbie

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...
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Mar 10th, 2009
0

Re: Help for a Newbie

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

The exception is
C# Syntax (Toggle Plain Text)
  1. Exception System.Security.SecurityException was thrown in debuggee:
  2. Request for the permission of type 'System.Data.OleDb.OleDbPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
  3.  
  4. OpenConnection()
  5. Open()
  6. 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.
Reputation Points: 14
Solved Threads: 12
Junior Poster in Training
Hangfire is offline Offline
62 posts
since Mar 2009
Mar 10th, 2009
0

Re: Help for a Newbie

These are the connection strings i've tried so far...
C# Syntax (Toggle Plain Text)
  1. //OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb");
  2.  
  3. //OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb;Jet OLEDB:System Database=system.mdw;");
  4.  
  5. //OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb;Jet OLEDB:System Database=system.mdw;Trusted_Connection=yes");
  6.  
  7. OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\Initiatives4.mdb;Jet OLEDB:System Database=system.mdw;Trusted=yes");
Reputation Points: 14
Solved Threads: 12
Junior Poster in Training
Hangfire is offline Offline
62 posts
since Mar 2009
Mar 10th, 2009
0

Re: Help for a Newbie

Try this:
c# Syntax (Toggle Plain Text)
  1. public static string BuildAccessConnectionString(string Filename, string Username, string Password, string DatabasePassword)
  2. {
  3. return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';User Id={1};Password={2};Jet OLEDB:Database Password={3};",
  4. Filename.Replace("'", "''"),
  5. Username,
  6. Password,
  7. DatabasePassword);
  8. }

Call it with
c# Syntax (Toggle Plain Text)
  1. string connStr = BuildAccessConnectionString(@"H:\Initiatives4.mdb", string.Empty, string.Empty, string.Empty);
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Mar 10th, 2009
0

Re: Help for a Newbie

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

C# Syntax (Toggle Plain Text)
  1. Argument '2': cannot convert from 'string' to 'System.Data.OleDb.OleDbConnection'
  2.  
  3. The best overloaded method match for 'System.Data.OleDb.OleDbCommand.OleDbCommand(string, System.Data.OleDb.OleDbConnection)' has some invalid arguments
  4.  
  5. 'string' does not contain a definition for 'Open'
  6.  
  7. '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'
Last edited by Hangfire; Mar 10th, 2009 at 12:15 pm.
Reputation Points: 14
Solved Threads: 12
Junior Poster in Training
Hangfire is offline Offline
62 posts
since Mar 2009
Mar 10th, 2009
0

Re: Help for a Newbie

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?
Reputation Points: 14
Solved Threads: 12
Junior Poster in Training
Hangfire is offline Offline
62 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Combo box trouble
Next Thread in C# Forum Timeline: Excel and C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC