I’ve seen so many ways of accessing a database using C# I don’t know which one is “best” for simple reading/writing/manipulating data. I used to just use generic DataSet/DataTable objects but I’m trying to use more type safe structures. I’ve started by adding a dataset (xsd) in visual studio and connecting to a SQL server backend.
I’m trying to query search a column in a table for a given string, but the column isn’t the primary key (so i can’t use .Find()). How do i do this? Do i need to use LINQ or can i use extension methods/lambda expressions?
On a more basic level, when using the design-time dataset do I need to use Table adapters to fill each table in the Dataset I use or do i just instantiate the dataset? The documentation is a bit confusing to me.
You can use
Linq-To-DataSetwhich is the most powerful way (not in terms of efficiency but in terms of readability and maintainability) to query theDataSet.Read more: Querying Typed DataSets.
You have the option to create a
TableAdapteror just aDataTableon the VS designer. If you add a TableAdapter VS creates also the according DataTable. If you only add a DataTable, you have to provide your own way to fill it.TableAdaptersare similar to aDataAdapterin ADO.NET.So if you have created a DataSet named
DataSet1and added a TableAdapter which selects a table namedtabData, Visual Studio will automatically create a DataTable namedtabDataDataTableand aTableAdapternamedtabDataTableAdapterin the namespaceDataSet1TableAdapters.So you could fill this table in this way:
Assuming that the table has columns
NameandAgeand you want to find all rows where the name starts with"Jon"and age is > 30, you can use LINQ’sWhere:Note that you can use LINQ-To-DataSet type safe(
r.Nameis a string andr.Agean int).