How to connect to Database in C# web application

18,843

Solution 1

I would look at Microsofts ORM (Object Realtionship Mapping) tool called Entity Framework. You pretty much add a new Entity Framework Model into your Project, and then you can select all the table and stored procedures that you want, this then give you the accessibility to access your data via object rather than Datasets.

Datasets are not used much these day in new projects, due to them being hard to change down the line and there is a need for many changes down the line to happen, and what people having been telling you is right about them not being very extensible.

Checkout the link below for Entity Framework.

Entity Framework Tutorial

Solution 2

You can use SqlDataReader, it's the fastest way to retrieve data in .NET.

//ensures the SQLconnection will be closed...
using (SqlConnection connection =
           new SqlConnection(connectionString))
{
    SqlCommand command =
        new SqlCommand("select * from MyTable", connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    // Reads data
    while (reader.Read())
    {
        Response.Write(String.Format("{0}, {1}",
            reader[0], reader[1]));
    }

    // Close dataReader after use...
    reader.Close();
}

Solution 3

May be Data Abstract can be a good way for you

Solution 4

i would recommend you to go through these articles as they describes best practices for data access in ASP.NET.

http://msdn.microsoft.com/en-us/library/ms971481.aspx

http://msdn.microsoft.com/en-us/library/ee817654.aspx

The use of DataSet, DataTable or SqlDataReader, all depends on your situation. You need to analyze which suits best in your situation.

Solution 5

If you choose typed datasets you kind of stick to one technology. It's harder to follow the Separation Of Concerns since the datasets and their table is usually getting passed over to both the business logic and in the client, not just the data access layser.

So the scalability is definitely an issue if you choose typed datasets in your application, because it's not just so simple swapping out that technologies because it's usually implemented across layers and tiers

Others swear to technologies like NHibernate instead, which is very good.

But if it's only a sample application you get very much for free using typed dataset. Go ahead and implement it unless this is supposed to be a longer-living application which intend to scale

Share:
18,843
Jame
Author by

Jame

Updated on June 18, 2022

Comments

  • Jame
    Jame almost 2 years

    I am working in C++ from last four years, and suddenly i am assigned a task to develop a sample web application whose purpose is to fetch data from a sample table and show it in a grid view.

    About Sample Application:

    The application is developed in Visual web Developer 2010under Framework 4.0, using ASP.net with C#. The backend database is developed in MS SQL Server 2008

    My Approach:

    Since, i am new to .net world. So i surf over internet and found the typed data set (the most easier way, i.e using wizards rather than coding) and hence i use type dataset for database connectivity.

    My Question:

    I have faced lot of criticism for using typed data sets :(. Some one say its not efficent, some one say its harder to extend . . . and so on :( Now i only want to ask that what is the best way of making database connectivity (especially, for large scale project). I am only asking this because i an new to this field. However i study from internet and found the another way through coding i.e There exist classes for each thing (DataSet, DataTable, DataRow, Connection etc.). Is it better way? or there exist some thing else. Some sample code or a web resource guiding how to do this would be more helpful