我正在使用 MySql 數(shù)據(jù)庫,如果我執(zhí)行解決方案,我會在 catch 塊中收到異常,下面是我的代碼MySqlManager dac = new MySqlManager();long value = 0;DbCommand dbCommand = dac.GetStoredProcCommand("Spforgotpassword");dac.AddOutParameter(dbCommand, "p_op_EmailId", DbType.Int32, -1);dac.AddInParameter(dbCommand, "p_p_EmailId", DbType.String, data.Email_Id);dac.AddInParameter(dbCommand, "p_p_Password", DbType.String, Generatehash256(data.Password));dac.AddInParameter(dbCommand, "p_P_EncryptedPassword", DbType.String, Encryption.Encrypt(data.Password));dac.AddInParameter(dbCommand, "p_P_Mode", DbType.String, 102);dac.AddInParameter(dbCommand, "p_IPAddress", DbType.String, data.IPAddress);IDataReader reader = dac.ExecuteReader(dbCommand);if (reader.Read()){ value = reader["result"] == DBNull.Value ? default(long) : Convert.ToInt32(reader["result"]);}dac.CloseConnection(dbCommand, reader);if (Convert.ToBoolean(ConfigurationSettings.AppSettings["EnableEmail"])){ if (Convert.ToInt32(dac.GetParameterValue(dbCommand, "p_op_EmailId")) != -1) { Mail.WebMail oWebmail = new Mail.WebMail(dac.GetParameterValue(dbCommand, "p_op_EmailId").ToString()); }}return value;一旦我執(zhí)行這一行,我就會收到異常 --- Object cannot be cast from DBNull to other typesdac.GetParameterValue(dbCommand, "p_op_EmailId")該p_op_EmailId是輸出參數(shù)。
1 回答

白衣非少年
TA貢獻1155條經(jīng)驗 獲得超0個贊
看起來存儲過程將值設(shè)置為p_op_EmailId
to null
。所以你不能Int32
直接將它轉(zhuǎn)換為。
相反,您可以將其轉(zhuǎn)換為一個可為空的 int,并-1
在比較之前將其合并:
(dac.GetParameterValue(dbCommand, "p_op_EmailId") as int? ?? -1) != -1
- 1 回答
- 0 關(guān)注
- 475 瀏覽
添加回答
舉報
0/150
提交
取消