Internetwork Consulting LLC
3880 Greenhouse Road #421
Houston, TX 77084

832-606-3300

sales@internetworkconsulting.net
Custom Software Development - Computer Repair
 

Extending Data Tables and Rows

I’ve searched around and it seems that everyone recommends not extending or inheriting from DataTables. While inheriting from a DataTable is not possible, VisualStudio does generate custom DataTable and DataRow classes. This led me to believe that it was not only possible, but also safe.

After examining the generated code, and writing a class from scratch, here is the outline of what’s required.

This example will be for two classes: UserDataTable, and UserDataRow. While these classes use MySQL, the same applies to Microsoft SQL.

DataTable

The data table must inherit from System.Data.TypedTableBase<T> where T is the DataRow type you want to use. Secondly, overriding “GetRowType()” is required. Finally, you’ll have to add a reference to “System.Data.DataSetExtensions”. Here’s the UsersDataTableClass:

using System;

using MySql.Data.MySqlClient;

namespace incBusiness.Data {

public partial class UserDataTable : System.Data.TypedTableBase<UserDataRow> {

public static string Id = "`ID`";

public static string LoginName = "`Login Name`";

protected override global::System.Type GetRowType() {

return typeof(UserDataRow);

}

public UserDataRow this[int index] {

get {

return ((UserDataRow) (this.Rows[index]));

}

}

protected override global::System.Data.DataRow NewRowFromBuilder(global::System.Data.DataRowBuilder builder) {

return new UserDataRow(builder);

}

public static UserDataTable LoadTable(MySqlConnection conn, string where = "", string order = "") {

UserDataTable ret = new UserDataTable();

string sql = "SELECT * FROM `User`";

if(where.Length > 0)

sql += " WHERE " + where;

if(order.Length > 0)

sql += " ORDER BY " + order;

MySqlDataAdapter adapter = ret.GetAdapter(conn);

adapter.SelectCommand = new MySqlCommand(sql, conn);

adapter.Fill(ret);

return ret;

}

public static UserDataRow LoadRow(MySqlConnection conn, string where = "", string order = "") {

UserDataTable ret = new UserDataTable();

string sql = "SELECT * FROM `User`";

if(where.Length > 0)

sql += " WHERE " + where;

if(order.Length > 0)

sql += " ORDER BY " + order;

MySqlDataAdapter adapter = ret.GetAdapter(conn);

adapter.SelectCommand = new MySqlCommand(sql, conn);

adapter.Fill(ret);

if(ret.Rows.Count != 1)

throw new Exception("Could not locate a unique row using WHERE clause '" + where + "'!");

return (UserDataRow) ret.Rows[0];

}

public MySqlDataAdapter GetAdapter(MySqlConnection conn) {

MySqlCommand cmd = conn.CreateCommand();

cmd.CommandText = "SELECT * FROM `User` WHERE 1 <> 1";

MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

MySqlCommandBuilder builder = new MySqlCommandBuilder(adapter);

return adapter;

}

}

}

UserRow

Extending a DataRow is as simple as inheriting from the DataRow class. Below is the UserDataRow.

using System;

namespace incBusiness.Data {

public partial class UserDataRow : System.Data.DataRow {

public UserDataRow(System.Data.DataRowBuilder drb) : base(drb) { }

public System.String Id {

get {

if(this.IsNull("ID"))

return default(System.String);

else

return (System.String) this["ID"];

}

set { this["ID"] = value; }

}

public System.String LoginName {

get {

if(this.IsNull("Login Name"))

return default(System.String);

else

return (System.String) this["Login Name"];

}

set { this["Login Name"] = value; }

}

}

}