import xml data in sql server through asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satishv96
    New Member
    • Feb 2012
    • 2

    import xml data in sql server through asp.net

    DECLARE @results table (result xml)
    DECLARE @xml XML
    SELECT @xml =CONVERT(XML, bulkcolumn, 2)
    FROM OPENROWSET(BULK 'D:\xmlfile\abc \Technicle10210 12.xml', SINGLE_BLOB)
    AS x
    select @xml



    i want to stor this xml data in our sql database
    so help me
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    If you're wanting to do this in a .NET language then take that SQL code, make a stored procedure then call it from code. Here's an example of what I'm describing:

    Code:
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE ImportXML 
    	-- Add the parameters for the stored procedure here
    	@FileContents VARCHAR(4000)
    AS
    BEGIN
    	SET NOCOUNT ON;
    	DECLARE @results table (result xml)
    	DECLARE @xml XML
    	SELECT @xml =CONVERT(XML, bulkcolumn, 2)
    	FROM OPENROWSET(BULK @FileContents, SINGLE_BLOB) 
    	AS x
    	select @xml
    END
    GO
    And the code for executing it

    Code:
    public void ImportSql()
    {
    	SqlConnection conn = new SqlConnection("YourConnectionString");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "ImportXML";
        cmd.CommandType = CommandType.StoredProcedure;
    
        cmd.Parameters.AddWithValue("@FileContents", "D:\xmlfile\abc\Technicle1021012.xml");
        cmd.ExecuteNonQuery();
    }
    NOTE: There may be some changes that need to be made, I wrote this example in Notepad but it should get you started down the right path.

    Comment

    Working...