できる限り簡潔にしようと思います。とてもシンプルなはずなので、我慢してください。
目標:
プロジェクトの特定のセクションを普遍化しようとし
SQL databse transactions
ています。
サイドノート
あなたの答えを助けるために、私は以下を貼り付けました(参考のために)
使用法コード:をGetTestOfTablTime()
返しますDataTable
クラス:SQLDBInteraction
これは別のクラスです-final(SQLトランザクション)ステージを担当します
以下のこのコードでは、私が呼ぶものを構築しています:「ストアドプロシージャのメタデータ」
そのクラスは、すべてのSQLDbSPを保持するクラスです。
HTSPs
(HTは会社のエイリアスです)
このクラスは、それぞれ のSP
(必要な)を保持しています parameter
HTSPs
クラスには別のサブクラスが含まれ、すべての名前に対して、各名前に対してSP
のみが含まれます。const string
SP
public sealed class HTSPs
{
//so for example this is one of the members of this class - a stored procedure
//its mission: get evnents with specified id OF specified userId in spec' month, year..
public sealed class GetTimesWithCustomerNames
{
//if I DO need Constructor for its parameters how do I properly format the constructor?
public GetTimesWithCustomerNames()
{
Userid.ParameterName = ParNameUserid;
Month.ParameterName = ParNameMonth;
Year.ParameterName = ParNameYear;
EventId.ParameterName = ParNameReasonid;
}
const string ParNameUserId = "@userId",
ParNameMonth = "@month",
ParNameYear = "@year",
ParNameEventId = "@eventId";
public static SqlParameter Userid = new SqlParameter();
public static SqlParameter Month = new SqlParameter();
public static SqlParameter Year = new SqlParameter();
public static SqlParameter EventId = new SqlParameter();
}
}
したがって、問題は次 のとおりです。請負業者を初期化するにはどうすればよいですか?
StoredProcedure
シンプルなカスタマイズされた「メタデータ」を作成する適切な方法は何ですか
Iv'eは現在、以下のメソッドの実装を完了しています(その問題は別として...)
利用方法
これは、 /DataTable
を使用しているときに戻るメソッドです。HTSPs
class
constuctor
using SPTime = HT_DBSchema.HTSPs.GetTimesWithCustomerNames;
private DataTable GetTestOfTablTime()
{
SQLDBInteraction.DataContainer DC_Time = new SQLDBInteraction.DataContainer();
SQLDBInteraction.SqlParamList parmsTime = new SQLDBInteraction.SqlParamList();
Dictionary<SqlParameter, int> SqlCmdParDict = new Dictionary<SqlParameter, int>();
parmsTime.SqlCmd.CommandType = CommandType.StoredProcedure;
parmsTime.SqlCmd.CommandText = AppDb.MetaSqlSProc.Time.Name;
parmsTime.SP_Name = AppDb.MetaSqlSProc.Time.Name;
parmsTime.TableName = AppDb.MetaSqlTable.Time.Name;
//While folowing implementation Does Work I comented it out to try using the SP Struct
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameMonth, 9));
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameReasonid, 1));
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameYear, 2012));
//ParmsTTime.SP_Params.Add(new SqlParameter(SPTime.ParNameUserid, 3571));
//here's where I'm currently stuck, in section below. trying to assign values for the SqlCommand
parmsTime.SqlCmd.Parameters.AddWithValue(SPTime.ParNameMonth, 9);
parmsTime.SqlCmd.Parameters.AddWithValue(SPTime.ParNameYear, 2012);
parmsTime.SqlCmd.Parameters.AddWithValue(SPTime.ParNameReasonid, 1);
SPTime.Userid.Direction = ParameterDirection.Input;
SPTime.Userid.SqlValue = 3571;
return DC_Time.LocalTbl_V3(ParmsTime);
}
アップデート
上記のコードの最後の行は、パラメーターの割り当てを実装しようとしています。
したがって、以下を使用する必要はなくなります。
SQLDBInteraction.SqlParamList.SP_Params
(これはList<SqlParameter>
)
代わりに私は本当に使用できるようにしたいと思います
SQLDBInteraction.SqlParamList.SqlCmd.Parameters
これは、データベースと対話するために必要なほとんどの手順ですでに使用されているため、
だからこれは私が余分な変数のいくつかの不必要な使用法を落とす方法です
同時に、私はSqlCmd(parmsTime.SqlCmd.Parameters.Add(......)
)を割り当てたいと思いました
構造体を使用-RealSPTime
SqlParameters
...現在の名前を表す文字列を使用する代わりに
例 parameter.name
-(SPTime.ParNameMonth, someValue
)
最終段階-SQLトランザクション
トランザクションを実行するSQLDBInteraction
クラス
public class SQLDBInteraction
{
public class SqlParamList
{
public SqlCommand SqlCmd = new SqlCommand();
public List<SqlParameter> SP_Params = new List<SqlParameter>();
public String SP_Name;
public string TableName;
public string SelectCommand;
///public SqlCommandType SelectedCmdType;
}
public class DataContainer
{
public DataTable LocalTbl_V3(SqlParamList Params)
{
SqlConnection sqlConnection;
DataTable Usrs = new DataTable(Params.TableName);
SqlDataAdapter sqlDataAdapter;
using (sqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["HTConn"].ConnectionString))
{
sqlConnection.Open();
using (Params.SqlCmd.Connection = sqlConnection)
{
using (sqlDataAdapter = new SqlDataAdapter(Params.SqlCmd.CommandText, sqlConnection))
{
if (sqlDataAdapter.SelectCommand.Parameters.Count > 0 == false)
{
sqlDataAdapter.SelectCommand = Params.SqlCmd;
sqlDataAdapter.Fill(Usrs);
}
}
}
}
return Usrs;
}
ストアドプロシージャのパラメータの割り当てられた部分で私が間違っていることを誰かが見つけたら、本当に感謝します。SQL Command