GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  Ado.NET
Go To First  |  Previous Question  |  Next Question 
 Ado.NET  |  Question 21 of 31    Print  
What are the steps to connect to a database?

  
Total Answers and Comments: 10 Last Update: April 17, 2008     Asked by: lakshminarayanan 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: nishant2200
 

1. Create a connection. This requires a connection string, which can be given declaratively or put in a well defined place like the .config files. Advantage of keeping in .config files is that it enables use of Connection Pooling by .Net framework, else even one small change in connection string will cause CLR to think it's not the same connection and will instantiate new connection for other request.

2. Open the connection and keep it open until done, typically done as using (con) { //use }

3. If using connected data model, create a SqlCommand object, decorate it with desired command, command type (stored procedure for eg), add any parameters and their values to the command, and then consume the command by using ExcuteReader or ExecuteScalar. In case of ExecuteReader, we will get back a handle to a fast-forward, read only pointer to the recordset. We can also decorate Command object with multiple recordsets in 2.0 and execute one by one (MARS - Multiple Active Record Sets)

4. If using disconnected data model, create a DataAdapter object, decorate it with desired SELECT, INSERT, UPDATE, DELETE commands, add parameters as necessary and then fill up a DataSet or DataTable using the DataAdapter. Subsequent  SQL can be executed using insert, update, delete commands on the dataset.



Above answer was rated as good by the following members:
drsudhak, sushapat, thitnaing, Ag2703
December 20, 2006 05:37:22   #1  
aman        

RE: What are the steps to connect to a database?

THere r many steps ;

Create a data adapter & connection & pass query..


 
Is this answer useful? Yes | No
December 22, 2006 00:23:07   #2  
Balli        

What are the steps to connect to a database?

1) we need to create a connection string like in example dbconn.

2)open the connection

3)create your required like in below example sql

4)create a command object to execute the sql query like dbcomm

5)using Datareader object get the results like dbread

dim dbconn sql dbcomm dbread
dbconn New OleDbConnection( Provider Microsoft.Jet.OLEDB.4.0;
data source & server.mappath( northwind.mdb ))
dbconn.Open()
sql SELECT * FROM customers
dbcomm New OleDbCommand(sql dbconn)
dbread dbcomm.ExecuteReader()


 
Is this answer useful? Yes | No
January 07, 2007 14:11:26   #3  
nishant2200 Member Since: January 2007   Contribution: 3    

RE: What are the steps to connect to a database?

1. Create a connection. This requires a connection string which can be given declaratively or put in a well defined place like the .config files. Advantage of keeping in .config files is that it enables use of Connection Pooling by .Net framework else even one small change in connection string will cause CLR to think it's not the same connection and will instantiate new connection for other request.

2. Open the connection and keep it open until done typically done as using (con) { //use }

3. If using connected data model create a SqlCommand object decorate it with desired command command type (stored procedure for eg) add any parameters and their values to the command and then consume the command by using ExcuteReader or ExecuteScalar. In case of ExecuteReader we will get back a handle to a fast-forward read only pointer to the recordset. We can also decorate Command object with multiple recordsets in 2.0 and execute one by one (MARS - Multiple Active Record Sets)

4. If using disconnected data model create a DataAdapter object decorate it with desired SELECT INSERT UPDATE DELETE commands add parameters as necessary and then fill up a DataSet or DataTable using the DataAdapter. Subsequent SQL can be executed using insert update delete commands on the dataset.


 
Is this answer useful? Yes | NoAnswer is useful 4   Answer is not useful 0Overall Rating: +4    
March 23, 2007 06:28:31   #4  
subhrajyotsna Member Since: March 2007   Contribution: 2    

RE: What are the steps to connect to a database?
1. create a connection string.
2.open the connection.
3.pass a query through command object
4.raed the using datareader.
5.Display the data using any display control.
6.close the connection

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 29, 2007 12:23:27   #5  
sahu Member Since: December 2005   Contribution: 323    

RE: What are the steps to connect to a database?
Hi


Ans:

Step 1. create a connection string.
SqlConnection SqlCon new SqlConnecyion("Connectionstring");


Step 2.open the connection.

SqlCon.Open();

Step 3.pass a query through command object

SqlCommand SqlCom new SqlCommand("sqlQuries" SqlCon);

Step 4.Read the using datareader.

Datareader dr SqlCom.ExcuteReader();
Step 5.Display the data using any display control.

Like DataGrid or GridView or DropDownBox Datalist etc

Step 6.close the connection
SqlCon.Close()

Thanx all
Here Sahu

 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
June 13, 2007 20:06:30   #6  
Venu        

RE: What are the steps to connect to a database?
I created oledb connectionString

Dim StaffConnectionString As String Provider Microsoft.Jet.OLEDB.4.0;Data Source |DataDirectory|Staff.mdb


Dim Conn As New OleDbConnection(StaffConnectionString)


Conn.Open()


Dim myQuery As String select Phone2 from tblStaff where Firstname value


Dim Cmd As New OleDbCommand(myQuery Conn)

Dim dbread Cmd.ExecuteReader()

Here I am getting an exception in ExecuteReader() saying one or more parameters are missing.

Please solve this as quickly as posible


 
Is this answer useful? Yes | No
January 02, 2008 04:20:34   #7  
suneelpachipulusu Member Since: January 2008   Contribution: 3    

RE: What are the steps to connect to a database?
step 1:
first we have to add namespace using system.data.sqlclient;
then connect to database by using this Base class library

step 2:if ur connecting with ur own system(PC) conect server as localhost
sqlconnection con new sql connection("server localhost;uid sa;database northwind");

step 3:if u want to connect as connection oriented we have to give
con.open();

step 4:then type a command
sqlcommand cmd new sqlcommand (select * from products);

step 5:sqldatareader dr cmd.executereader();
while(dr.read())
{
/*wat ever we want to execute we have to write in this loop
}
dr.close();
con.close();

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
January 22, 2008 00:23:37   #8  
rahul.kale Member Since: January 2008   Contribution: 1    

RE: What are the steps to connect to a database?
1. Create Connection object
2. Open Connection
3. Create Command Object.
4. For insert update delete call ExecuteNonQuery() & For Select Create DataReader Object & Call ExecuteReader()
5. Close the Connection


 
Is this answer useful? Yes | No
April 08, 2008 19:14:24   #9  
johnjustin Member Since: March 2008   Contribution: 8    

RE: What are the steps to connect to a database?
1. Implements the base class for data access
2. Create a connection
3. Open that connection
4. Create a data reader and execute the data reader with proper sql statements
You can get the values of the record using the methods GetString GetInt32 ....etc.

 
Is this answer useful? Yes | No
April 17, 2008 03:16:13   #10  
ushalakshmi Member Since: April 2008   Contribution: 40    

RE: What are the steps to connect to a database?
TO Connect to a DatabaseFirst we need to have a namespace like System.Data.Sqlclientthen connect to database as follows1.Create connection using SqlConnection Sqlconnection con new sqlconnection( Connectionstring )2.Open connection con.Open()3.Pass query using command object SqlCommand cmd new sqlCommand( Sqlquery con)4. Read the data using Datareader Datareader rd cmd.ExecuteReader()5. Dispaly the data using display control6. Close the connection con.Close()
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape