| | |
Help for a Newbie
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 14
Reputation:
Solved Threads: 1
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
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)
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); } } }
•
•
•
•
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 =).
Take a closer look at the connection string generated from the IDE...
•
•
Join Date: Mar 2009
Posts: 14
Reputation:
Solved Threads: 1
Thank you for the replies - had to wait until I got back to work to reply.
The exception is
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.
The exception is
C# Syntax (Toggle Plain Text)
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.
•
•
Join Date: Mar 2009
Posts: 14
Reputation:
Solved Threads: 1
These are the connection strings i've tried so far...
C# Syntax (Toggle Plain Text)
//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");
Try this:
Call it with
c# Syntax (Toggle Plain Text)
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
c# Syntax (Toggle Plain Text)
string connStr = BuildAccessConnectionString(@"H:\Initiatives4.mdb", string.Empty, string.Empty, string.Empty);
•
•
Join Date: Mar 2009
Posts: 14
Reputation:
Solved Threads: 1
Thanks for the help Scott, unfortunatley i get these errors, might be something I'm doing wrong.
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'
C# Syntax (Toggle Plain Text)
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'
Last edited by Hangfire; Mar 10th, 2009 at 12:15 pm.
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- Questions about building a system (was: newbie) (Troubleshooting Dead Machines)
- Best free C/C++ compiler for a newbie? (C++)
- help newbie alert needs help with login page (ASP.NET)
- newbie needs help, basic mfc stuff (C++)
- Hello, newbie here... (Geeks' Lounge)
- Book For Newbie (C++)
- Newbie - how do I start C++ programming? (C++)
- PHP newbie, project feasibility (PHP)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the C# Forum
- Previous Thread: Combo box trouble
- Next Thread: Excel and C#
Views: 854 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






