Skip to main content

XML vs User defined table type

Suppose , I have to insert multiple records at a single click. The option for a developer to insert the data in database with the help of 2 options.
1. With XML
2. With User defined table type.

Here, we are defining both these options one by one.

1.With XML

We have a grid with 3 columns boxid, quantity and weight and we have picked the data from the gridview and store these data in 3 different array objects .suppose in the below example quantity is a string has value “200,300,140”.we fetch the values from string to array.
string[] arrrQ = quantity.Split(','); //fetch the value from string to array
string[] arrW = weight.Split(',');
string[] arrB = boxtypeId.Split(',');


XmlDocument oXmlProductDetail = new XmlDocument(); // create an object of XmlDocument class
XmlElement oXmlElement = oXmlProductDetail.CreateElement("doc");// create an element with specified name

for (int i = 0; i < arrrQ.Length; i++)
{
XmlElement oXmlIM = oXmlProductDetail.CreateElement("Record");//create another element
oXmlIM.SetAttribute("boxid", arrB[i]);
oXmlIM.SetAttribute("quantity", arrrQ[i]);
oXmlIM.SetAttribute("weight", arrW[i]);
oXmlElement.AppendChild(oXmlIM); // add the element “Record” under the main element “doc”
}
oXmlProductDetail.AppendChild(oXmlElement); //Finally add the element in the xmldocument object.

---------------//pass the xml paramenter to the method as under
int intResult = objname.XYZ (oXmlProductDetail);



//Now write a method in a class to pass the xml to the specified stored procedure. Create a argument of type XMLDocument type
public static int XYZ(XmlDocument data)
{
objDic.Add("@paramenter1 ", data.OuterXml);
}



Stotred procedure


CREATE PROCEDURE [dbo].[procedure_name]
(
@paramenter1 xml
)
AS
BEGIN
-- POPULATING TEMP TABLE FROM XML VARIABLE
INSERT INTO @tbl (boxid,quantity,[weight])
select x.value(N'@boxid', N'int') AS boxid
, x.value(N'@quantity', N'int') AS quantity
, x.value(N'@weight', N'decimal(10,2)') AS [weight]
FROM @ProductDetail.nodes(N'/doc/Record') t(x)



===========================
2.With Table Type
===========================
1.First create user defined table type

CREATE TYPE [dbo].[ tabletype_name] AS TABLE(
[col_tt1] [int] NULL,
[col_tt2] [int] NULL
)


2. create a stored procedure and used the user defined table type.

CREATE PROCEDURE [dbo].[procedure_name]
(
@paramenter tabletype_name READONLY
)
AS
BEGIN
INSERT INTO table1([col1],[col2)
SELECT [col_tt1],[col_tt2],'A' FROM @paramenter
END
Coding end:
3. passing the datatable to the above parameter.
public static void MethodName(DataTable dt)
{
objDic.Add("@RegionDetail", dt);
}

Comments

Popular posts from this blog

URL Rewritting

http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/ http://msdn.microsoft.com/en-us/library/ms972974.aspx URL Rewriting in ASP.NET Summary: Examines how to perform dynamic URL rewriting with Microsoft ASP.NET. URL rewriting is the process of intercepting an incoming Web request and automatically redirecting it to a different URL. Discusses the various techniques for implementing URL rewriting, and examines real-world scenarios of URL rewriting. (31 printed pages) Download the source code for this article. Contents Introduction Common Uses of URL Rewriting What Happens When a Request Reaches IIS Implementing URL Rewriting Building a URL Rewriting Engine Performing Simple URL Rewriting with the URL Rewriting Engine Creating Truly "Hackable" URLs Conclusion Related Books Introduction Take a moment to look at some of the URLs on your website. Do you find URLs like http://yoursite.com/info/dispEmployeeInfo.aspx?EmpID=459-099&type=summ...

SEND A PDF FILE AS AN ATTACHEMENT OF MAIL!

System.Net.Mail.MailMessage m1 = new System.Net.Mail.MailMessage(); m1.From = "manpreet@gmail.com" m1.Subject = "Test mail "; m1.Body = str.ToString(); m1.IsBodyHtml = true; m1.To.Add("jasdeep@gmail.com"); m1.CC.Add("jasdeep123@gmail.com"); m1.Attachments.Add(new Attachment(strserverpath + @"\pdf\" + PdfFileName)); smtp.Send(m1);

Sql Server Tips

IF WE WANT TO SELECT  TOP N ROWS  FROM A TABLE ,  WE USE THE FETCH NEXT  SELECT    * FROM  table1   ---//First m rows ignore first n rows ORDER BY id OFFSET n ROWS FETCH NEXT m ROWS ONLY if n=0, m=10 then first  10 rows if n=10, m=10 then first  10 rows start from 11th row Row_number()   SELECT  ROW_NUMBER() OVER(order by db.id) Insertion Simultaneouly in temp table with output clause DECLARE @table1 table (   Id int,   name nvarchar(50) ); DECLARE @table2 table (  Id int,  name nvarchar(50) ); INSERT INTO @table2 OUTPUT INSERTED.*   INTO @table1   select top 10 id, name  from FinalTable SELECT * FROM @table2; SELECT * FROM @table1;