2つの日付間のエントリの列の合計を返しますメソッドを作成しようとしています。しかし、何があってもDBnull
が返ってきます。私は、クエリがより簡単な(SUM、単純select * where statement
)ときに動作する他の同様のメソッドがあります。 performQuery()
は、クエリー結果で塗りつぶしたdatatable
を返すヘルパーメソッドです。データテーブルの列の合計を取得できません
public static int getBookedQuantity(int shopID, int bikeID, DateTime datetime)
{
string query = "SELECT sum(quantity) as \"quantity\" FROM booking WHERE [email protected] AND shopID = @shopID AND starttime >= @Datetime AND endtime < @Datetime";
SqlParameter param1 = new SqlParameter("@bikeID", bikeID);
SqlParameter param2 = new SqlParameter("@shopID", shopID);
SqlParameter param3 = new SqlParameter("@Datetime", SqlDbType.DateTime);
param3.Value = datetime;
DataTable bookingData = performQuery(query, param1, param2, param3);
DataRow[] row = bookingData.Select();
int totalBooked = 0;
if ((row.Count()) > 0 && (bookingData != null)) // if there are rows returned
totalBooked = Convert.ToInt32(row[0]["quantity"]);
return totalBooked;
}
ありがとうございます!
FYI: '1 + 2 + NULL'は' NULL'を返します –