DataSet and DataTable - c#

What is dataset and datatable?
How both of them are related to each other.
And what are the uses of it.
I also want to know how to create a dataset manually and display the data in the web page using c sharp.
As i am newbie to .net and c sharp

A data set is a collection of data tables and the relationships between them. It normally represents a set of database tables and the relationships (foreign keys) between them. See DataSet on MSDN.
A data table is usually a representation of a database table, in memory. See DataTable on MSDN.
The uses are that you do not need to go to the database every time you want to lookup a value.
The rest of your question is rather broad and not really answerable, as there are many ways to create a dataset in memory. You need to be much more specific regarding what you are trying to do.

A DataSet is an in memory representation of data,It containing one or more DataTables.
A DataTable is an in-memory representation of data, typically retrieved from a database or XML source.

As far as I remember, a DataSet is a collection of DataTables and the Relations between tables.
A DataTable is an object that represents a table of data similar to a database table.
If I were you I'd avoid the use of DataSets and DataTables as they are fairly heavyweight objects and for simple display you don't need any of the extra features that they provide.
Google for information about DataReaders and maybe DataReaders vs DataTables

Related

Are .NET DataTables useful beyond as a means of interfacing with databases?

I often use the DataTable class in my .NET WCF services, since many of our SPs require TVPs. As far as I know, DataTables are the only way of passing TVPs to SPs.
It just occurred to me that similarly to how tables, in which information is stored according to rows and columns are useful, that the DataTable class may be useful beyond just as a means of interfacing with SQL Server TVPs.
Actually... thinking about this, I have previously written code that iterated over a DataTable's rows, building up an HTML string. However the main reason we used a DataTable as because the same table could be passed to SQL Server as a TVP.
Looking at the docs: https://msdn.microsoft.com/en-us/library/system.data.datatable%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396, it looks like you can effectively create relational object models using DataTables.
Would using DataTables be an effective way of caching data retrieved from a SQL Server in a service?
Another potential use-case that comes to mind... Would there be any benefit of using a DataTable for a collection instead of List<MyType>?
Datatables are slower than Lists/Enumerables, and its better to use dataAdapter while reading data if you really care about performance.
But Datatables can be really useful as a item source for grids, where you want to just publish whole table data on the UI and no need to specify each column individually as in the case of List.

Data relation in ADO.net

Sorry if my question is basic, when we use ADO.net to write own data access code, and we work with more than of one data table, data relation between data tables comes automatically from data base tables or we have to add data relation between data tables separately.
To understand Data Relation in Ado.Net you can read This.
when you work with more than of one data table then you can either write code yourself(using DataRelation),The DataSet contains DataTable objects and DataRelation objects. The DataRelation objects represent the relationship between two tables or you can use ERD(entity Relationship Diagram) to drag and show relationship among tables.
In my opinion database diagram is better as it reduces or exempt you do do extra code work, but manual code adds flexibility and customization.

Save a DataSet to a database

When I load from the database I use one store procedure which loads the DataItem and any Data associated with it. This comes back in one DataSet with two tables, the first table has one row and describes the DataItem and each row in the other table describing the related Data.
This DataSet is then used to populate my objects.
My problem comes when I have to save the objects back to the database. I am currently saving the DataItem and then looping through all of my Data and performing a save on each one. Completely horrible way to go about doing it, I know. It's both slow and it's not transactional.
So what I'd ideally like to do is convert my objects back into my DataSet and then save it all back to the database in one efficient transactional operation. What code do I need on the C# side to make this transactional and to allow me to pass back a DataSet. I presume this will involve using a TableAdapter. But given that I have two tables how will this work? What do I use on the SQL side - Can I use store procedures? (I would like to avoid having SQL in my C# project) Would I need to write something that will handle cycling through a datatable to save each record?
What's the best way to go about doing all this? This will form the lynchpin of a project I'm working on so I want it to be as fast and efficient as it can be!
(.NET 4.0 and SQL 2005)
Did not use TableAdapter in the end as it was more effort than it was worth.
From the comments:
http://msdn.microsoft.com/en-us/library/4esb49b4.aspx

Integrating Database Functionality

I am looking for a few suggestions on how I can integrate database functionality into my WPF application for fast and efficient query processing. Are there libraries I can integrate, or does C# have this built in.
What I intend to have is several tables with less than one thousand entries each. This data is read from an XML file when the application launches and is inserted into tables. All data should reside in memory so no filesystem support is necessary.
Look at ADO.net's DataSet: http://msdn.microsoft.com/en-us/library/ss7fbaez.aspx (emphasis mine):
The ADO.NET DataSet is a memory-resident representation of data that
provides a consistent relational programming model regardless of the
source of the data it contains. A DataSet represents a complete set of
data including the tables that contain, order, and constrain the data,
as well as the relationships between the tables.
There are several ways of working with a DataSet, which can be applied
independently or in combination. You can:
Programmatically create a DataTable, DataRelation, and Constraint within a DataSet and populate the tables with data.
Populate the DataSet with tables of data from an existing relational data source using a DataAdapter.
Load and persist the DataSet contents using XML. For more information, see Using XML in a DataSet (ADO.NET).
See http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx for specifics on "Loading a DataSet from XML".
This is available by default in ADO.Net - one of the base .Net libraries, so it is immediately usable from C# without any additional dependencies. (Technically, C# is just a language - it doesn't provide any libraries.)
You can use ADO.NET Datasets from the xml files. Take a look here for a example
DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("table1");
dataTable.Columns.Add("col1", typeof(string));
dataSet.Tables.Add(dataTable);
string xmlData = "<XmlDS><table1><col1>Value1</col1></table1><table1> <col1>Value2</col1></table1></XmlDS>";
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlData);
dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema);

C# (Visual studio): Correlation between database, dataset, binding source

I am just learning C# through Visual Studio 2008?
I was wondering what exactly is the correlation between dabases, datasets and binding sources?
As well, what is the function of the table adapter?
At a super high level:
Database -- stores raw data
DataSet -- a .NET object that can be used to read, insert, update and delete data in a database
BindingSource -- a .NET object that can be used for Data Binding for a control. The BindingSource could point to a DataSet, in which case the control would display and edit that data
TableAdapter -- Maps data from a database table into a DataSet
There is a lot more to all of these, and understanding the way ADO.NET is architected can take a bit of time. Good luck!
A DataSet is usually used to hold a result from the database in memory, i.e. it contains a DataTable object. The DataSet and DataTable objects themselfs are independent of the database, so the result doesn't have to come from a database. The DataSet can contain several DataTables, and you can even define relations between them. It's like a mini database in memory.
A binding source is any object that can provide a list of objects with properties. A DataSet or a DataTable can do that, but it could basically be any kind of list containing objects that has properties.
A TableAdapter is used to read data from a DataReader provided by a Command object, and put the data in a DataTable object.
The dataset is a (partial) in-memory representation of a database. Tables or Views in the datatbase are represented as datatables in a dataset. The dataadapter is the link between the database and the dataset. Once the adapter has loaded the data into the dataset, the physical connection to the dataset is disposed. This is why it's called a disconnected data-model.

Categories