|
-
July 12th, 2008, 02:14 AM
#1
C# and Access connection
Hi all,
Need a little help, a very basic one at that.
Am new to C# and the Visual c# 2005 Express Edition IDE. Am trying to connect to an access database through c#, but to no joy (faltering at the first step).
Below is the code that I have typed in. Even this basic code gives me problems.
First the IDE does not recognise "conn", because i guess the open() method sgould show up automatically on intellisense when i type a dot after conn but I dont get anything. When I compile the error comes up at the open method where it says Invalid token '(' in class.
I have added reference to system.data dll.
I know its basic stuff but can some one help?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data; // State variables
using System.Data.ADO; // Database
using System.Globalization; // Date
namespace Ticket_Order
{
public class dbFunctions
{
string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
}
}
-
July 12th, 2008, 03:28 AM
#2
Re: C# and Access connection
Hi,
u can not call the open mehode of oledbconnection here
Instead u should try this
Code:
public class dbFunctions
{
string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
OleDbConnection conn;
public void openConnection()
{
conn = new OleDbConnection(connString);
conn.Open();
}
}
Last edited by ujjwalmeshram; July 12th, 2008 at 03:31 AM.
-
July 12th, 2008, 01:23 PM
#3
Re: C# and Access connection
You cannot have code in a class without having it in a method or in the Constructor.
 Originally Posted by preetham.n
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data; // State variables
using System.Data.ADO; // Database
using System.Globalization; // Date
namespace Ticket_Order
{
public class DbFunctions {
string connString = string.Empty
//string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
// the constructor
public DBFunctions(){
connString = Properties.Settigs.ConnString;
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
}
}
}
Here I have shown you how to do this in the Constructor of your DbFunctions class.
For getting this working do the Connectionstring in the settings of your program as programm defined setting.
This has good reasons regarding using your program on different machines or different adresses of your database without needing to recompile your code. ( Settings can be changed from outside compiled code Look to MSDN help regarding 'Settings'. I also would recommand you to reread 'naming conventions in C# as public classes should begin with a big letter and the name Functions is no good name for a class. We are not in VB 6.0 where you created code moduls. If you want to have a class that only provides methods then why not using static methods so you can use them without creating instances of your class ? But IMHO you simple want to build a sort of DatabaseLayer class which allows you to connect to your database and contains all methods needed to read, write, delete data in your database, isn't it ?
BTW in futuer I would not answer to posts not using forum habits like code tags. ( look to the bottom of my post how to do it.)
Last edited by JonnyPoet; July 14th, 2008 at 06:05 AM.
Reason: Corrected Typo
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
July 14th, 2008, 12:20 AM
#4
Re: C# and Access connection
Thanks guys, and Jonny will keep in mind all your suggestions .Thanks again.
-
July 14th, 2008, 05:47 AM
#5
Re: C# and Access connection
 Originally Posted by preetham.n
Thanks guys, and Jonny will keep in mind all your suggestions .Thanks again.
Look in my code again There was an error in the constructor. Its corrected now
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|