我正在编写一个将XML数据映射到数据库表的程序。
这是我尝试的方式:
namespace TESTMAPPNING2
{
class Program
{
static void Main(string[] args)
{
Test();
}
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,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) values(@PersonalIdentityNumber, @ProtectedIdentity, @ReferedCivicRegistrationNumber,@UnregistrationReason,@UnregistrationDate,@MessageComputerComputer,@GivenNameNumber,@FirstName,@LastName,@NotifyName,@NationalRegistrationDate,@NationalRegistrationCountyCode,@NationalRegistrationMunicipalityCode,@NationalRegistrationCoAddress,@NationalRegistrationDistributionAddress1,@NationalRegistrationDistributionAddress2,@NationalRegistrationPostCode,@NationalRegistrationCity,@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.Parameters.AddWithValue("@ProtectedIdentity", ProtectedIdentity);
command1.Parameters.AddWithValue("@ReferedCivicRegistrationNumber", ReferedCivicRegistrationNumber);
command1.Parameters.AddWithValue("@UnregistrationReason", UnregistrationReason);
command1.Parameters.AddWithValue("@UnregistrationDate", UnregistrationDate);
command1.Parameters.AddWithValue("@MessageComputerComputer", MessageComputerComputer);
command1.Parameters.AddWithValue("@GivenNameNumber", GivenNameNumber);
command1.Parameters.AddWithValue("@FirstName", FirstName);
command1.Parameters.AddWithValue("@LastName", LastName);
command1.Parameters.AddWithValue("@NotifyName", NotifyName);
command1.Parameters.AddWithValue("@NationalRegistrationDate", NationalRegistrationDate);
command1.Parameters.AddWithValue("@NationalRegistrationCountyCode", NationalRegistrationCountyCode);
command1.Parameters.AddWithValue("@NationalRegistrationMunicipalityCode", NationalRegistrationMunicipalityCode);
command1.Parameters.AddWithValue("@NationalRegistrationCoAddress", NationalRegistrationCoAddress);
command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress1", NationalRegistrationDistributionAddress1);
command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress2", NationalRegistrationDistributionAddress2);
command1.Parameters.AddWithValue("@NationalRegistrationPostCode", NationalRegistrationPostCode);
command1.Parameters.AddWithValue("@NationalRegistrationCity", NationalRegistrationCity);
command1.Parameters.AddWithValue("@NationalRegistrationNotifyDistributionAddress", NationalRegistrationNotifyDistributionAddress);
command1.Parameters.AddWithValue("@NationalRegistrationNotifyPostCode", NationalRegistrationNotifyPostCode);
command1.Parameters.AddWithValue("@NationalRegistrationNotifyCity", NationalRegistrationNotifyCity);
command1.Parameters.AddWithValue("@ForeignDistrubtionAddress1", ForeignDistrubtionAddress1);
command1.Parameters.AddWithValue("@ForeignDistrubtionAddress2", ForeignDistrubtionAddress2);
command1.Parameters.AddWithValue("@ForeignDistrubtionAddress3", ForeignDistrubtionAddress3);
command1.Parameters.AddWithValue("@ForeignDistrubtionCountry", ForeignDistrubtionCountry);
command1.Parameters.AddWithValue("@ForeignDate", ForeignDate);
command1.Parameters.AddWithValue("@BirthCountyCode", BirthCountyCode);
command1.Parameters.AddWithValue("@BirthParish", BirthParish);
command1.Parameters.AddWithValue("@ForeignBirthCity", ForeignBirthCity);
command1.Parameters.AddWithValue("@CitizenshipCode", CitizenshipCode);
command1.Parameters.AddWithValue("@CitizenshipDate", CitizenshipDate);
command1.Parameters.AddWithValue("@Email", Email);
command1.Parameters.AddWithValue("@Telephone ", Telephone);
command1.Parameters.AddWithValue("@Mobiletelephone", Mobiletelephone);
command1.Parameters.AddWithValue("@Gender", Gender);
command1.Parameters.AddWithValue("@NotNewsPaper", NotNewsPaper);
command1.Parameters.AddWithValue("@Note", Note);
command1.Parameters.AddWithValue("@StatusCode", StatusCode);
command1.Parameters.AddWithValue("@NationalRegistrationCode", NationalRegistrationCode);
command1.Parameters.AddWithValue("@RegistrationDate", RegistrationDate);
command1.Parameters.AddWithValue("@LastUpdatedFromNavet", LastUpdatedFromNavet);
command1.Parameters.AddWithValue("@TemporaryDistrubtionAddress1", TemporaryDistrubtionAddress1);
command1.Parameters.AddWithValue("@TemporaryDistrubtionAddress2", TemporaryDistrubtionAddress2);
command1.Parameters.AddWithValue("@TemporaryDistrubtionAddress3", TemporaryDistrubtionAddress3);
command1.Parameters.AddWithValue("@TemporaryDistrubtionCountry", TemporaryDistrubtionCountry);
command1.Parameters.AddWithValue("@Password", Password);
command1.Parameters.AddWithValue("@VisibilityLevel", VisibilityLevel);
command1.Parameters.AddWithValue("@LastChangedBy", LastChangedBy);
command1.Parameters.AddWithValue("@LastChangedDate", LastChangedDate);
command1.Parameters.AddWithValue("@SeamanIdentity", SeamanIdentity);
command1.Parameters.AddWithValue("@Category", Category);
command1.ExecuteNonQuery();
}
connection.Close();
Console.WriteLine("Done");
}
}
}
我在command1.ExecuteNonQuery();
错误说
名称&#34; NationalRegistrationNotifyDistributionAddress&#34;在这种情况下是不允许的。有效表达式是常量,常量表达式和(在某些上下文中)变量。不允许使用列名。
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>
为什么我会收到错误?
最重要的是,我怎样才能翻译&#34;用于匹配表列的XML标记?
答案 0 :(得分:0)
您没有通过所有字段。您必须为其余部分添加参数。
command1.Parameters.AddWithValue("@NationalRegistrationNotifyDistributionAddress", yourData);
// same for the rest fields
此外,在第一个参数之后,sql中缺少@
。
答案 1 :(得分:0)
在你的长代码行中找到
at
第一个是 values(@PersonalIdentityNumber, NationalRegistrationNotifyDistribution ...
,第二个不是。
您必须知道使用列名称的位置以及使用变量名称的位置。
您可以在@
中使用文字值,但是 - 如果您使用参数(使用VALUES
,您必须声明所有,并在命令中添加值对象。出于测试目的,您只需添加以下内容:
@
使用文字值进行测试或添加所有参数并使用command1.Parameters.AddWithValue("@PersonalIdentityNumber", ...
正确命名
答案 2 :(得分:0)
也许尝试用所有XML内容编写序列化类,然后反序列化它(将XML读入类中)。最好是使用XML具有内部节点的内部类,而不仅仅是平面XML。
我认为这只是一次尝试,所以下一个训练点可能是分裂单个怪物&#34; DB中的表进入逻辑链接单元(又称规范化)。