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; }
}
}
}
SQLインジェクション攻撃を避けるために、クエリをパラメータ化する必要があります。 – Sunil