當我在我的數據庫上執行此命令時,有時會收到result = null。我想在這種情況下輸入「其他」部分,但我收到從數據庫中接收到NULL時出錯
mscorlib.dll中發生未處理的異常類型'System.FormatException'。 附加信息:輸入字符串格式不正確。
DB database = new DB();
int reservedSeats = 0;
using (database.sqlConnection)
{
database.sqlConnection.Open();
string command = "select SUM(nrLocuri) as result from Rezervari where idRand = @idRand and [email protected]";
using (SqlCommand cmd = new SqlCommand(command, database.sqlConnection))
{
cmd.Parameters.Add("@idRand", SqlDbType.Int).Value = idRand;
cmd.Parameters.Add("@idShow", SqlDbType.Int).Value = idShow;
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
if (dr["result"].ToString() != null)
reservedSeats = Convert.ToInt32(dr["result"].ToString());
else
return totalSeats;
}
}
}
return totalSeats-reservedSeats;
您需要在異常處理讀了。 –
列結果的數據類型是什麼? –