How do you create a recordsource using ADO.NET, like you would create a recordsource in VB6 using SQL statements?
If by recordsource you mean the ADO RecordSet, it doesn't exist in ADO.NET. It has been replaced by the far more powerful DataSet. This object represents the data in a source-independent manner, meaning that it will have exactly the same form whether you load it from an XML file, a database or programatically. In order to connect to a database, you use what are called Data Providers, which are implementations of a common interface for a specific database server product. For example, to connect to SQL Server, you use the System.Data.SqlClient namespace, which contains the data provider for it. For OleDb-compliant products, you can use the System.Data.OleDb namespace, likewise.
The good thing is that both implement a common set of classes and methods, allowing your code to be almost identical in both cases, but at the same time allowing each provider to be as efficient as it can at connecting to its specific backend.
The code to fill a DataSet from a SQL statement is straightforward:
SqlDataAdapter ad = new SqlDataAdapter("your SQL query", "your SQLServer connection"); DataSet data = new DataSet(); ad.Fill(ad);
There are also connection, command and transaction classes, for example, to deal directly with each entity.
Dig Deeper on Win Development Resources
Related Q&A from Daniel Cazzulino
Here Daniel Cazzulino explains how to load a DSL (domain specific language) domain model instance file programmatically. This requires the .NET type ... Continue Reading
Here we offer a glimpse at 12 of .NET development expert Danny Cazzulino's top ASP.NET questions and answers. Continue Reading
C# developers should NOT be modifying InitializeComponent method in the code-behind (or any of the variable definitions) by hand. Continue Reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.