13

I'm creating something like a small cashier application that keeps record for the clients, employees, services, sales, and appointments. I'm using windows forms, and within that DataGrids. I've created the database that I'm going to be using for the application. I want to know if I should use SqlCommand-SqlDataReader or SqlDataAdapter-DataSet instead. Which approach is better?

user990692
  • 543
  • 1
  • 8
  • 16
  • possible duplicate of [SqlDataAdapter vs SqlDataReader](http://stackoverflow.com/questions/1676753/sqldataadapter-vs-sqldatareader) – John Saunders Dec 27 '11 at 04:10
  • Possible duplicate of [SqlDataAdapter vs SqlDataReader](http://stackoverflow.com/questions/1676753/sqldataadapter-vs-sqldatareader) – George Lanetz Feb 18 '17 at 19:49

6 Answers6

16

This is highly depend upon type of operation you want.

Following is my suggetion.

  1. If you want to read data faster go for SQLDataReader but that comes as cost of operation you need to take during read after that also. Open Connection Read Data Close Connection. If you forgot to close than it will hit performance.

  2. Go for SQLDataAdapter

    • If you want to read faster and use benefit of Disconnected Arch. of ADO.net
    • This will automatically close/open connection.
    • Also it will also allow you to automatically handle update in DataSet back to DataBase. ( SqlCommandBuilder)
  3. Use SQLCommand ( This will also comes when you read SQLDataReader for read data) and for insert and update.

    • This will give you better performance for insert and update.

If you are using .NET Frame 3.5 sp1 or later i would suggest Linq to SQL or Entity Framework would also solve your purpose.

Thanks.

dotnetstep
  • 17,065
  • 5
  • 54
  • 72
4

SqlDataAdapter

  • stores data on your client and updates database as necessary. So it consumes more memory.
  • On the other hand you wouldn't need to be connected to your database on insert/delete/update/select command.
  • It manages connections internally so you wouldn't have to worry about that.

All good stuff from SqlDataAdapter come at a cost of more memory consumption. It's usually used for systems that need multiple users connected to database.
So I'd say if that's not your situation go for SqlCommand and the connected model.

atoMerz
  • 7,534
  • 16
  • 61
  • 101
3

If you are just reading data and not doing updates/inserts/deletes, then SqlDataReader will be faster. You can also combine it with a DataSet. If you wrap the data access objects with using statements, the runtime will handle the connection cleanup logic for you.

A pattern I often use for synchronous access is something like this:

DataTable result = new DataTable();
using (SqlConnection conn = new SqlConnection(MyConnectionString))
{
    using (SqlCommand cmd = new SqlCommand(MyQueryText, conn))
    {
        // set CommandType, parameters and SqlDependency here if needed
        conn.Open();
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            result.Load(reader);
        }
    }
}

For updates/deletes/inserts, a SqlDataAdapter might be worth considering, but usually only if you already have your data in a DataSet. Otherwise, there are faster/better ways of doing things.

RickNZ
  • 18,448
  • 3
  • 51
  • 66
2

If you are aware of these components (Core ADO.NET) (Command,Connection, DataAdapter) then I'd suggest Entity Data Model or Linq-SQL.

SqlDataAdapter is helper class which implicitly uses SqlCommand, SqlConnection and SqlDataReader.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

DataReader – The datareader is a forward-only, readonly stream of data from the database. This makes the datareader a very efficient means for retrieving data, as only one record is brought into memory at a time. The disadvantage: A connection object can only contain one datareader at a time, so we must explicitly close the datareader when we are done with it. This will free the connection for other uses. The data adapter objects will manage opening and closing a connection for the command to execute

DataAdapter – Represents a set of SQL commands and a database connection that are used to fill the DataSet and update the data source. It serves as a bridge between a DataSet and a data source for retrieving and saving data. The DataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source, and Update, which changes the data in the data source to match the data in the DataSet. By using it, DataAdapter also automatically opens and closes the connection as and when required.

1

SQL Command is Easier but not Automated. SQL Data Adapter is Less easy but Automated.

*Automated means it manages the opening and closing of a server, etc. automatically.

Both of them shares the same functionalities on Data

Teco
  • 11
  • 3