cannot convert value of parameter date from system string to system datetime

后端 未结 5 1191
终归单人心
终归单人心 2020-12-22 15:31
public Kupac(SqlDataReader reader)
{
    KupacId = Convert.ToInt32(reader[\"KupacId\"]);
    Ime = reader[\"Ime\"].ToString();
    Prezime = reader[\"Prezime\"].ToSt         


        
5条回答
  •  滥情空心
    2020-12-22 16:22

    what Smith.h.Neil said, but I would use the other version of try parse if the value could be an invalid date.

     string validDate = "2014-04-17";
     string invalidDate = "not a date";
     DateTime date;
     DateTime date2;
     bool isValidDate = DateTime.TryParse(validDate, out date);
     bool isValidDate2 = DateTime.TryParse(invalidDate , out date2);
    

    http://msdn.microsoft.com/en-us/library/system.datetime.aspx

    Assuming ClanOd is a DateTime Replace

    ClanOd = (DateTime)reader["ClanOd"];
    

    With

    DateTime date;
    bool isValid = DateTime.TryParse(reader["ClanOd"].ToString(), out date);
    if(isValid)
        ClanOd  = date;
    

提交回复
热议问题