MySQL Connector / Netを使用していて、実行時に名前が指定されるテーブルに対してクエリを作成したいと思います。
この例は私の頭のてっぺんから外れています(テストされていません):
public class DataAccess
{
public enum LookupTable
{
Table1,
Table2,
Table3
}
public int GetLookupTableRowCount(LookupTable table)
{
string tableName = string.Empty;
switch (table)
{
case LookupTable.Table1:
tableName = "table_1";
break;
case LookupTable.Table2:
tableName = "table_2";
break;
case LookupTable.Table3:
tableName = "table_3";
break;
default:
throw new ApplicationException("Invalid lookup table specified.");
}
string commandText = string.Concat("SELECT COUNT(*) FROM ", tableName);
// Query gets executed and function returns a value here...
}
}
クエリでテーブル名をパラメータ化できるとは思わないので、 SQLインジェクションの可能性を制限するために、関数パラメータで文字列ではなく列挙型を使用しました。
それは良いアプローチのように思えますか?もっと良い方法はありますか?