Code Snippets

Filling DataSets with Multiple Tables

Filling a DataSet with multiple tables can be done by sending multiple requests to the database, or in a faster way: Multiple SELECT statements can be sent to the database server in a single request. The problem here is that the tables generated from the queries have automatic names Table and Table1. However, the generated table names can be mapped to names that should be used in the DataSet

SqlDataAdapter adapter = new SqlDataAdapter(
     
"SELECT * FROM Customers; SELECT * FROM Orders", connection);
adapter.TableMappings.Add("Table", "Customer");
adapter.TableMappings.Add("Table1", "Order");

adapter.Fill(ds);

Christian


Code Snippets

I've added a new category to my weblog: Code Snippets.

Here I will show some code segments that generate special interest in my courses.

For example, one "homework" that I've given to my online students is that they should create a class to make this code happen:

Person p1 = new Person("Arnold", "Schwarzenegger");
p1.City = "Los Angeles";

Person p2 = new Person();
p2.FirstName = "Niki";
p2.LastName = "Lauda";

The interesting part follows here, custom formatting of the person objects:

Console.WriteLine("{0:Lastname}", p2);
Console.WriteLine("{0:Full}", p1);

The output should be:

Lauda
Arnold Schwarzenegger, Los Angeles

I will post the answer here after PDC, when the "homework" should be completed :-)

Christian