从C#中的字符串转换datetime时转换失败

时间:2016-05-03 07:57:55

标签: c# datetime

我有以下代码:

string connetionString = null;
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
XmlReader xmlFile;

connetionString = "Data Source=tsrv2062;Initial Catalog=Bums;User ID=BumsUser;Password=2tusen7Bums";


xmlFile = XmlReader.Create("navetout.xml", new XmlReaderSettings());
ds.ReadXml(xmlFile);


using (var connection = new SqlConnection(connetionString))
{
    connection.Open();
    DateTime datum = DateTime.Now;

    foreach (Person p in myPersons)
    {
        SqlCommand command1 = Avreg(p.UnregistrationReason, p.GivenNameNumber, connection);

    command1.Parameters.AddWithValue("@PersonalIdentityNumber", string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
        command1.Parameters.AddWithValue("@FirstName", p.FirstName);
        command1.Parameters.AddWithValue("@LastName", p.LastName);
        command1.Parameters.AddWithValue("@NationalRegistrationCountyCode", p.NationalRegistrationCountyCode);
        command1.Parameters.AddWithValue("@NationalRegistrationMunicipalityCode", p.NationalRegistrationMunicipalityCode);
        command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress1", p.NationalRegistrationDistributionAddress1 ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress2", p.NationalRegistrationDistributionAddress2 ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@NationalRegistrationPostCode", p.NationalRegistrationPostCode ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@NationalRegistrationCity", p.NationalRegistrationCity ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@BirthCountyCode", p.BirthCountyCode ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@UnregistrationDate", p.UnregistrationDate ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@BirthParish", p.BirthParish ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@CitizenshipCode", p.CitizenshipCode);
        command1.Parameters.AddWithValue("@CitizenshipDate", p.CitizenshipDate ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@NationalRegistrationDate", p.NationalRegistrationDate ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@ForeignDistrubtionAddress1", p.ForeignDistrubtionAddress1 ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@ForeignDistrubtionAddress2", p.ForeignDistrubtionAddress2 ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@ForeignDistrubtionAddress3", p.ForeignDistrubtionAddress3 ?? DBNull.Value.ToString());
        command1.Parameters.AddWithValue("@ForeignBirthCity", p.ForeignBirthCity ?? DBNull.Value.ToString());

        command1.Parameters.AddWithValue("@LastChangedDate", datum);

        command1.ExecuteNonQuery();

    Console.WriteLine(string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));


    }
}

Console.WriteLine("Done");


}// Put a break-point here, then mouse-over PersonalIdentityNumber...  deserializedList contains everything if you need it
catch (Exception)
{

throw;
}
Console.ReadKey();
}

public static SqlCommand Avreg(string s, string t, SqlConnection connection)
{
var query = "UPDATE Seamen SET FirstName = @FirstName, "+
"LastName = @LastName, "+
"NationalRegistrationCountyCode = @NationalRegistrationCountyCode, "+
"NationalRegistrationMunicipalityCode = @NationalRegistrationMunicipalityCode, "+
"NationalRegistrationDistributionAddress1 = @NationalRegistrationDistributionAddress1, "+
"NationalRegistrationDistributionAddress2 = @NationalRegistrationDistributionAddress2, "+
"UnregistrationDate = @UnregistrationDate, "+
"NationalRegistrationPostCode = @NationalRegistrationPostCode, "+
"NationalRegistrationCity = @NationalRegistrationCity, "+
"BirthCountyCode = @BirthCountyCode, "+
"BirthParish = @BirthParish, "+
"CitizenshipCode = @CitizenshipCode, "+
"CitizenshipDate = @CitizenshipDate, "+
"NationalRegistrationDate = @NationalRegistrationDate, "+
"ForeignDistrubtionAddress1 = @ForeignDistrubtionAddress1, "+
"ForeignDistrubtionAddress2 = @ForeignDistrubtionAddress2, "+
"ForeignDistrubtionAddress3 = @ForeignDistrubtionAddress3, "+
"ForeignBirthCity = @ForeignBirthCity, " +
"LastChangedDate = @LastChangedDate";
SqlCommand command1;
return command1;
}

这在运行时给出了错误:

  

从字符串转换日期时转换失败。

添加

时发生错误
"CitizenshipDate = @CitizenshipDate, "+

到查询字符串

任何人都可以看到问题所在吗?

更新

以下是值:

    List<FolkbokforingspostTYPE> deserializedList = new List<FolkbokforingspostTYPE>();
    deserializedList = Deserialize<List<FolkbokforingspostTYPE>>();


    var myPersons = Deserialize<List<FolkbokforingspostTYPE>>()
        .Select(x => new Person
        {
            PersonalIdentityNumber = x.Personpost.PersonId.PersonNr,
            SpecialIdentityNumber = x.Personpost.PersonId.Tilltalsnamnsmarkering != null ? x.Personpost.PersonId.Tilltalsnamnsmarkering : null, 
            LastName = x.Personpost.Namn.Efternamn,
            FirstName = x.Personpost.Namn.Fornamn,
            NationalRegistrationCountyCode = x.Personpost.Folkbokforing.LanKod,
            NationalRegistrationMunicipalityCode = x.Personpost.Folkbokforing.KommunKod,
            ForeignDistrubtionAddress1 = x.Personpost.Adresser.Utlandsadress != null ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress1 : null,
            ForeignDistrubtionAddress2 = x.Personpost.Adresser.Utlandsadress != null ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress2 : null,
            ForeignDistrubtionAddress3 = x.Personpost.Adresser.Utlandsadress != null ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress3 : null,
            NationalRegistrationDistributionAddress1 = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.Utdelningsadress1 : null,
            NationalRegistrationDistributionAddress2 = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.Utdelningsadress2 : null,
            NationalRegistrationPostCode = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.PostNr : null,

            UnregistrationReason = x.Personpost.Avregistrering != null ? x.Personpost.Avregistrering.AvregistreringsorsakKod : null,
            UnregistrationDate = x.Personpost.Avregistrering != null ? x.Personpost.Avregistrering.Avregistreringsdatum : null,

            NationalRegistrationCity = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.Postort : null,
            BirthCountyCode = x.Personpost.Fodelse.HemortSverige != null ? x.Personpost.Fodelse.HemortSverige.FodelselanKod : null,
            BirthParish = x.Personpost.Fodelse.HemortSverige != null ? x.Personpost.Fodelse.HemortSverige.Fodelseforsamling : null,
            CitizenshipDate = x.Personpost.Medborgarskap != null ? x.Personpost.Medborgarskap.Medborgarskapsdatum : null,
            GivenNameNumber = x.Personpost.Namn.Tilltalsnamnsmarkering != null ? x.Personpost.Namn.Tilltalsnamnsmarkering : null,
            NationalRegistrationDate = x.Personpost.Folkbokforing != null ? x.Personpost.Folkbokforing.Folkbokforingsdatum : null,

            ForeignBirthCity = x.Personpost.Fodelse.OrtUtlandet != null ? x.Personpost.Fodelse.OrtUtlandet.FodelseortUtland : null,




            CitizenshipCode = x.Personpost.Medborgarskap.MedborgarskapslandKod,

        });

class Person {
       public string PersonalIdentityNumber { get; set; }
       public string SpecialIdentityNumber { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string NationalRegistrationCountyCode { get; set; }
       public string NationalRegistrationMunicipalityCode { get; set; }
       public string NationalRegistrationDistributionAddress1 { get; set; }
       public string NationalRegistrationDistributionAddress2 { get; set; }
       public string NationalRegistrationPostCode { get; set; }
       public string NationalRegistrationCity { get; set; }
       public string BirthCountyCode { get; set; }
       public string BirthParish { get; set; }
       public string CitizenshipDate { get; set; }
       public string UnregistrationReason { get; set; }
       public string UnregistrationDate { get; set; }
       public string CitizenshipCode { get; set; }
       public string GivenNameNumber { get; set; }
       public string NationalRegistrationDate { get; set; }
       public string ForeignDistrubtionAddress1 { get; set; }
       public string ForeignDistrubtionAddress2 { get; set; }
       public string ForeignDistrubtionAddress3 { get; set; }
       public string ForeignBirthCity { get; set; }
}

1 个答案:

答案 0 :(得分:0)

验证Seamen表中的p.CitizenshipDate和CitizenshipDate都声明为DateTime。 如果必须将p.CitizenshipDate声明为字符串,请在添加参数值时强制转换它。