Can you explain ADO.Net steps to connect to SQL Server database?( ADO.NET interview questions)

A connected famous ADO.NET interview question which is asked is Explain What dataset is a disconnected architecture means?


datasetTo connect using ADO.NET we need to perform five steps( Import , Connect , Command , dataset and close):-

Step 1 : Import namespace

In ASP to connect to the data the first we need to import the “System.Data.SqlClient” namespace on our code.

“using System.Data.SqlClient;”

Step 2 : Create connection object

The next step is to connect to the database. To connect to the database we need to create the “SqlConnection” object. To create “SqlConnection” we need the connection string. Connection string provides the name of the server , database and security details. Once the connection string is provided we need to open the connection by calling the “Open” method of “SqlConnection” class.

SqlConnection con = new SqlConnection(“Server=ServerNameDatabase=Yourdatabasename Trusted_Connection=True”);

Step 3 :- Create the command object

Once the connection is created we can now fire SQL using that connection. To fire SQL we need to create the “SqlCommand” object and provide the SQL and the connection object created in the first step. In this SQL are searching customer record using “id” field.

SqlCommand cmd = new SqlCommand(“select * from customer where id=1001”,con);

Step 4 :- Load dataset object

Now that we have written the command object it’s time to fire the SQL command. When the SQL command is fired it returns a dataset object which is nothing but data in columns and rows format. To fill dataset we need to use the “SqlDataAdapter” class.

DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();

Step 5 :- Close the connection.

Once you have got your records and you are done with your operation we need to close the connection to release the memory and resources.



About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- Learn AngularJS Step by Step:- Learn Design Pattern in 8 hours:- Learn C# and .NET in 60 days:- Learn MSBI in 32 hours:- Learn SharePoint Step by Step in 8 hours:-
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s