Jyncus
Jyncus

Reputation: 13

Accessing Database via DataSet in C#

I added a new service-based database to my project. Under the Data Source Configuration Wizard for my DataSet, I selected the "Parts" table from the Database. Why am I not able to access any data from the QuotingDataSet class?

    private void buttonTest_Click(object sender, EventArgs e)
    {
        QuotingDataSet dataSet = new QuotingDataSet();

        var partQuery =
            from part in dataSet.Parts.AsEnumerable()
            where part.Part_Number == txtBoxTestInput.Text
            select part.Part_Description;

        foreach (var part in partQuery)
        {
            txtBoxTestOutput.Text = part;
        }
    }

If it needs to be populated manually, is there a LINQ to SQL command to simply put the entire table into the DataSet? I'm not comfortable on SQL, so I'm trying to avoid writing all the SQL into an SqlDataAdapter as shown here.

I guess I'm not clear on why there is a need for this second level of abstraction; what would be the problem with running LINQ queries directly on the database itself?

Upvotes: 0

Views: 84

Answers (1)

user1961169
user1961169

Reputation: 799

Take a look at the Entity Framework. Right click on your Project, and Manage NuGet packages, search for the "Entity Framework" and add it to your project.

There's plenty of info on the net about Entity Framework, and version 6 is the latest version. Using a database-first methodology, Entity framework will create C# structures based on your tables and columns, and will allow you to run LINQ queries straight on the database.

There's a good example at CodeProject for the database-first methodology, which is how I prefer to use it, as it allows easy setup with existing databases...

http://www.codeproject.com/Tips/739164/Entity-Framework-Tutorial-for-Beginners

The microsoft information covers a lot more options, although I found it to be more confusing and less helpful because of this...

https://msdn.microsoft.com/en-us/data/ee712907.aspx

Upvotes: 1

Related Questions