Help for a Newbie

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 14
Reputation: Hangfire is an unknown quantity at this point 
Solved Threads: 1
Hangfire Hangfire is offline Offline
Newbie Poster

Help for a Newbie

 
0
  #1
Mar 9th, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Help for a Newbie

 
1
  #2
Mar 9th, 2009
Tell us what the exception is.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

Re: Help for a Newbie

 
0
  #3
Mar 9th, 2009
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 =).
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,443
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 626
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Help for a Newbie

 
0
  #4
Mar 9th, 2009
Originally Posted by danielernesto View Post
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...
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 14
Reputation: Hangfire is an unknown quantity at this point 
Solved Threads: 1
Hangfire Hangfire is offline Offline
Newbie Poster

Re: Help for a Newbie

 
0
  #5
Mar 10th, 2009
Thank you for the replies - had to wait until I got back to work to reply.

The exception is
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 14
Reputation: Hangfire is an unknown quantity at this point 
Solved Threads: 1
Hangfire Hangfire is offline Offline
Newbie Poster

Re: Help for a Newbie

 
0
  #6
Mar 10th, 2009
These are the connection strings i've tried so far...
  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");
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,443
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 626
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Help for a Newbie

 
0
  #7
Mar 10th, 2009
Try this:
  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
  1. string connStr = BuildAccessConnectionString(@"H:\Initiatives4.mdb", string.Empty, string.Empty, string.Empty);
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 14
Reputation: Hangfire is an unknown quantity at this point 
Solved Threads: 1
Hangfire Hangfire is offline Offline
Newbie Poster

Re: Help for a Newbie

 
0
  #8
Mar 10th, 2009
Thanks for the help Scott, unfortunatley i get these errors, might be something I'm doing wrong.

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 14
Reputation: Hangfire is an unknown quantity at this point 
Solved Threads: 1
Hangfire Hangfire is offline Offline
Newbie Poster

Re: Help for a Newbie

 
0
  #9
Mar 10th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 854 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC