2017-11-16 22 views
0

現在、「追加」、「更新」、および「削除」には3つの異なるxml要素があります。これらの要素に続いて、SQLテーブルから追加、更新、または削除するいくつかの属性があります。私は "add"と "update"要素に対して "LOAD XML LOCAL INFILE"クエリを使用することができましたが、 "LOAD XML LOCAL INFILE"クエリの削除/削除オプションはありません。以下は、get/setメソッド、Class1クラスと私のメインメソッドクラスです。xmlファイルの属性に基づいてSQLテーブル行を削除するクエリC#

メインクラス

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クラス

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; } 
     } 
    } 
} 

答えて

0

以来、私の私は、そう

OdbcCommand Command3 = new OdbcCommand("DELETE FROM item WHERE item_id = " + el.Attribute("item_id").Value, connection); 

また同様に、クエリに自分の属性と値を追加することによってこの問題を解決することができましたクエリは、私のXMLファイルから直接情報を取得するので、getメソッドとsetメソッドは必要ありません。

+0

SQLインジェクション攻撃を避けるために、クエリをパラメータ化する必要があります。 – Sunil

関連する問題