如何使用XML按列将Xml文件插入数据库并与gridview绑定 [英] How To insert Xml file into database with xml columnwise and bind with gridview
问题描述
先生您好,
我必须上传Xml文件并使用Xml列保存到数据库中,并最终使用Gridview进行绑定...
我已经编写了用于上传的代码:
Hello sir,
I have to upload the Xml file and save into database with Xml columns and finnaly bind with Gridview ...
I have written the code for uploading:
string xmlfile = "";
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(MapPath("/XMLFILE/" + FileUpload1.FileName));
xmlfile = FileUpload1.FileName;
}
cmd = "Insert Into FileTable (fil)values(''" + xmlfile + "'')";
SqlConnection cn1 = new SqlConnection(cn);
SqlCommand cmd1 = new SqlCommand(cmd, cn1);
cn1.Open();
cmd1.ExecuteNonQuery();
cn1.Close();
这段代码插入字符串值,但是我想按列插入...
而且我必须将这些数据与Gridview绑定
我必须插入这种类型的数据.....
This code insert the string value but i want column wise insertion...
and that data i have to bind with Gridview
I have to insert this type of data .....
<?xml version="1.0" standalone="yes" ?>
- <DocumentElement>
- <Sample>
<brand>Pyramid</brand>
<sku>PA105</sku>
<weight>10.01</weight>
<stock>375</stock>
</Sample>
</DocumentElement>
推荐答案
请参考以下代码...
Hi,
Refer the following code...
DECLARE @sXML XML
SET @sXML =''<?xml version="1.0"?>
<DocumentElement>
<Sample>
<brand>Pyramid</brand>
<sku>PA105</sku>
<weight>10.01</weight>
<stock>375</stock>
</Sample>
</DocumentElement>''
SELECT
tab.col.value( ''brand[1]'', ''NVARCHAR(50)''),
tab.col.value( ''sku[1]'', ''NVARCHAR(50)''),
tab.col.value( ''weight[1]'', ''NUMERIC(18,2)''),
tab.col.value( ''stock[1]'', ''INT'')
FROM @xml.nodes(''/DocumentElement/Sample'') tab(col)
在您的过程中使用此代码,只需创建一个临时表,即可将XML数据添加到您的过程中的该临时表中,然后将其用于您的操作.
干杯:)
Using this code in your procedure,Just create one temp table add the XML data into that temp table in your procedure then use that one for your operation.
Cheers:)
您可以将整个xml读取为xml字符串,并可以从该xml字符串创建DataSet/DataTable.
选中此 ^ ]
You can read whole xml into a xml string and you can create a DataSet/DataTable from that xml string.
Check this Google[^]
这篇关于如何使用XML按列将Xml文件插入数据库并与gridview绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!