SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to insert xml file into sql server 2006 database structure using visual c#


how to insert xml file into sql server 2006 database structure using visual c#

Author
Message
Raymond-710502
Raymond-710502
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 0
i need help : i want to insert xml file into sql server2005 database table using c# such that i can access the file from c# console application
brandstefan
brandstefan
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
Hi, Here is an exampel.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Xml;
using System.Data;

namespace XMLImportToSQL
{
public class XMLImport
{

//Create a Connectionstring to SQL Server
// Trust connection (Server=;Database=;Trusted_Connection=TrueWink
public static string SQLConn = "Server= Your server;Database= your database;User Id= User namn;Password= Password;"

public void SaveXmlToSqlServer()
{
//Create the XML Document for tranactions
m_xmld = new XmlDocument();

//Load the Xml file
m_xmld.Load(@"C:\XmlFile.xml");

//Get the list of name nodes
XmlNodeList financialyears = m_xmld.SelectNodes("/MainNod/Nod");

//Loop through the XmlNode

foreach (XmlNode financialyear in financialyears)
{
// Get the Costcenter Attribute Value
id = financialyear.ChildNodes.Item(0).InnerText;
fromdate = financialyear.ChildNodes.Item(1).InnerText;
todate = financialyear.ChildNodes.Item(2).InnerText;

// Update if value has change else Insert new row
using (SqlConnection conn = new SqlConnection(SQLConn))
{
SqlCommand sqlCmdUpdate = new SqlCommand();
sqlCmdUpdate = new SqlCommand("Update_Insert_Financialyears", conn);
sqlCmdUpdate.CommandType = CommandType.StoredProcedure;

sqlCmdUpdate.Parameters.AddWithValue("@id", id);
sqlCmdUpdate.Parameters.AddWithValue("@fromdate", fromdate);
sqlCmdUpdate.Parameters.AddWithValue("@todate", todate);

// Check to see if the state is closed.
if (conn.State == ConnectionState.Closed)
{
// Open the connection.
conn.Open();
}
sqlCmdUpdate.ExecuteNonQuery();

// Check to see if the state is open.
if (conn.State == ConnectionState.Open)
{
// Open the connection.
conn.Close();
}
}

}
}
}
}
Hope this will help you?

Regads
Stefan Brand
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60703 Visits: 17954
Not sure they will see this. This was posted nearly 5 years ago and the poster has not logged in since then. :-D

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search