Search

Saturday, July 7, 2012

Avoid For Loop in asp.net using XML, using sp_XML_Preparedocument


Save multiple data and Avoid For Loop in asp.net  save data in XML, In stored procedure use sp_XML_Preparedocument

private SqlCommand _MyComm = new SqlCommand();
    private SqlTransaction _MyTrans;
    private string _ConnectionStr = ConfigurationManager.ConnectionStrings("Conn").ToString();


  List Parameter = new List();
        for (int i = 0; i < 10; i++)
        {
            Parameter.Add(i.ToString());
        }

        //Create XML
        StringBuilder XMLValue = new StringBuilder();
        XMLValue.Append("");

        foreach (string d in Parameter)
        {
            if (d != null)
            {
                XMLValue.Append("");
                XMLValue.Append("");
                XMLValue.Append(d);
                XMLValue.Append("");
                XMLValue.Append("");

            }
        }

        XMLValue.Append("");
        XMLValue.Replace("&", "&");

        InsertRecord(XMLValue.ToString());

 private void InsertRecord(string Parameter)
    {
        try
        {
            //Pass XML as string in Database.
            using (SqlConnection dataConnection = new SqlConnection(_ConnectionStr))
            {
                using (SqlCommand dataCommand = dataConnection.CreateCommand())
                {
                    dataConnection.Open();
                    dataCommand.CommandType = CommandType.StoredProcedure;
                    dataCommand.CommandTimeout = 3000;
                    dataCommand.CommandText = "Sp_Insert";
                    dataCommand.Parameters.Add("@xmlData", Parameter);
                    dataCommand.ExecuteNonQuery();
                    dataConnection.Close();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Stored Procedure



Create PROCEDURE [dbo].Sp_Insert
,@xmlData NVARCHAR(max) = ''
AS
BEGIN

DECLARE @resultValue VARCHAR(max),@resultId VARCHAR(max), @iTree INT
  CREATE TABLE  #TempStatus(ItemValue NVARCHAR(200))
 
                                       
--insert record in temp table \\#TempStatus

--what is sp_xml_preparedocument?
--It is a system procedure which is used to read the XML document in the memory and returns a handle to this document. After that you can use OPEN_XML method to iterate through the xml result set.
--Before OPEN_XML, one has to use sp_XML_Preparedocument.

                        EXEC sp_xml_preparedocument @iTree OUTPUT, @xmlNotificationData
                        INSERT INTO #TempStatus
                        SELECT Value FROM OPENXML(@iTree, 'InsertDataValue/DataValue',1)
                        WITH (Value varchar(200) 'Value')    
                        EXEC sp_xml_removedocument @iTree    
                     
--Now u can use temp table for insert purpose.
    --  select *from #TempStatus  
   
   insert into City (cityname) values  select  ItemValue from #TempStatus
DROP TABLE #TempStatus

END

Blog Archive

Contributors