【发布时间】:2017-11-16 15:15:16
【问题描述】:
我正在尝试在 C# 中创建一个 sql 查询,它将我的数据从我的 xml 文件传输到 sql 服务器。我成功地将我的 XML 反序列化为“获取、设置”方法并将我的 xml 输出到控制台。过去一天我一直在搜索 Google,以查找如何创建插入查询以将我的 xml 添加到我的 sql 表中。这是我迄今为止为我的两个课程所做的。 Class1 是我的“get、set”方法类,Program 是我的“main”方法类。
Class1.cs
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; }
}
}
}
主类
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=XXX;Port=YYY;"
+ "Database=inventory;"
+ "uid=ZZZ;pwd=XYZ";
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("{0} {1} {2} {3} {4} {5} {6}", el.Name, el.Attribute("qoh").Value, el.Attribute("curr_price").Value, el.Attribute("color").Value, el.Attribute("itemsize").Value, el.Attribute("invent_id").Value, el.Attribute("item_id").Value);
Console.ReadLine();
OdbcCommand Command1 = new OdbcCommand("INSERT INTO item (item_id, invent_id, itemsize, color, curr_price, qoh) VALUES( ?, ?, ?, ?, ?, ?) ", connection);
Command1.Parameters.Add("@SZ", OdbcType.VarChar).Value = Class1.Itemsize;
Command1.Parameters.Add("@COL", OdbcType.VarChar).Value = Class1.Color;
Command1.Parameters.Add("@PR", OdbcType.Double).Value = (double)Class1.Curr_price;
Command1.Parameters.Add("@QOH", OdbcType.Int).Value = Class1.Qoh;
Command1.Parameters.Add("@ID", OdbcType.Int).Value = Class1.Item_ID;
Console.ReadLine();
}
else if (el.Name == "UPDATE")
{
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6}", el.Name, el.Attribute("qoh").Value, el.Attribute("curr_price").Value, el.Attribute("color").Value, el.Attribute("itemsize").Value, el.Attribute("invent_id").Value, el.Attribute("item_id").Value);
Console.ReadLine();
}
else if (el.Name == "DELETE")
{
Console.WriteLine("{0} {1}", el.Name, el.Attribute("item_id").Value);
Console.ReadLine();
}
}
}
}
}
}
更新:
关于使用 MySQL LOAD 查询,这是我所拥有的,但不确定它是否正确。在阅读了有关 Infile Loading 的链接后,我似乎只是将查询指向 xml 文件位置,它会将数据复制到我的 SQL 表中的字段中。我还以正确的顺序更改了参数,但仍然无法正常工作。似乎没有看到我的 xml 文件中的值或我的参数行不正确。
OdbcCommand Command1 = new OdbcCommand("LOAD XML LOCAL INFILE 'C:\\Users\\Bob\\Documents\\Update.xml' INTO TABLE item ROWS IDENTIFIED BY '<ADD>'");
【问题讨论】:
-
MySQL 5.5+ 支持 LOAD XML 读取 (dev.mysql.com/doc/refman/5.5/en/load-xml.html)
-
按照它们在 INSERT 语句中出现的顺序添加参数。
-
我是否缺少执行阅读器行?如果使用 LOAD XML INFILE,是否需要这样做?
-
当我从 MySQL 中的“更新”帖子中运行我的行时,它工作正常。但不在我的 c# 脚本中...