Click to See Complete Forum and Search --> : C# and Access connection


preetham.n
July 12th, 2008, 02:14 AM
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();
}
}

ujjwalmeshram
July 12th, 2008, 03:28 AM
Hi,

u can not call the open mehode of oledbconnection here

Instead u should try this
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();
}

}

JonnyPoet
July 12th, 2008, 01:23 PM
You cannot have code in a class without having it in a method or in the Constructor.


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.)

preetham.n
July 14th, 2008, 12:20 AM
Thanks guys, and Jonny will keep in mind all your suggestions .Thanks again.

JonnyPoet
July 14th, 2008, 05:47 AM
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