私は自分のコードをシンプルに保ち、再利用できるようにトリミングするのが大好きです。さまざまな種類のオブジェクトにデータリーダーを使用しているのと苦労しているところで、メソッドでそれを見つけて見つけました接続が閉じているか開いている状態での問題でした。だから私は強制的に、コードをコピーして貼り付けることになります。これは私が嫌いなものです! 私はこれをスケールダウンしてメソッドに入れ、再利用可能でいいものにする方法はありますか?C#:コードを短縮してストアドプロシージャを実行するにはどうすればよいですか?
ENT_AuctionBid ret = new ENT_AuctionBid();
try
{
SqlParameter[] Params = new SqlParameter[]{
new SqlParameter("@ID", ID)
};
using (SqlConnection conn = new SqlConnection(this.ConnectionString))
{
using (SqlCommand command = new SqlCommand("GetItem", conn))
{
SqlDataReader reader;
command.CommandType = CommandType.StoredProcedure;
conn.Open();
command.Parameters.AddRange(Params);
reader = command.ExecuteReader(CommandBehavior.SingleRow);
while (reader.HasRows)
{
while (reader.Read())
{
//
ret = this.Convert(reader);
}
reader.NextResult();
}
reader.Close();
}
}
}
catch (Exception ex)
{
}
return ret;
SqlDataAdapterにはかなりのオーバーヘッドがあります。これは実際には必要ありません。だから私はそれに反対します。 EFがオプションである可能性があります –
私はエンティティフレームワークに切り替えました.Fantastic! – Funky