Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
C#
String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;_
	Data Source=..\\..\\myDB.mdb;User ID=Admin;Password=";

OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
string query = "SELECT studentID, firstName, lastName, birthDate, _
				address, contactNo FROM studentInfo";
OleDbDataAdapter oleDA = new OleDbDataAdapter(query,conn);

Where to type this coding for creating a data set
Posted
Updated 19-Mar-11 23:12pm
v2

It depends on what you are going to do with it.
Under normal circumstances, you do not open a database connection and leave it open: You should open, do your transactions, and close it again - database connections are a scarce resource and should not be held open when they are not in use as it can prevent other users from getting in.

What are you trying to do? That will affect where you need to put your code.
 
Share this answer
 
Comments
Аslam Iqbal 20-Mar-11 6:05am    
good thinking. my 5
Albin Abel 20-Mar-11 8:09am    
Good suggestion. My 5
Your question not clear. You can have this code wherever you need to access data. Remember to close the connection which you opened.

To get dataset from the adapter you can use the data adapter's Fill method which has four overloaded options.

C#
DataSet ds = new DataSet("Test");
oleDA.Fill(ds);


Now the question is Preserve these objects or not. I think that is the 'Where' part of your question. Well it depends on necessity. Adapter and the connection string you could keep global scope. But in view of dataset, If many places you need to use this then you can store dataset in a form's global variable otherwise no need to preserve. But open the connection whenever need and close it after used.

As a general rule, bring all the required data from the database to the memory, then use business logic to access specific data. Keep in mind there should be a balance.
 
Share this answer
 
Hi umasankari1234,

You can add it in the Load_Form event handler.

I hope this help,
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900