我正在编写一个将XML数据映射到数据库表的程序。
这是我尝试的方式:
命名空间TESTMAPPNING2 { 课程 { static void Main(string [] args) { 测试(); }
static void Test()
{
string connetionString = null;
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
XmlReader xmlFile;
string sql = null;
//The XML-tags that i want to insert in the table columns
string PersonalIdentityNumber = null;
//string ProtectedIdentity = null;
//int ReferedCivicRegistrationNumber = 0;
//string UnregistrationReason = null;
//DateTime? UnregistrationDate = null;
//string MessageComputerComputer = null;
//int GivenNameNumber = 0;
//string FirstName = null;
//string LastName = null;
//string NotifyName = null;
//DateTime? NationalRegistrationDate = null;
//string NationalRegistrationCountyCode = null;
//string NationalRegistrationMunicipalityCode = null;
//string NationalRegistrationCoAddress = null;
//string NationalRegistrationDistributionAddress1 = null;
//string NationalRegistrationDistributionAddress2 = null;
//string NationalRegistrationPostCode = null;
//string NationalRegistrationCity = null;
//string NationalRegistrationNotifyDistributionAddress = null;
//string NationalRegistrationNotifyPostCode = null;
//string NationalRegistrationNotifyCity = null;
//string ForeignDistrubtionAddress1 = null;
//string ForeignDistrubtionAddress2 = null;
//string ForeignDistrubtionAddress3 = null;
//string ForeignDistrubtionCountry = null;
//string ForeignDate = null;
//string BirthCountyCode = null;
//string BirthParish = null;
//string ForeignBirthCity = null;
//string CitizenshipCode = null;
//DateTime? CitizenshipDate = null;
//string Email = null;
//string Telephone = null;
//string Mobiletelephone = null;
//string Gender = null;
//string NotNewsPaper = null;
//string Note = null;
//string StatusCode = null;
//string NationalRegistrationCode = null;
//DateTime? RegistrationDate = null;
//DateTime? LastUpdatedFromNavet = null;
//string TemporaryDistrubtionAddress1 = null;
//string TemporaryDistrubtionAddress2 = null;
//string TemporaryDistrubtionAddress3 = null;
//string TemporaryDistrubtionCountry = null;
//string Password = null;
//string VisibilityLevel = null;
//string LastChangedBy = null;
//DateTime? LastChangedDate = null;
//int SeamanIdentity = 0;
//string Category = null;
connetionString = "Data Source=tsrv2062;Initial Catalog=Bums;User ID=BumsUser;Password=2tusen7Bums";
connection = new SqlConnection(connetionString);
xmlFile = XmlReader.Create("navetout.xml", new XmlReaderSettings());
ds.ReadXml(xmlFile);
int i = 0;
connection.Open();
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
PersonalIdentityNumber = ds.Tables[0].Rows[i].ItemArray[0].ToString();
SqlCommand command1 = new SqlCommand("INSERT INTO Seamen(PersonalIdentityNumber, ProtectedIdentity, ReferedCivicRegistrationNumber, UnregistrationReason, UnregistrationDate, MessageComputerComputer, GivenNameNumber, FirstName, LastName, NotifyName, NationalRegistrationDate, NationalRegistrationCountyCode, NationalRegistrationMunicipalityCode, NationalRegistrationCoAddress, NationalRegistrationDistributionAddress1, NationalRegistrationDistributionAddress2, NationalRegistrationPostCode, NationalRegistrationCity ) values(@PersonalIdentityNumber, NationalRegistrationNotifyDistributionAddress, NationalRegistrationNotifyPostCode, NationalRegistrationNotifyCity, ForeignDistrubtionAddress1, ForeignDistrubtionAddress2, ForeignDistrubtionAddress3, ForeignDistrubtionCountry, ForeignDate, BirthCountyCode, BirthParish, ForeignBirthCity, CitizenshipCode, CitizenshipDate, Email, Telephone, Mobiletelephone, Gender, NotNewsPaper, Note, StatusCode, NationalRegistrationCode, RegistrationDate, LastUpdatedFromNavet, TemporaryDistrubtionAddress1,TemporaryDistrubtionAddress2, TemporaryDistrubtionAddress3, TemporaryDistrubtionCountry, Password, VisibilityLevel, LastChangedBy, LastChangedDate, SeamanIdentity, Category)", connection);
command1.Parameters.AddWithValue("@PersonalIdentityNumber", PersonalIdentityNumber);
command1.ExecuteNonQuery();
}
connection.Close();
Console.WriteLine("Done");
}
}
}
我在command1.ExecuteNonQuery();
错误说
在此上下文中不允许使用名称“NationalRegistrationNotifyDistributionAddress”。有效表达式是常量,常量表达式和(在某些上下文中)变量。不允许使用列名。
为什么我会收到错误?
XML navetout.xml
看起来像这样:
<?xml version="1.0" encoding="utf-8"?> <ArrayOfFolkbokforingspostTYPE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <FolkbokforingspostTYPE>
<Sekretessmarkering xsi:nil="true" />
<Personpost>
<PersonId>
<PersonNr>194903188953</PersonNr>
</PersonId>
<HanvisningsPersonNr xsi:nil="true" />
<Namn>
<Tilltalsnamnsmarkering>20</Tilltalsnamnsmarkering>
<Fornamn>Erik Lennart</Fornamn>
<Mellannamn xsi:nil="true" />
<Efternamn>Enberg</Efternamn>
<Aviseringsnamn xsi:nil="true" />
</Namn>
<Folkbokforing>
<Folkbokforingsdatum>20060512</Folkbokforingsdatum>
<LanKod>25</LanKod>
<KommunKod>14</KommunKod>
<ForsamlingKod xsi:nil="true" />
<Fastighetsbeteckning>PÅLÄNGE 6:38</Fastighetsbeteckning>
<FiktivtNr>0</FiktivtNr>
</Folkbokforing>
<Adresser>
<Folkbokforingsadress>
<CareOf xsi:nil="true" />
<Utdelningsadress1 xsi:nil="true" />
<Utdelningsadress2>STORVÄGEN 65</Utdelningsadress2>
<PostNr>95204</PostNr>
<Postort>PÅLÄNG</Postort>
</Folkbokforingsadress>
<Riksnycklar>
<FastighetsId>250015520</FastighetsId>
<AdressplatsId>2416233</AdressplatsId>
<LagenhetsId>22143838</LagenhetsId>
</Riksnycklar>
</Adresser>
<Fodelse>
<HemortSverige>
<FodelselanKod>25</FodelselanKod>
<Fodelseforsamling>NEDERKALIX</Fodelseforsamling>
</HemortSverige>
</Fodelse>
<Medborgarskap>
<MedborgarskapslandKod>SE</MedborgarskapslandKod>
<Medborgarskapsdatum>0</Medborgarskapsdatum>
</Medborgarskap>
</Personpost> </FolkbokforingspostTYPE> <FolkbokforingspostTYPE>
<Sekretessmarkering xsi:nil="true" />
<Personpost>
<PersonId>
<TilldelatPersonNrSamordningsNr>195405869370</TilldelatPersonNrSamordningsNr>
</PersonId>
<HanvisningsPersonNr xsi:nil="true" />
<Namn>
<Fornamn styrkt="J">Eduardo Metran</Fornamn>
<Mellannamn xsi:nil="true" />
<Efternamn styrkt="J">Garcia</Efternamn>
<Aviseringsnamn xsi:nil="true" />
</Namn>
<Folkbokforing>
<Folkbokforingsdatum xsi:nil="true" />
<LanKod>14</LanKod>
<KommunKod xsi:nil="true" />
<ForsamlingKod xsi:nil="true" />
<Fastighetsbeteckning xsi:nil="true" />
</Folkbokforing>
<Adresser />
<Fodelse>
<OrtUtlandet>
<FodelseortUtland styrkt="J">Sta Cruz Zambales</FodelseortUtland>
<Fodelseland>FILIPPINERNA</Fodelseland>
</OrtUtlandet>
</Fodelse>
<Medborgarskap>
<MedborgarskapslandKod styrkt="J">PH</MedborgarskapslandKod>
<Medborgarskapsdatum>0</Medborgarskapsdatum>
</Medborgarskap>
</Personpost> </FolkbokforingspostTYPE> </ArrayOfFolkbokforingspostTYPE>
为什么我会收到错误?
最重要的是,我如何“翻译”XML标签以匹配表格列?