切换情况C#存储数据

发布于 2025-01-24 13:48:23 字数 3114 浏览 0 评论 0原文

我正在数据库中存储来自XML文件的数据,但是我找不到正确的方法。

我正在使用开关案例浏览标签并获取正常工作的数据,但是我希望数据存储在一行中,以便它们必须同时使用数据库。

并非每个标签都有相同的数据存储。因此,例如标签1具有身份,但标签2为null,标签3再次具有数据。

string identity= null;
string birth = null;
if (xmlFile.Depth >= 9)
{
    switch (xmlFile.Name)
    {
        case "identity":
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            identity = listXML[listXML.Count - 1].TagLine;
            break;
        case "birth"
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            birth = listXML[listXML.Count - 1].TagLine;
            break;
    }

我在这里不包括课程,因为我认为它们与之无关。如果我调试出生和身份有价值。

private static void InsertValues(string a, string b)
{
    using (NpgsqlConnection con = ConnectorDatabase.GetConnection())
    {
        con.Open();

        string sql = @"INSERT INTO public.Database(IDENTITY, BIRth)values('" + a + "','" + b + "')";
        NpgsqlCommand cmd = new NpgsqlCommand(psql, con);
        int n = cmd.ExecuteNonQuery();
        con.Close();
    }
}   

因此,如果我在开关案例语句之后使用我的方法,则值为null;

如果我在内部使用它,例如出生案例,它仅包括出生案例为真的数据,而每次都不是这样,所以我会错过数据。

我尝试了一些事情,但最好的结果是,在我的数据库中,每个数据都在那里,但在同一行中不在,因此身份x在他的行中只有空值,而列总是具有其数据。希望您明白我的意思是

编辑: 我现在意识到为什么在开关案例之后无法使用它。因此,实际上我认为我必须这样做:

string identity= null;
string birth = null;
if (xmlFile.Depth >= 9)
{
    switch (xmlFile.Name)
    {
        case "identity":
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            identity = listXML[listXML.Count - 1].TagLine;
            break;
        case "birth"
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            birth = listXML[listXML.Count - 1].TagLine;
            break;
    }
    InsertValues(identiy, birth);

我没有包含所有代码,但是XMLFILE是XMLTEXTREADER。 在ther的循环中(xmlfile.read)。 这导致了问题,因此我说XML文件有很多标签,但是我只需要身份和出生。 如果我在开关后放置。它通过时循环,下一个标签可能是“ Idnumber”。 因此,它不会在开关情况下跳跃,我会再次执行insertvalues()方法。这是错误的。这意味着我获得了双重数据,这会导致主要键错误。

已解决 对于在同一问题中跑来跑去的任何人,我宣布布尔值为真实。

string identity= null;
string birth = null;
bool isRunning= true;
if (xmlFile.Depth >= 9)
{
    switch (xmlFile.Name)
    {
        case "identity":
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            identity = listXML[listXML.Count - 1].TagLine;
            isRunning = true;
break;
        case "birth"
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            birth = listXML[listXML.Count - 1].TagLine;
            break;
    }
if(isRunning == true)
{
    InsertValues(identiy, birth);
    isRunning = false;
}

我知道每个标签在XML文件中都有身份,因此我可以肯定地说,每次陷入困境时,标签都将在TRUE上放置并转到更多代码。 它帮助我写了问题。也许我应该经常这样做。

XML文件示例

<person>
<identity V="Peter"/>
<Birth V="1995"/>
<IDNUMBER V="1021"/>
<JOB V="JANITOR"/>
</person>

问题是我所说的,由于while循环,它会贯穿每个标签,当它击中idnumber并跳过了开关案例时,它会导致我的主键问题。导致“识别”和“出生”的值没有改变。

I'm storing data from an xml file in a database, but I can't find how to do it the right way.

I'm using a switch case to go through the tags and get my data which works fine, but I want the data to be stored in one row so they have to go simultaneously in the database.

Not every tag has the same data stored. So for example Tag 1 has identity but Tag 2 is null, Tag 3 again has data.

string identity= null;
string birth = null;
if (xmlFile.Depth >= 9)
{
    switch (xmlFile.Name)
    {
        case "identity":
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            identity = listXML[listXML.Count - 1].TagLine;
            break;
        case "birth"
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            birth = listXML[listXML.Count - 1].TagLine;
            break;
    }

I don't include the classes here because they are not relevant in my opinion. if I debug birth and identity have values.

private static void InsertValues(string a, string b)
{
    using (NpgsqlConnection con = ConnectorDatabase.GetConnection())
    {
        con.Open();

        string sql = @"INSERT INTO public.Database(IDENTITY, BIRth)values('" + a + "','" + b + "')";
        NpgsqlCommand cmd = new NpgsqlCommand(psql, con);
        int n = cmd.ExecuteNonQuery();
        con.Close();
    }
}   

So if I use my method after the switch case statement the values are null;

if I use it inside for Example the Birth case it only includes data where the case of birth is true which isn't the case every time so I will miss out on Data.

I tried some things but the best outcome was that in my database every data was there, but not in the same row so identity x had only null values in his row but the column what always have its data. Hope you understand what I mean

EDIT:
I realized now why I can't use it after the switch case. So actually I think I have to do it like that:

string identity= null;
string birth = null;
if (xmlFile.Depth >= 9)
{
    switch (xmlFile.Name)
    {
        case "identity":
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            identity = listXML[listXML.Count - 1].TagLine;
            break;
        case "birth"
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            birth = listXML[listXML.Count - 1].TagLine;
            break;
    }
    InsertValues(identiy, birth);

I didn't include all of the code but the xmlFile is an XmlTextReader.
which is in a while loop with while( xmlFile.Read).
Which leads to the Problem, so as I said the Xml File got a lot of Tags, but I only need identity and birth.
if I put it after the switch. it goes through the while loop and the next tag may be "IDNUMBER".
So it doesn't jump in the switch case and I would do the InsertValues() method again. which is wrong. That means I get Double Data which leads to an Primary Key error.

SOLVED
For anyone who run in the same Problem I declared a boolean as true.

string identity= null;
string birth = null;
bool isRunning= true;
if (xmlFile.Depth >= 9)
{
    switch (xmlFile.Name)
    {
        case "identity":
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            identity = listXML[listXML.Count - 1].TagLine;
            isRunning = true;
break;
        case "birth"
            listXML.Add(new SearchedTags() { TagLine = xmlFile.GetAttribute("V") });
            birth = listXML[listXML.Count - 1].TagLine;
            break;
    }
if(isRunning == true)
{
    InsertValues(identiy, birth);
    isRunning = false;
}

I know that every Tag has identity in the XML File so I can for sure say every time it goes trough the Tag it will put on true and goes to further code.
It helped me writing the Problem down. Maybe I should do this more often.

XML FILE EXAMPLE

<person>
<identity V="Peter"/>
<Birth V="1995"/>
<IDNUMBER V="1021"/>
<JOB V="JANITOR"/>
</person>

Problem was as I said that because of the while loop, it goes through every tag, when it hits IDNUMBER and JOB it skipped the switch case, causing problems with my primary Key. Cause the values of "identiy" and "birth" didn't changed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2025-01-31 13:48:23

可能是与范围相关的问题,您可以尝试在静态特性中存储身份和出生。

Might be a scope related problem, you could try storing identity and birth in static properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文