C# parse string “0” to integer

前端 未结 8 957
抹茶落季
抹茶落季 2021-01-18 02:58

I have a new laptop at work and code that worked earlier in the week does not work today.

The code that worked before is, simplified:

while (dr.Read         


        
8条回答
  •  旧巷少年郎
    2021-01-18 03:35

    you should check dr["FieldName"] != DBNull.Value and you should use TryParse if it passes the DBNull test...

    if ( dr["FieldName"] != DBNull.Value )
    {
        int val = 0;
        if ( int.TryParse( dr["FieldName"], out val ) )
        {
            i = val;
        }
        else
        {
            i = 0; // or some default value
        }
    }
    

提交回复
热议问题