Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




C#/.NET interview questions: - What are the steps included to get a collection of records from database in C#?

1.  First you need SQL connection string

  String
  connectionString="Server=ServerAddress;Database=myDataBase;UserId=myUsername;
  Password=myPassword;";
  
2.  Create an object of SqlConnection class as follows.

  SqlConnection connection = new SqlConnection(connectionString)

3.  Create an object of SQLCommand class as follows and assign query and Connection to it as follows.

  SqlCommandcmd = new SqlCommand(“Select * from Employee” , connection);

4.  Create and initialize SqlDataAdatper and pass command object to it.

  SqlDataAdapter da = new SqlDataAdapter(cmd);

5.  Create and initialize datatable object which will store your records set

  DataTabledataTable=new DataTable();

6.  Use the fill method of Adapter class to fire the query and store the resulset in the datatable created in        above step.

  da.Fill(dataTable);

7.  Now you can use this datable and bind it to grid or can do other things.

For technical trainings on various topics like WCF, MVC, Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com
 
 For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla
 
 Also see C# and .NET interview questions video on improving Database Design: -
  

Helpful Website Url
http://www.questpond.com/
Share this article   |    Print    |    Article read by 4071 times
Author:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles:
Related Interview Questions: