本文介绍了如何保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have the following XML file:
<?xml version="1.0"?>
<company>
<companyname>
<group>Accenture BPO</group>
<group>Accenture IT </group>
<group>Accenture Infotech</group>
</companyname>
<companyname>HCL</companyname>
</company>
hOW would I store it in a database table ?
请帮助一个新的bea ...我已经完成了工作,但没有成功..
pleasehelp a new bea...i worked on it but didnt get success..
推荐答案
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("report.xml"));
SqlConnection connection = new SqlConnection("CONNECTION STRING");
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "report_table";
//if your DB col names don’t match your XML element names 100%
//then relate the source XML elements (1st param) with the destination DB cols
sbc.ColumnMappings.Add("campaign", "campaign_id");
sbc.ColumnMappings.Add("cost", "cost_USD");
if(connection.State==ConnectionState.Closed)
{
connection.Open();
}
//table 4 is the main table in this dataset
sbc.WriteToServer(reportData.Tables[4]);
connection.Close();
问候!
阿曼
*不要忘了标记答案或评论.
Regards!
Aman
* Don''t forget to Mark Answer or comment.
// Update database
SqlDataAdapter adp=new SqlDataAdapter("select * from tableName","your_conn_str");
SqlCommandBuilder cb = new SqlCommandBuilder(adp);
DataSet ds = new DataSet();
ds.ReadXml("file.xml");
DataTable dt = ds.Tables["tableName"];
dt.Rows.Add("val1", "val2");
// Adding row
adp.Update(ds, "tableName");
INSERT INTO MyTable(Data)
Values '<?xml version="1.0"?>
<company>
<companyname>
<group>Accenture BPO</group>
<group>Accenture IT </group>
<group>Accenture Infotech</group>
</companyname>
<companyname>HCL</companyname>
</company>'
因此,如果需要以编程方式保存XML
,则只需在执行INSERT
语句之前将XML
设置为字符串值.
So, if you need to save the XML
programmatically, you just need to set the XML
as a string value before executing the INSERT
statement.
这篇关于如何保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!