查询根据xml文件中的属性删除sql表行c#

时间:2017-11-16 20:45:09

标签: c# mysql xml

我目前有3个不同的xml元素来"添加","更新"和"删除"。在这些元素之后,我有一些属性可以从我的sql表中添加,更新或删除。我能够使用" LOAD XML LOCAL INFILE"查询"添加"和"更新"元素,但" LOAD XML LOCAL INFILE"没有删除/删除选项。查询。以下是我的get / set方法,Class1类和我的主方法类。

主类

using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string conString = "Driver={MySQL ODBC 5.3 ANSI Driver};"
                + "Server=WWW;Port=XXX;"
                + "Database=inventory;"
                + "uid=YYY;pwd=ZZZ";
            OdbcConnection connection = new OdbcConnection(conString);

            {
                XDocument theFile = XDocument.Load("C:\\Users\\Bob\\Documents\\Update.xml");
                foreach (XElement el in theFile.Root.Elements())
                {
                    if (el.Name == "ADD")
                    {
                       Console.WriteLine("Item {0} ", el.Attribute("invent_id").Value + " was successfully added.");
                        Console.ReadLine();

                        OdbcCommand Command1 = new OdbcCommand("LOAD XML LOCAL INFILE 'C:/Users/Adam/Documents/Update.xml' INTO TABLE item ROWS IDENTIFIED BY '<ADD>'", connection);
                        connection.Open();
                        OdbcDataReader reader = Command1.ExecuteReader();
                        connection.Close();
                    }
                    else if (el.Name == "UPDATE")
                    {
                        Console.WriteLine("Item {0} ", el.Attribute("invent_id").Value + " was successfully updated.");
                        Console.ReadLine();

                        OdbcCommand Command2 = new OdbcCommand("LOAD XML LOCAL INFILE 'C:/Users/Adam/Documents/Update.xml' REPLACE INTO TABLE item ROWS IDENTIFIED BY '<UPDATE>'", connection);
                        connection.Open();
                        OdbcDataReader reader = Command2.ExecuteReader();
                        connection.Close();
                    }
                    else if (el.Name == "DELETE")
                    {
                        Console.WriteLine("Item {0} ", el.Attribute("invent_id").Value + " was successfully deleted."); ;
                        Console.ReadLine();

                        OdbcCommand Command3 = new OdbcCommand("", connection, connection);
                        connection.Open();
                        OdbcDataReader reader = Command3.ExecuteReader();
                        connection.Close();
                    }
                    else
                    {
                       Console.WriteLine( "Nothing to do");
                    }
                }
            }
        }
    }
}

Class1 Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApp1
{
    public class Class1
    {
        public static object Item_ID { get; set; }
        public static object Invent_id { get; set; }
        public static object Itemsize { get; set; }
        public static object Color { get; set; }
        public static decimal Curr_price { get; set; }
        public static object Qoh { get; set; }
    }
    public class transactions
    {
        [XmlRoot(ElementName = "UPDATE")]
        public class UPDATE
        {
            [XmlAttribute(AttributeName = "qoh")]
            public string Qoh { get; set; }
            [XmlAttribute(AttributeName = "curr_price")]
            public string Curr_price { get; set; }
            [XmlAttribute(AttributeName = "color")]
            public string Color { get; set; }
            [XmlAttribute(AttributeName = "itemsize")]
            public string Itemsize { get; set; }
            [XmlAttribute(AttributeName = "invent_id")]
            public string Invent_id { get; set; }
            [XmlAttribute(AttributeName = "item_id")]
            public string Item_id { get; set; }
        }

        [XmlRoot(ElementName = "ADD")]
        public class ADD
        {
            [XmlAttribute(AttributeName = "qoh")]
            public string Qoh { get; set; }
            [XmlAttribute(AttributeName = "curr_price")]
            public string Curr_price { get; set; }
            [XmlAttribute(AttributeName = "color")]
            public string Color { get; set; }
            [XmlAttribute(AttributeName = "itemsize")]
            public string Itemsize { get; set; }
            [XmlAttribute(AttributeName = "invent_id")]
            public string Invent_id { get; set; }
            [XmlAttribute(AttributeName = "item_id")]
            public string Item_id { get; set; }
        }

        [XmlRoot(ElementName = "DELETE")]
        public class DELETE
        {
            [XmlAttribute(AttributeName = "item_id")]
            public string Item_id { get; set; }
        }

        [XmlRoot(ElementName = "transactions")]
        public class Transactions
        {
            [XmlElement(ElementName = "UPDATE")]
            public UPDATE UPDATE { get; set; }
            [XmlElement(ElementName = "ADD")]
            public List<ADD> ADD { get; set; }
            [XmlElement(ElementName = "DELETE")]
            public DELETE DELETE { get; set; }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我能够通过将属性和值添加到查询中来解决这个问题,如此

https://example.com

此外,由于我的查询直接从我的xml文件中提取信息,因此不需要我的get和set方法。