I came into the C# .NET Development World just as the .NET Framework did a lightning speed transition from 2.0 one day, 3.0 the next day, then 3.5 the very next day and let’s not forget the 3.5 SP1 over that weekend. Now we are in Framework 4.0. That’s a lot of catching up for someone coming in at the Framework Boom.
That being said I jumped straight into Data Access via Typed DataSets and LINQ to SQL. Why bother creating an Untyped DataSet, why create an Untyped Disconnected DataTable. Well, sometimes you have to backwards in order to go forward.
So I will never forget again below are snippets to create an Untyped Disconnected DataTable and an Untyped Connected DataSet.
Untyped Disconnected DataTable
private DataTable GetDataTable()
{
//Create a new instance of a DataTable
DataTable employee = new DataTable(“Employee”);
//Create the Columns that the DataTable will have
DataColumn eid = new DataColumn(“Eid”);
DataColumn firstName = new DataColumn(“FirstName”);
DataColumn lastName = new DataColumn(“LastName”);
DataColumn salary = new DataColumn(“Salary”, typeof(decimal));
DataColumn lastNameFirstName = new DataColumn(“LastName and FirstName”);
//Define the Contraints for the Each Column. There are default Constraints
// I recommend that you investigate those on your own.
eid.DataType = typeof(string);
eid.MaxLength = 10;
eid.Unique = true;
eid.AllowDBNull = false;
eid.Caption = “EID”;
employee.Columns.Add(eid);
firstName.MaxLength = 35;
firstName.AllowDBNull = false;
employee.Columns.Add(firstName);
lastName.AllowDBNull = false;
employee.Columns.Add(lastName);
salary.DefaultValue = 0.00m;
employee.Columns.Add(salary);
lastNameFirstName.DataType = typeof(string);
lastNameFirstName.MaxLength = 70;
lastNameFirstName.Expression = “lastName + ‘, ‘ + firstName”;
employee.Columns.Add(lastNameFirstName);
//Always make sure to create a Primary Key Column for your DataTable.
employee.PrimaryKey = new DataColumn[] { eid };
//Now you can add as many columns as you would like.
employee.Rows.Add(“001DK”, “Dave”, “KoZ”, 10.00m);
employee.Rows.Add(“002DB”, “David”, “Benoit”, 15.00m);
employee.Rows.Add(“003BJ”, “Boney”, “James”, 25.00m);
employee.Rows.Add(“004MA”, “Mindi”, “Abair”, 17.00m);
employee.Rows.Add(“005NB”, “Norman”, “Brown”, 85.00m);
//You can also update columns that you have created.
employee.LoadDataRow(new object[] { “002DB”, “Will”, “Downing”, 20.00m }, LoadOption.OverwriteChanges);
return employee;
}
protected void Page_Load(object sender, EventArgs e)
{
// A little LINQ to the DataTable never hurts
var query = GetDataTable().AsEnumerable()
.Select(em => em);
mygrid.DataSource = query.AsDataView();
mygrid.DataBind();
}
Untyped Connected DataSet
// Create a String variable to store the connection string
string conn = “Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\NORTHWND.MDF;Integrated Security=True;User Instance=True”;
string query = “SELECT * “ +
“FROM Orders “ +
“JOIN Customers ” +
“ON Orders.CustomerID = Customers.CustomerID “;
// Create and instance of a SqlConnection Class.
SqlConnection mySQLconnection = new SqlConnection(conn);
SqlCommand mySqlCommand = new SqlCommand(query, mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
//Create an instance of a DataSet and Fill the DataAdapter with it to make the call
//to the Database using the query in the SqlCommand object.
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
// A little LINQ to DataSet never hurts
var lq = from c in myDataSet.Tables[0].AsEnumerable()
where c.IsNull(“ShippedDate”) == false &&
c.Field<string>(“Country”) == “USA”
orderby c.Field<DateTime>(“ShippedDate”) descending
select c;
GridView1.DataSource = lq.AsDataView();
GridView1.DataBind();
I want to point out something very important here. You can see that the connection string is hard coding and seats in your code behind, not the most secure thing to do. The best thing to do here is to put you connection string in the Web.conf or app.config file. Here I will put it within the appsetting of the Web.config. So let’s replace the connection string variable with this:
string conn = ConfigurationManager.AppSettings["ConnectionString"].ToString();
Now in the Web.config or App.config, if there isn’t a tag already, let’s add the following:
<appSettings>
<add key=“ConnectionString” value=“Data Source=.\SQLEXPRESS;Initial Catalog=NORTHWND.MDF;Integrated Security=True”/>
</appSettings>