このコードの一部を単純なDALに抽象化するためのより良い方法は何でしょうか。現時点では、コードにパッチを適用しているだけで、EF、Linq2Sql、またはORMを使用する時間や必要はありません。
public string GetMySpecId(string dataId)
{
using (SqlConnection conn = new SqlConnection(this.MyConnectionString))
{
conn.Open();
// Declare the parameter in the query string
using (SqlCommand command = new SqlCommand(@"select ""specId"" from ""MyTable"" where ""dataId"" = :dataId", conn))
{
// Now add the parameter to the parameter collection of the command specifying its type.
command.Parameters.Add(new SqlParameter("dataId", SqlDbType.Text));
command.Prepare();
// Now, add a value to it and later execute the command as usual.
command.Parameters[0].Value = dataId;
using (SqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
specId = dr[0].ToString();
}
}
}
}
return specId;
}
GetMySpecId()から接続やコマンドなどを引き出すためのクリーンな方法は何ですか。これらの関数がたくさんあり、using....を何度も書きたくないからです。