我想将xml文件数据导出到sql数据库表。任何人都可以为此指导我吗?
答案 0 :(得分:4)
如果是SQL Server,我已经回答了类似的问题。看看以下帖子:
.NET: How to insert XML document into SQL Server
您可以使用那个小的c#部分来存储您的数据。您只需要修改表格和列字段。
class Program
{
private static void SaveXmlToDatabase(DbConnection connection,
XmlDocument xmlToSave)
{
String sql = "INSERT INTO xmlTable(xmlColumn) VALUES (@xml)";
using (DbCommand command = connection.CreateCommand())
{
XPathNavigator nav = xmlToSave.CreateNavigator();
string xml = nav.SelectSingleNode("/catalog/cd[title='Manowar']").InnerXml;
command.CommandText = sql;
command.Parameters.Add(
new SqlParameter("@xml", SqlDbType.Xml)
{Value = new SqlXml(new XmlTextReader(xml
, XmlNodeType.Document, null)) });
DbTransaction trans = connection.BeginTransaction();
try
{
command.ExecuteNonQuery();
trans.Commit();
}
catch (Exception)
{
trans.Rollback();
throw;
}
}
}
static void Main(string[] args)
{
XmlDocument document = new XmlDocument();
document.Load(args.First());
SqlConnection connection = new SqlConnection(
"Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;");
SaveXmlToDatabase(connection, document);
connection.Close();
}
}
答案 1 :(得分:1)