Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Could you please give me any reason for using SqlCommand (to execute SQL statements directly) better than using DataTable and DataAdapter? Of course what I want to do with my database are only INSERT, DELETE, UPDATE (except SELECT and others features can only be achieved using SQL statements directly).
I want to say more about how easy inserting, deleting and updating with DataTable and DataAdapter are. First, to insert a new row into some table, simply create a new DataRow and then Add this DataRow to DataTable.Rows, then use DataAdapter to update the database. Second, to delete a row, simply remove it from DataTable.Rows or commonly we have a DataGridView bound to the DataTable and deleting a row in DataGridView will reflect the effect to the DataTable. The last, to update the database, assume that users edit the data through the DataGridView, all these changes will reflect to the DataTable and simply we use DataAdapter to update the database from that modified DataTable.
Contrarily, using SqlCommand to insert, delete, update the database is much more difficult and complexed.
I'm hesitating between those two ways.
Thank you very much!
Posted

0) Because you want to.

1) Because you need to.

Using adapters is a convenience/preference thing, and is used when you would prefer to use wizard-based coding techniques. If you prefer to get closer to the metal, you use SqlConnection and SqlCommand.

I prefer metal to plastic. It's just the way I roll.
 
Share this answer
 
Comments
[no name] 18-Oct-11 11:19am    
I like the simplicity! At least when building an application, of course when studying, researching, playing with codes... I will turn to like the complexity!
Thank you! Please wait for other ideas!
If all you're doing is mapping business layer data tables to database tables, then there is no reason; the binding and adapter capabilities of System.Data are adequate. If you want to do database operations that don't directly represent table changes, then you might want to insert/delete/etc directly (particularly true if you want to map a List<T> to a database and you don't want so much that you want a persistence management framework like NHibernate to handle that all for you).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900