简体   繁体   中英

Conversion failed when converting datetime from character string in C#

I have the following code:

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;
}

This gives me the error when running:

Conversion failed when converting datetime from character string.

The error occurred when I added

"CitizenshipDate = @CitizenshipDate, "+

to the query string

Can anyone see where the problem is?

UPDATE:

Here are the values:

    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; }
}

Verify the both p.CitizenshipDate and CitizenshipDate in Seamen table declared as DateTime. If you must declare p.CitizenshipDate as string, cast its when you add the parameter value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM