我试图遍历PersonalIdentityNumber
但收到错误:
指数超出范围。它不能是负面的,必须更小 而不是人群的大小。
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, CitizenshipCode = @CitizenshipCode, LastChangedDate = @LastChangedDate WHERE PersonalIdentityNumber = @PersonalIdentityNumber", connection);
for (int i = 0; i < PersonalIdentityNumber.Count; i++) //<--Error
{
var personnummer = PersonalIdentityNumber[i];
var fornamn = FirstName[i];
var efternamn = LastName[i];
var lankod = LanKod[i];
var kommunkod = Kommunkod[i];
var utdelAdress2 = UtdelAdress2[i];
var medborgarskapslandKod = MedborgarskapslandKod[i];
command1.Parameters.Clear();
command1.Parameters.AddWithValue("@PersonalIdentityNumber", personnummer);
command1.Parameters.AddWithValue("@FirstName", fornamn);
command1.Parameters.AddWithValue("@LastName", efternamn);
command1.Parameters.AddWithValue("@NationalRegistrationCountyCode", lankod);
command1.Parameters.AddWithValue("@NationalRegistrationMunicipalityCode", kommunkod);
command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress2", utdelAdress2);
command1.Parameters.AddWithValue("@CitizenshipCode", medborgarskapslandKod);
command1.Parameters.AddWithValue("@LastChangedDate", datum);
command1.ExecuteNonQuery();
Console.WriteLine(personnummer);
}
}
Console.WriteLine("Done");
对象:
List<FolkbokforingspostTYPE> deserializedList = new List<FolkbokforingspostTYPE>();
deserializedList = Deserialize<List<FolkbokforingspostTYPE>>();
var PersonalIdentityNumber = deserializedList.Select(item => item.Personpost.PersonId.PersonNr).ToList();
var FirstName = deserializedList.Select(item => item.Personpost.Namn.Fornamn).ToList();
var LastName = deserializedList.Select(item => item.Personpost.Namn.Efternamn).ToList();
var LanKod = deserializedList.Select(item => item.Personpost.Folkbokforing.LanKod).ToList();
var Kommunkod = deserializedList.Select(item => item.Personpost.Folkbokforing.KommunKod).ToList();
var UtdelAdress2 = deserializedList.Select(item => item.Personpost.Adresser.Folkbokforingsadress.Utdelningsadress2).FirstOrDefault().ToList();
我反序列化XML并将值存储在这些对象上。
我的目标是使用XML中的值更新数据库中的表,它适用于foreach
循环,但很难处理那么多变量foreach
循环,这就是我希望它与for
循环
有谁知道我为什么会收到这个错误?
更新
显然,问题出在这一行: var utdelAdress2 = UtdelAdress2[i];
它是空的。
我现在的问题是:是否可以跳过空值并继续其余的?