<?xml version="1.0" encoding="utf-8" ?>
<users>
<user>
<FirstName>Suresh</FirstName>
<LastName>Dasari</LastName>
<UserName>SureshDasari</UserName>
<Job>Team Leader</Job>
</user>
<user>
<FirstName>Mahesh</FirstName>
<LastName>Dasari</LastName>
<UserName>MaheshDasari</UserName>
<Job>Software Developer</Job>
</user>
<user>
<FirstName>Madhav</FirstName>
<LastName>Yemineni</LastName>
<UserName>MadhavYemineni</UserName>
<Job>Business Analyst</Job>
</user>
</users>
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Send xml file as a parameter to SQL stored procedure in
asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Button ID="btnSend"
Text="Send XML
to Database" runat="server" onclick="btnSend_Click" /><br /><br />
<b>Inserted Records Details</b> :
<asp:GridView ID="gvDetails"
runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
|
protected void
btnSend_Click(object sender, EventArgs e)
{
XmlTextReader xmlreader = new
XmlTextReader(Server.MapPath("Sample.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count != 0)
{
using (SqlConnection
con = new SqlConnection("Data Source=SureshDasari;Integrated
Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("prc_readxmldata",
con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@XMLdata",
SqlDbType.Xml).Value = ds.GetXml();
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds1=new DataSet();
da.Fill(ds1);
gvDetails.DataSource = ds1;
gvDetails.DataBind();
con.Close();
}
}
}
|
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Partial Class
Default2
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs)
End Sub
Protected Sub
btnSend_Click(ByVal sender As Object, ByVal e As
EventArgs)
Dim xmlreader As New XmlTextReader(Server.MapPath("Sample.xml"))
Dim ds As New DataSet()
ds.ReadXml(xmlreader)
xmlreader.Close()
If ds.Tables.Count <> 0 Then
Using con As New SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("prc_readxmldata",
con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@XMLdata",
SqlDbType.Xml).Value = ds.GetXml()
Dim da As New SqlDataAdapter(cmd)
Dim ds1 As New DataSet()
da.Fill(ds1)
gvDetails.DataSource = ds1
gvDetails.DataBind()
con.Close()
End Using
End If
End Sub
End Class
|
|
CREATE PROCEDURE
prc_readxmldata
(
@XMLdata XML
)
AS
BEGIN
SELECT
t.value('(FirstName/text())[1]','nvarchar(120)')AS FirstName ,
t.value('(LastName/text())[1]','nvarchar(120)')AS LastName,
t.value('(UserName/text())[1]','nvarchar(120)')AS UserName,
t.value('(Job/text())[1]','nvarchar(120)')AS Job
FROM
@XMLdata.nodes('/users/user')AS TempTable(t)
END
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
11 comments :
how to insert data into sqlserver table using xml file
good work good code thank you sharing
what's the sql server version have used to create the stored procedure ?
Nice One Suresh Garu :-) :->
how can i send more than one sms at time..can u give us query regarding this....
Cannot generate SSPI context.
give me solution of this error
thank you for posting this..it was very usefull
i have one doubt?
i have to create web application in that select xml file as dynamic, aftr that store that entire file in database table as a string,at last store row and columns with respective , and space separeted values? how?
plz help me.................
SIR WORKING SMOTHLY GOOD ARTICLE
Thank U...
Very nice article but how can i insert xml data into sqlsever table