C# w/ Database and WPF Control interfaces

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Two issues - Using MFC, these would not be a problem, but I have to move on in the world :cool:

I have a basic WPF application with simple controls
Grid, listbox, combox, etc
I need to load the controls with text and read selections of the controls
Clues on to where to look and/or the methods?



I have a SQL data base schema defined using MS studio 10

I need to figure what C# class types are needed to
Open the database
Open a table
Read the Table in entirety
Read/Write a specific record.

I expect that there is one group that will handle the above - which one?

TIA
 

Train

Lifer
Jun 22, 2000
13,599
90
91
www.bing.com
I havent used WPF in a while, but am pretty much a Ninja with regards to accessing SQL from C#

Are you going to be using Entity Framework or Linq to SQL? If so that changes things. I would actually recommend Entity Framwork. But that depends on your app. If your doing something simple it may bea easier to skip setting up entities and object sets.

If you want to start with plain old data access with out any fancy middle layers, you want to include System.Data and System.Data.SqlClient

First you want an open connection:

Code:
SqlConnection dbConn = new SqlConnection(yourConnectionString);
dbConn.Open();

Edit: Actually you'll want to get in the habit of using the using statement:
Code:
using(SqlConnection dbConn = new SqlConnection(yourConnectionString))
{
     dbConn.Open();
     // data access code here
}

using is syntactic sugar that is basically short hand for this:
Code:
try
{
     SqlConnection dbConn = new SqlConnection(yourConnectionString);
     dbConn.Open();
}
catch
{
     // some error handling or whatever
}
finally
{
     dbConn.Dispose();
}

Because the database connection uses an unmanaged resource, you'll want to make sure it is closed/disposed/returned to the connection pool, even if something goes wrong.
 
Last edited:

Train

Lifer
Jun 22, 2000
13,599
90
91
www.bing.com
Once you have an open connection, you can start sending sql statements to the db server. These can be done in various forms, the most common is of course retrieving a set of records. Let's say I want the results of this:

Code:
string query = "SELECT UserName, Email FROM Users ORDER BY UserName"

I would instantiate a command object:

Code:
SqlCommand cmdGetRecords = new SqlCommand(query, dbConn);

And send the results to a data reader.

Code:
SqlDataReader reader = cmdGetRecords.ExecuteReader();

Now I can loop through the results using the reader.

Code:
while (reader.Read())
{
     Console.WriteLine(reader["UserName"].ToString());
}

The ToString may or may not be required depending on how you are using it.
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Train
Thanks. Syntax is slightly different that the MFC but the overall flow/concept is same.

Essentially, I have 4k of microscope slides that have to be tracked in different test configurations. Color, label, physical alignment within the instrument for laser detection. New instrument coming out of development and we need to determine if certain combinations can cause failure. Spreadsheet has become unworkable in tracking what combination has been tested.

Database allows me to select what parameters have been tested in combination to ensure complete coverage and select combinations for data analysis.

Under MFC and Access, a two day job. Here, at least a week, but it will be worth the learning time.