输入的字符串在C#中的格式不正确?获取异常作为System.formatexception吗?

时间:2018-08-14 06:51:23

标签: c# mysql

在C#中执行mysql存储过程时,我遇到了此异常Input string was not in a correct format

下面是我的示例代码:

        MySqlManager dac = new MySqlManager();
        DbCommand dbCommand = dac.GetStoredProcCommand("DocdetailsById");
        dac.AddInParameter(dbCommand, "p_P_ID", DbType.Int32, id);
        dac.AddInParameter(dbCommand, "p_P_Mode", DbType.Int32, mode);
        dac.AddInParameter(dbCommand, "p_P_IPADDRESS", DbType.String, ipaddress);
        dac.AddInParameter(dbCommand, "p_P_OsBrowser", DbType.String, SessionManager.OSandBrowser);
        dac.AddInParameter(dbCommand, "p_P_Subscriberid", DbType.Int32, SessionManager.SubscriberId);
        IDataReader reader = dac.ExecuteReader(dbCommand);
        List<FilesDTO> result = new List<FilesDTO>();
        while (reader.Read())
        {
            result.Add(new FilesDTO
            {
                EmailID = reader["EmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["EmailID"]),
                DocumentID = reader["DocumentID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["DocumentID"]),
                Name = reader["Name"] == DBNull.Value ? default(string) : Convert.ToString(reader["Name"]),
                DocumentName = reader["DocumentName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DocumentName"]),
                StatusID = reader["StatusID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["StatusID"]),
                SubscriberID = reader["SubscriberID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["SubscriberID"]),
                CreatedBy = reader["Initiater"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["Initiater"]),
                DelegatorName = reader["DelegatorName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorName"]),
                DelegatorEmailID = reader["DelegatorEmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorEmailID"]),
                SignatureType = reader["SignatureType"] == DBNull.Value ? default(Int16) : Convert.ToInt16(reader["SignatureType"])
            });
        }
        dac.CloseConnection(dbCommand, reader);
        return result;

完成 ExecuteReader 执行后,我将在DocumentID = reader["DocumentID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["DocumentID"]),

中将reader.Read()作为System.formatexception获取异常。

请有人可以帮助我吗?谢谢。

2 个答案:

答案 0 :(得分:0)

先将Convert.ToInt32转换为字符串,然后再尝试。

DocumentID = reader["DocumentID"] == DBNull.Value
                                    ? default(int) 
                                    : Convert.ToInt32(reader["DocumentID"].ToString()),

答案 1 :(得分:0)

在执行FormatExceptionConvert.ToInt32的过程中,您将得到Convert.ToInt16。当输入值为null或不可转换为整数时,会发生这种情况。我希望您使用Int32.TryParse()Int16.TryParse()代替Convert.To..

例如:

int documentID = 0;
Int32.TryParse(reader["DocumentID"], out documentID)

对于您的方案,您需要针对不同的字段重复执行此操作,因此最好定义如下所示的方法,该方法接受一个对象并返回整数。该方法的签名将如下所示:

public static int GetInteger(object readerValue)
{
     int outputInt = 0;
     int.TryParse(readerValue.ToString(), out outputInt);
     return outputInt;
}

以便您的代码可以重写为:

while (reader.Read())
{
    result.Add(new FilesDTO
    {
        EmailID = reader["EmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["EmailID"]),
        DocumentID = GetInteger(reader["DocumentID"]),
        Name = reader["Name"] == DBNull.Value ? default(string) : Convert.ToString(reader["Name"]),
        DocumentName = reader["DocumentName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DocumentName"]),
        StatusID = GetInteger(reader["StatusID"]),
        SubscriberID = GetInteger(reader["SubscriberID"]),
        CreatedBy = GetInteger(reader["Initiater"]),
        DelegatorName = reader["DelegatorName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorName"]),
        DelegatorEmailID = reader["DelegatorEmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorEmailID"]),
        SignatureType = GetInteger(reader["SignatureType"])
    });
}