any one post solution for this
I have an XML file like this below i mentioned
-
-
-
-
-
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Nov 30, 2011, 4:13 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("test.xml"));
SqlBulkCopy sbc = new SqlBulkCopy(con);
sbc.DestinationTableName ="employee";
sbc.ColumnMappings.Add("TALLY_REC_ID_CODE", "empid");
sbc.ColumnMappings.Add("CLUSTER_ID", "empname");
sbc.ColumnMappings.Add("UNIT_TYPE", "empaddress");
sbc.ColumnMappings.Add("VPRC_ID", "empcity");
sbc.ColumnMappings.Add("SHG_ID", "empstate");
sbc.ColumnMappings.Add("PIP_NO", "empzip");
sbc.ColumnMappings.Add("NAME_OF_THE_MEMBER", "emplic");
sbc.ColumnMappings.Add("TYPE_OF_LOAN", "empstatus");
sbc.ColumnMappings.Add("PRINCIPAL", "empp");
sbc.ColumnMappings.Add("INTEREST", "empi");
con.Open();
sbc.WriteToServer(reportData.Tables[0]);
con.Close();
Label1.Text = "Records inserted successfully";
}
}
Thanks
Rajkumar RPosted Nov 30, 2011, 5:53 AM
Rajkumar RPosted Nov 30, 2011, 5:48 AM
Rajkumar RPosted Nov 30, 2011, 5:26 AM
i want to browse the file and to upload that how can select the file using fileupload control and to upload that xml file in sqlserver
Rajkumar RPosted Nov 30, 2011, 4:40 AM
Datta KharadPosted Nov 30, 2011, 2:53 AM
You can try this code..
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();
}
}