我正在尝试对XML进行deserilize并将其插入数据库。
这是我的代码:
static void Main(string[] args)
{
//try
//{
List<FolkbokforingspostTYPE> deserializedList = new List<FolkbokforingspostTYPE>();
deserializedList = Deserialize<List<FolkbokforingspostTYPE>>();
var myPersons = Deserialize<List<FolkbokforingspostTYPE>>()
.Select(x => new Person
{
PersonalIdentityNumber = x.Personpost.PersonId.PersonNr,
LastName = x.Personpost.Namn.Efternamn,
FirstName = x.Personpost.Namn.Fornamn,
NationalRegistrationCountyCode = x.Personpost.Folkbokforing.LanKod,
NationalRegistrationMunicipalityCode = x.Personpost.Folkbokforing.KommunKod,
NationalRegistrationDistributionAddress2 = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.Utdelningsadress2 : null,
NationalRegistrationPostCode = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.PostNr : null,
CitizenshipCode = x.Personpost.Medborgarskap.MedborgarskapslandKod
// and so on
});
var PersonalIdentityNber = deserializedList.Select(item =>
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;
SqlCommand command1 = new SqlCommand("UPDATE Seamen SET FirstName = @FirstName, LastName = @LastName, NationalRegistrationCountyCode = @NationalRegistrationCountyCode, NationalRegistrationMunicipalityCode = @NationalRegistrationMunicipalityCode,NationalRegistrationDistributionAddress2 = @NationalRegistrationDistributionAddress2,NationalRegistrationPostCode = @NationalRegistrationPostCode, CitizenshipCode = @CitizenshipCode, LastChangedDate = @LastChangedDate WHERE PersonalIdentityNumber = @PersonalIdentityNumber", connection);
foreach (Person p in myPersons)
{
command1.Parameters.Clear();
command1.Parameters.AddWithValue("@PersonalIdentityNumber", p.PersonalIdentityNumber);
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("@NationalRegistrationDistributionAddress2", p.NationalRegistrationDistributionAddress2);
command1.Parameters.AddWithValue("@NationalRegistrationPostCode", p.NationalRegistrationPostCode);
////command1.Parameters.AddWithValue("@NationalRegistrationCity", postOrt);
////command1.Parameters.AddWithValue("@BirthCountyCode", fodelselanKod);
////command1.Parameters.AddWithValue("@BirthParish", fodelseforsamling);
command1.Parameters.AddWithValue("@CitizenshipCode", p.CitizenshipCode);
//// command1.Parameters.AddWithValue("@CitizenshipDate", medborgarskapsdatum);
command1.Parameters.AddWithValue("@LastChangedDate", datum);
command1.ExecuteNonQuery();
Console.WriteLine(p.PersonalIdentityNumber);
}
}
Console.WriteLine("Done");
}
//}// Put a break-point here, then mouse-over PersonalIdentityNumber... deserializedList contains everything if you need it
// catch (Exception)
// {
// throw;
// }
// Console.ReadKey();
//}
class Person {
public string PersonalIdentityNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string NationalRegistrationCountyCode { get; set; }
public string NationalRegistrationMunicipalityCode { get; set; }
public string NationalRegistrationDistributionAddress2 { get; set; }
public string NationalRegistrationPostCode { get; set; }
public string CitizenshipCode { get; set; }
}
当我尝试这个时,我收到错误:
参数化查询&#39;(@ PersonalIdentityNumber nvarchar(12),@ FirstName nvarchar(14),@ L&#39;期待参数 &#39; @NationalRegistrationDistributionAddress2&#39;,未提供。
我不知道为什么会收到此错误。有谁知道我为什么会得到这个错误以及如何解决它?