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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 29, 2011, 11:13 PM EST
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
Datta KharadRank #2,135 · 668 reputationPosted Nov 29, 2011, 9:53 PM EST
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();
}
}
Rajkumar R175 reputationPosted Nov 29, 2011, 11:40 PM EST
Rajkumar R175 reputationPosted Nov 30, 2011, 12:26 AM EST
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 R175 reputationPosted Nov 30, 2011, 12:48 AM EST
Rajkumar R175 reputationPosted Nov 30, 2011, 12:53 AM EST