<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6721503270778376897</id><updated>2011-07-30T18:02:22.331-07:00</updated><category term='SqL Connection'/><category term='Sql Helper Class'/><title type='text'>Code Help</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://uroojs.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://uroojs.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Urooj Mahmood</name><uri>http://www.blogger.com/profile/15713414569362527460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6721503270778376897.post-2020597106663033997</id><published>2009-11-24T10:36:00.000-08:00</published><updated>2009-11-24T10:54:57.855-08:00</updated><title type='text'>test</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?xml:namespace prefix = asp /&gt;&lt;asp:image id="Image3" runat="server" imageurl="'&lt;%#"&gt;&lt;br /&gt;&lt;asp:image id="Image3" imageurl =" '&lt;%#"&gt;' runat = "server" /&gt;&lt;/td&gt;&lt;br /&gt;&lt;td style="padding-left: 10px"&gt;&lt;br /&gt;&lt;a href="'Subcat.aspx?targetid="&lt;%#"&gt;&amp;amp;name=&lt;%# DataBinder.Eval(Container.DataItem,"CatName")%&gt;'&gt;&lt;br /&gt;&lt;asp:label id="Label1" cssclass="webmenu" runat="server" text="'&lt;%#"&gt;'&gt;&lt;/asp:Label&gt;&lt;br /&gt;&lt;/a&gt;&lt;/asp:image&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;// page load&lt;br /&gt;intPageSize.Text = "6";&lt;br /&gt;lblIntCurrentIndex.Text = "0";&lt;br /&gt;///&lt;br /&gt;intRoundCount.Text = Convert.ToString(ds.Tables[0].Rows.Count);&lt;br /&gt;&lt;br /&gt;if (ds.Tables[0].Rows.Count &gt; 0)&lt;br /&gt;{&lt;br /&gt;lblFirst.Visible = true;&lt;br /&gt;lblPervious.Visible = true;&lt;br /&gt;lblNext.Visible = true;&lt;br /&gt;lblLast.Visible = true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public void ShowFirst(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;lblIntCurrentIndex.Text = "0";&lt;br /&gt;DataBind();&lt;br /&gt;}&lt;br /&gt;public void ShowPerivous(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;lblIntCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblIntCurrentIndex.Text) - Convert.ToInt32(intPageSize.Text));&lt;br /&gt;if (Convert.ToInt32(lblIntCurrentIndex.Text) &lt; text = "0" text =" Convert.ToString(Convert.ToInt32(lblIntCurrentIndex.Text)" i =" Convert.ToInt32(intRoundCount.Text)"&gt; 0)&lt;br /&gt;{&lt;br /&gt;lblIntCurrentIndex.Text = Convert.ToString(Convert.ToInt32(intRoundCount.Text) - i);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;lblIntCurrentIndex.Text = Convert.ToString(Convert.ToInt32(intPageSize.Text) - i);&lt;br /&gt;}&lt;br /&gt;DataBind();&lt;br /&gt;}&lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;br /&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;if (Session["userLoginID"] != null)&lt;br /&gt;{&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;uid = dt.CInt(Session["userLoginID"]);&lt;br /&gt;utype = dt.CInt(Session["userTypeID"]);&lt;br /&gt;ulocid = dt.CInt(Session["userLocationID"]);&lt;br /&gt;}&lt;br /&gt;catch { }&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;Response.Redirect("Default.aspx");&lt;br /&gt;}&lt;br /&gt;if (!Page.IsPostBack)&lt;br /&gt;{&lt;br /&gt;binddl();&lt;br /&gt;if (Request["id"] != null)&lt;br /&gt;{&lt;br /&gt;getproduct(Convert.ToInt32(Request["id"].ToString()));&lt;br /&gt;Button1.Text = "Update";&lt;br /&gt;Session.Add("PID", Request["id"].ToString());&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;lblmsg.Text = "";&lt;br /&gt;trreason.Visible = false;&lt;br /&gt;Button1.Text = "Create";&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public void binddl()&lt;br /&gt;{&lt;br /&gt;DataSet ds = new DataSet();&lt;br /&gt;DataView dvloc;&lt;br /&gt;string strloc = "";&lt;br /&gt;ds = aobj.getddls();&lt;br /&gt;ddlscat.DataSource = ds.Tables[11];&lt;br /&gt;ddlscat.DataTextField = "prodSubCatName";&lt;br /&gt;ddlscat.DataValueField = "prodSubCatID";&lt;br /&gt;ddlscat.DataBind();&lt;br /&gt;ddlscat2.DataSource = ds.Tables[8];&lt;br /&gt;ddlscat2.DataTextField = "prodSubCatName";&lt;br /&gt;ddlscat2.DataValueField = "prodSubCatID";&lt;br /&gt;ddlscat2.DataBind();&lt;br /&gt;//dvloc = new DataView(ds.Tables[10]);&lt;br /&gt;dvloc = new DataView(ds.Tables[3]);&lt;br /&gt;if (utype == 3  utype == 4)&lt;br /&gt;{&lt;br /&gt;strloc = " retLocID = -1 OR retLocID = 0 OR retLocID = " + ulocid ;&lt;br /&gt;dvloc.RowFilter = strloc ;&lt;br /&gt;}&lt;br /&gt;ddlloc.DataSource = dvloc;&lt;br /&gt;ddlloc.DataTextField = "retLocLocationName";&lt;br /&gt;ddlloc.DataValueField = "retLocID";&lt;br /&gt;ddlloc.DataBind();&lt;br /&gt;ddlloc.Items.Insert(0, new ListItem("All Location", "0"));&lt;br /&gt;ddlloc.SelectedValue = "0";&lt;br /&gt;if (Session["searchPName"] != null)&lt;br /&gt;txtsearch.Text = Session["searchPName"].ToString();&lt;br /&gt;if (Session["searchPLocation"] != null)&lt;br /&gt;ddlloc.SelectedValue = Session["searchPLocation"].ToString();&lt;br /&gt;if (Session["searchPSubCategory"] != null)&lt;br /&gt;ddlscat2.SelectedValue = Session["searchPSubCategory"].ToString();&lt;br /&gt;fillGrid();&lt;br /&gt;}&lt;br /&gt;public void insertproduct()&lt;br /&gt;{&lt;br /&gt;pobj.prodCodeType = dt.CInt(rbl1.SelectedItem.Value);&lt;br /&gt;pobj.prodAltCodeType = dt.CInt(rbl2.SelectedItem.Value);&lt;br /&gt;pobj.prodCreatedByID = uid;&lt;br /&gt;//pobj.prodDescription = txtdesc.Text;&lt;br /&gt;pobj.prodModifyByID = uid;&lt;br /&gt;pobj.prodName = txtname.Text;&lt;br /&gt;if (pobj.prodAltCodeType == 1)&lt;br /&gt;pobj.prodCode = txtcode.Text;&lt;br /&gt;else&lt;br /&gt;pobj.prodCode = "";&lt;br /&gt;if(ddlstatus.SelectedValue == "1")&lt;br /&gt;{&lt;br /&gt;pobj.prodStatus = true;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;pobj.prodStatus = false;&lt;br /&gt;}&lt;br /&gt;pobj.prodUPC = txtpcode.Text;&lt;br /&gt;pobj.prodSubCatID =dt.CInt( ddlscat.SelectedValue);&lt;br /&gt;if (utype == 3  utype == 4)&lt;br /&gt;{&lt;br /&gt;pobj.prodLocId = ulocid ;&lt;br /&gt;}&lt;br /&gt;if (utype == 1)&lt;br /&gt;{&lt;br /&gt;pobj.prodLocId = -1;&lt;br /&gt;}&lt;br /&gt;if (utype == 2)&lt;br /&gt;{&lt;br /&gt;pobj.prodLocId = 0 ;&lt;br /&gt;}&lt;br /&gt;int result = 0;&lt;br /&gt;result = prodobj.InsertProduct(pobj);&lt;br /&gt;if (result &gt; 0)&lt;br /&gt;{&lt;br /&gt;lblmsg.Text = "Product added successfully";&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;lblmsg.Text = "Product could not be added";&lt;br /&gt;}&lt;br /&gt;fillGrid();&lt;br /&gt;ResetControls();&lt;br /&gt;}&lt;br /&gt;protected void Button1_Click(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;if (Session["PID"] != null)&lt;br /&gt;{&lt;br /&gt;//trreason.Visible = true;&lt;br /&gt;Button1.Text = "Update";&lt;br /&gt;Editproduct();&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;//trreason.Visible = false;&lt;br /&gt;Button1.Text = "Create";&lt;br /&gt;insertproduct();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public void Editproduct()&lt;br /&gt;{&lt;br /&gt;pobj.prodID = dt.CInt(Session["PID"]);&lt;br /&gt;pobj.prodCodeType = dt.CInt(rbl1.SelectedItem.Value);&lt;br /&gt;pobj.prodAltCodeType = dt.CInt(rbl2.SelectedItem.Value);&lt;br /&gt;pobj.prodCreatedByID = 1;&lt;br /&gt;//pobj.prodDescription = txtdesc.Text;&lt;br /&gt;pobj.prodModifyByID = uid;&lt;br /&gt;pobj.prodName = txtname.Text;&lt;br /&gt;if (pobj.prodAltCodeType == 1)&lt;br /&gt;pobj.prodCode = txtcode.Text;&lt;br /&gt;else&lt;br /&gt;pobj.prodCode = "";&lt;br /&gt;pobj.prodUpdateReason = txtreason.Text;&lt;br /&gt;if(ddlstatus.SelectedValue == "1")&lt;br /&gt;{&lt;br /&gt;pobj.prodStatus = true;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;pobj.prodStatus = false;&lt;br /&gt;}&lt;br /&gt;pobj.prodUPC = txtpcode.Text;&lt;br /&gt;pobj.prodSubCatID =dt.CInt( ddlscat.SelectedValue);&lt;br /&gt;pobj.prodLocId = Convert.ToInt32(txtProductLocID.Text.ToString());&lt;br /&gt;int result = 0;&lt;br /&gt;result = prodobj.ReturnUPdateProduct(pobj);&lt;br /&gt;if (result &gt; 0)&lt;br /&gt;{&lt;br /&gt;lblmsg.Text = "Product updated successfully";&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;lblmsg.Text = "Product could not be updated";&lt;br /&gt;}&lt;br /&gt;fillGrid();&lt;br /&gt;}&lt;br /&gt;public void getproduct(int pid)&lt;br /&gt;{&lt;br /&gt;DataSet dsprod = new DataSet();&lt;br /&gt;dsprod = ppobj.GetProdByID(pid);&lt;br /&gt;if (utype == 3  utype == 4)&lt;br /&gt;{&lt;br /&gt;if (ulocid == dt.CInt(dsprod.Tables[0].Rows[0]["prodLocId"].ToString()))&lt;br /&gt;{&lt;br /&gt;Button1.Visible = true;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;Button1.Visible = false;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;for (int j = 0; j &lt; rbl1.Items.Count; j++)&lt;br /&gt;{&lt;br /&gt;if (rbl1.Items[j].Value == dsprod.Tables[0].Rows[0]["prodCodeType"].ToString())&lt;br /&gt;{&lt;br /&gt;rbl1.Items[j].Selected = true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;txtpcode.Text =dsprod.Tables[0].Rows[0]["prodUPC"].ToString();&lt;br /&gt;txtcode.Text = dsprod.Tables[0].Rows[0]["prodCode"].ToString();&lt;br /&gt;//txtdesc.Text = dsprod.Tables[0].Rows[0]["prodDescription"].ToString();&lt;br /&gt;txtname.Text = dsprod.Tables[0].Rows[0]["prodName"].ToString();&lt;br /&gt;ddlscat.SelectedValue = dsprod.Tables[0].Rows[0]["prodSubCatID"].ToString();&lt;br /&gt;txtreason.Text = dsprod.Tables[0].Rows[0]["prodUpdateReason"].ToString();&lt;br /&gt;if (dsprod.Tables[0].Rows[0]["prodAltCodeType"].ToString() == "2")&lt;br /&gt;rbl2.SelectedValue = "2";&lt;br /&gt;else&lt;br /&gt;rbl2.SelectedValue = "1";&lt;br /&gt;if (dsprod.Tables[0].Rows[0]["prodStatus"].ToString() == "True")&lt;br /&gt;{&lt;br /&gt;ddlstatus.SelectedValue = "1";&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;ddlstatus.SelectedValue = "0";&lt;br /&gt;}&lt;br /&gt;txtProductLocID.Text = dsprod.Tables[0].Rows[0]["prodLocID"].ToString();&lt;br /&gt;}&lt;br /&gt;public void ResetControls()&lt;br /&gt;{&lt;br /&gt;//lblmsg.Text = "";&lt;br /&gt;txtcode.Text = "";&lt;br /&gt;//txtdesc.Text = "";&lt;br /&gt;txtname.Text = "";&lt;br /&gt;ddlscat.SelectedValue = "-1";&lt;br /&gt;ddlstatus.SelectedValue = "1";&lt;br /&gt;txtreason.Text = "";&lt;br /&gt;txtpcode.Text = "";&lt;br /&gt;//ddlloc.SelectedValue = "-1";&lt;br /&gt;}&lt;br /&gt;protected void LinkButton1_Click(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;trreason.Visible = false;&lt;br /&gt;ResetControls();&lt;br /&gt;Session["PID"] = null;&lt;br /&gt;Button1.Text = "Create";&lt;br /&gt;}&lt;br /&gt;protected void btnsearch_Click(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;//ResetControls();&lt;br /&gt;lblsearch.Visible = true;&lt;br /&gt;lblsearch.Text = "";&lt;br /&gt;Session["searchPCurrentPageIndex"] = "0";&lt;br /&gt;Session["searchPName"] = txtsearch.Text.ToString();&lt;br /&gt;Session["searchPLocation"] = ddlloc.SelectedValue.ToString();&lt;br /&gt;Session["searchPSubCategory"] = ddlscat2.SelectedValue.ToString();&lt;br /&gt;fillGrid();&lt;br /&gt;}&lt;br /&gt;protected void ddlscat2_SelectedIndexChanged(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;Session["searchPCurrentPageIndex"] = "0";&lt;br /&gt;Session["searchPName"] = txtsearch.Text.ToString();&lt;br /&gt;Session["searchPLocation"] = ddlloc.SelectedValue.ToString();&lt;br /&gt;Session["searchPSubCategory"] = ddlscat2.SelectedValue.ToString();&lt;br /&gt;fillGrid();&lt;br /&gt;}&lt;br /&gt;protected void ddlloc_SelectedIndexChanged(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;Session["searchPCurrentPageIndex"] = "0";&lt;br /&gt;Session["searchPName"] = txtsearch.Text.ToString();&lt;br /&gt;Session["searchPLocation"] = ddlloc.SelectedValue.ToString();&lt;br /&gt;Session["searchPSubCategory"] = ddlscat2.SelectedValue.ToString();&lt;br /&gt;fillGrid();&lt;br /&gt;}&lt;br /&gt;protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)&lt;br /&gt;{&lt;br /&gt;GridView1.PageIndex = e.NewPageIndex;&lt;br /&gt;Session["searchPCurrentPageIndex"] = e.NewPageIndex.ToString();&lt;br /&gt;fillGrid();&lt;br /&gt;}&lt;br /&gt;public void fillGrid()&lt;br /&gt;{&lt;br /&gt;DataSet dsloc = new DataSet();&lt;br /&gt;DataView dv;&lt;br /&gt;string str = "";&lt;br /&gt;dsloc = aobj.getddls();&lt;br /&gt;dv = new DataView(dsloc.Tables[9]);&lt;br /&gt;// Location Filter&lt;br /&gt;if (ddlloc.SelectedValue.ToString() == "0")&lt;br /&gt;str = " (prodLocId = -1 or prodLocId = 0 or prodLocId = " + ulocid + ")" ;&lt;br /&gt;else&lt;br /&gt;str = " (prodLocId = " + ddlloc.SelectedValue.ToString() + ")" ;&lt;br /&gt;//Sub Category Filter&lt;br /&gt;if (ddlscat2.SelectedValue.ToString() != "-1")&lt;br /&gt;{&lt;br /&gt;if (str == "")&lt;br /&gt;str = " (prodSubCatID = " + dt.CInt(ddlscat2.SelectedValue) + ")" ;&lt;br /&gt;else&lt;br /&gt;str = str + " AND (prodSubCatID = " + dt.CInt(ddlscat2.SelectedValue) + ")";&lt;br /&gt;}&lt;br /&gt;// Search Text Filter&lt;br /&gt;if (txtsearch.Text.ToString() != "")&lt;br /&gt;{&lt;br /&gt;if (str == "")&lt;br /&gt;str = " (prodName LIKE '" + txtsearch.Text + "%' or prodUPC LIKE '%" + txtsearch.Text + "%')";&lt;br /&gt;else&lt;br /&gt;str = str + " AND (prodName LIKE '" + txtsearch.Text + "%' or prodUPC LIKE '%" + txtsearch.Text + "%')";&lt;br /&gt;}&lt;br /&gt;dv.RowFilter = str;&lt;br /&gt;GridView1.DataSource = dv;&lt;br /&gt;if (Session["searchPCurrentPageIndex"] != null)&lt;br /&gt;GridView1.PageIndex = Convert.ToInt32(Session["searchPCurrentPageIndex"].ToString());&lt;br /&gt;GridView1.DataBind();&lt;br /&gt;Session["searchPName"] = txtsearch.Text.ToString();&lt;br /&gt;Session["searchPLocation"] = ddlloc.SelectedValue.ToString();&lt;br /&gt;Session["searchPSubCategory"] = ddlscat2.SelectedValue.ToString();&lt;br /&gt;Int32 iPageSize = 0, iPageRows = 0, iTotalPages = 0, iCurrentPage = 0, iTotalRows = 0;&lt;br /&gt;iPageSize = Convert.ToInt32(GridView1.PageSize.ToString());&lt;br /&gt;iTotalRows = Convert.ToInt32(dv.Count.ToString());&lt;br /&gt;iCurrentPage = Convert.ToInt32(GridView1.PageIndex.ToString()) + 1;&lt;br /&gt;if ((iTotalRows % iPageSize) &gt; 0)&lt;br /&gt;iTotalPages = Convert.ToInt32((iTotalRows / iPageSize)) + 1;&lt;br /&gt;else&lt;br /&gt;iTotalPages = Convert.ToInt32((iTotalRows / iPageSize));&lt;br /&gt;if (iCurrentPage &lt; iTotalPages)&lt;br /&gt;iPageRows = iPageSize;&lt;br /&gt;else&lt;br /&gt;iPageRows = (iTotalRows % iPageSize);&lt;br /&gt;lblNoOfProducts.Text = dv.Count.ToString();&lt;br /&gt;lblcurrentPage.Text = iCurrentPage.ToString();&lt;br /&gt;lblTotalPage.Text = iTotalPages.ToString();&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6721503270778376897-2020597106663033997?l=uroojs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uroojs.blogspot.com/feeds/2020597106663033997/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://uroojs.blogspot.com/2009/11/test.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/2020597106663033997'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/2020597106663033997'/><link rel='alternate' type='text/html' href='http://uroojs.blogspot.com/2009/11/test.html' title='test'/><author><name>Urooj Mahmood</name><uri>http://www.blogger.com/profile/15713414569362527460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6721503270778376897.post-8253478755639504006</id><published>2009-11-24T10:24:00.000-08:00</published><updated>2009-11-24T11:00:51.696-08:00</updated><title type='text'>Data List Paging help</title><content type='html'>&lt;a href="http://www.asp.net/Learn/data-access/tutorial-44-cs.aspx"&gt;http://www.asp.net/Learn/data-access/tutorial-44-cs.aspx&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.dotnetspider.com/resources/22325-Paging-Data-list.aspx"&gt;http://www.dotnetspider.com/resources/22325-Paging-Data-list.aspx&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.codeproject.com/KB/aspnet/DataListPaging.aspx"&gt;http://www.codeproject.com/KB/aspnet/DataListPaging.aspx&lt;/a&gt;&lt;br /&gt;&lt;a href="http://aspalliance.com/157_Paging_in_DataList"&gt;http://aspalliance.com/157_Paging_in_DataList&lt;/a&gt;&lt;br /&gt;&lt;a href="http://69.10.233.10/KB/aspnet/ControlPaging.aspx"&gt;http://69.10.233.10/KB/aspnet/ControlPaging.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.csharpcorner.com/UploadFile/rizwan328/DataListCustomPaging01112009021450AM/DataListCustomPaging.aspx"&gt;http://www.csharpcorner.com/UploadFile/rizwan328/DataListCustomPaging01112009021450AM/DataListCustomPaging.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetjohn.com/articles.aspx?articleid=48"&gt;http://www.dotnetjohn.com/articles.aspx?articleid=48&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6721503270778376897-8253478755639504006?l=uroojs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uroojs.blogspot.com/feeds/8253478755639504006/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://uroojs.blogspot.com/2009/11/data-list-paging-help.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/8253478755639504006'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/8253478755639504006'/><link rel='alternate' type='text/html' href='http://uroojs.blogspot.com/2009/11/data-list-paging-help.html' title='Data List Paging help'/><author><name>Urooj Mahmood</name><uri>http://www.blogger.com/profile/15713414569362527460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6721503270778376897.post-2587860752149677697</id><published>2009-11-05T08:26:00.000-08:00</published><updated>2009-11-05T08:29:31.040-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Sql Helper Class'/><title type='text'>Sql Helper</title><content type='html'>using System;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Xml;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;using System.Collections;&lt;br /&gt;namespace Microsoft.ApplicationBlocks.Data&lt;br /&gt;{&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// The SqlHelper class is intended to encapsulate high performance, scalable best practices for&lt;br /&gt;/// common uses of SqlClient&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public sealed class SqlHelper&lt;br /&gt;{&lt;br /&gt;#region private utility methods &amp;amp; constructors&lt;br /&gt;// Since this class provides only static methods, make the default constructor private to prevent&lt;br /&gt;// instances from being created with "new SqlHelper()"&lt;br /&gt;private SqlHelper() {}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// This method is used to attach array of SqlParameters to a SqlCommand.&lt;br /&gt;///&lt;br /&gt;/// This method will assign a value of DbNull to any parameter with a direction of&lt;br /&gt;/// InputOutput and a value of null.&lt;br /&gt;///&lt;br /&gt;/// This behavior will prevent default values from being used, but&lt;br /&gt;/// this will be the less common case than an intended pure output parameter (derived as InputOutput)&lt;br /&gt;/// where the user provided no input value.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="command"&gt;The command to which the parameters will be added&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParameters to be added to command&lt;/param&gt;&lt;br /&gt;private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( command == null ) throw new ArgumentNullException( "command" );&lt;br /&gt;if( commandParameters != null )&lt;br /&gt;{&lt;br /&gt;foreach (SqlParameter p in commandParameters)&lt;br /&gt;{&lt;br /&gt;if( p != null )&lt;br /&gt;{&lt;br /&gt;// Check for derived output value with no value assigned&lt;br /&gt;if ( ( p.Direction == ParameterDirection.InputOutput &lt;br /&gt;p.Direction == ParameterDirection.Input ) &amp;amp;&amp;amp;&lt;br /&gt;(p.Value == null))&lt;br /&gt;{&lt;br /&gt;p.Value = DBNull.Value;&lt;br /&gt;}&lt;br /&gt;command.Parameters.Add(p);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// This method assigns dataRow column values to an array of SqlParameters&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;Array of SqlParameters to be assigned values&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values&lt;/param&gt;&lt;br /&gt;private static void AssignParameterValues(SqlParameter[] commandParameters, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if ((commandParameters == null)  (dataRow == null))&lt;br /&gt;{&lt;br /&gt;// Do nothing if we get no data&lt;br /&gt;return;&lt;br /&gt;}&lt;br /&gt;int i = 0;&lt;br /&gt;// Set the parameters values&lt;br /&gt;foreach(SqlParameter commandParameter in commandParameters)&lt;br /&gt;{&lt;br /&gt;// Check the parameter name&lt;br /&gt;if( commandParameter.ParameterName == null &lt;br /&gt;commandParameter.ParameterName.Length &lt;= 1 )&lt;br /&gt;throw new Exception(&lt;br /&gt;string.Format(&lt;br /&gt;"Please provide a valid parameter name on the parameter #{0}, the ParameterName property has the following value: '{1}'.",&lt;br /&gt;i, commandParameter.ParameterName ) );&lt;br /&gt;if (dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) != -1)&lt;br /&gt;commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)];&lt;br /&gt;i++;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// This method assigns an array of values to an array of SqlParameters&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;Array of SqlParameters to be assigned values&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;Array of objects holding the values to be assigned&lt;/param&gt;&lt;br /&gt;private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if ((commandParameters == null)  (parameterValues == null))&lt;br /&gt;{&lt;br /&gt;// Do nothing if we get no data&lt;br /&gt;return;&lt;br /&gt;}&lt;br /&gt;// We must have the same number of values as we pave parameters to put them in&lt;br /&gt;if (commandParameters.Length != parameterValues.Length)&lt;br /&gt;{&lt;br /&gt;throw new ArgumentException("Parameter count does not match Parameter Value count.");&lt;br /&gt;}&lt;br /&gt;// Iterate through the SqlParameters, assigning the values from the corresponding position in the&lt;br /&gt;// value array&lt;br /&gt;for (int i = 0, j = commandParameters.Length; i &lt; j; i++)&lt;br /&gt;{&lt;br /&gt;// If the current array value derives from IDbDataParameter, then assign its Value property&lt;br /&gt;if (parameterValues[i] is IDbDataParameter)&lt;br /&gt;{&lt;br /&gt;IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i];&lt;br /&gt;if( paramInstance.Value == null )&lt;br /&gt;{&lt;br /&gt;commandParameters[i].Value = DBNull.Value;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;commandParameters[i].Value = paramInstance.Value;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;else if (parameterValues[i] == null)&lt;br /&gt;{&lt;br /&gt;commandParameters[i].Value = DBNull.Value;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;commandParameters[i].Value = parameterValues[i];&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// This method opens (if necessary) and assigns a connection, transaction, command type and parameters&lt;br /&gt;/// to the provided command&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="command"&gt;The SqlCommand to be prepared&lt;/param&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection, on which to execute this command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction, or 'null'&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParameters to be associated with the command or 'null' if no parameters are required&lt;/param&gt;&lt;br /&gt;/// &lt;param name="mustCloseConnection"&gt;&lt;c&gt;true&lt;/c&gt; if the connection was opened by the method, otherwose is false.&lt;/param&gt;&lt;br /&gt;private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection )&lt;br /&gt;{&lt;br /&gt;if( command == null ) throw new ArgumentNullException( "command" );&lt;br /&gt;if( commandText == null  commandText.Length == 0 ) throw new ArgumentNullException( "commandText" );&lt;br /&gt;// If the provided connection is not open, we will open it&lt;br /&gt;if (connection.State != ConnectionState.Open)&lt;br /&gt;{&lt;br /&gt;mustCloseConnection = true;&lt;br /&gt;connection.Open();&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;mustCloseConnection = false;&lt;br /&gt;}&lt;br /&gt;// Associate the connection with the command&lt;br /&gt;command.Connection = connection;&lt;br /&gt;// Set the command text (stored procedure name or SQL statement)&lt;br /&gt;command.CommandText = commandText;&lt;br /&gt;// If we were provided a transaction, assign it&lt;br /&gt;if (transaction != null)&lt;br /&gt;{&lt;br /&gt;if( transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;command.Transaction = transaction;&lt;br /&gt;}&lt;br /&gt;// Set the command type&lt;br /&gt;command.CommandType = commandType;&lt;br /&gt;// Attach the command parameters if they are provided&lt;br /&gt;if (commandParameters != null)&lt;br /&gt;{&lt;br /&gt;AttachParameters(command, commandParameters);&lt;br /&gt;}&lt;br /&gt;return;&lt;br /&gt;}&lt;br /&gt;#endregion private utility methods &amp;amp; constructors&lt;br /&gt;#region ExecuteNonQuery&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in&lt;br /&gt;/// the connection string&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string&lt;br /&gt;/// using the provided parameters&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;// Create &amp;amp; open a SqlConnection, and dispose of it after we are done&lt;br /&gt;using (SqlConnection connection = new SqlConnection(connectionString))&lt;br /&gt;{&lt;br /&gt;connection.Open();&lt;br /&gt;// Call the overload that takes a connection in place of the connection string&lt;br /&gt;return ExecuteNonQuery(connection, commandType, commandText, commandParameters);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the database specified in&lt;br /&gt;/// the connection string using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored prcedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlConnection.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns no resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Finally, execute the command&lt;br /&gt;int retval = cmd.ExecuteNonQuery();&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;if( mustCloseConnection )&lt;br /&gt;connection.Close();&lt;br /&gt;return retval;&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlTransaction.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Finally, execute the command&lt;br /&gt;int retval = cmd.ExecuteNonQuery();&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;return retval;&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified&lt;br /&gt;/// SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion ExecuteNonQuery&lt;br /&gt;#region ExecuteDataset&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in&lt;br /&gt;/// the connection string.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;// Create &amp;amp; open a SqlConnection, and dispose of it after we are done&lt;br /&gt;using (SqlConnection connection = new SqlConnection(connectionString))&lt;br /&gt;{&lt;br /&gt;connection.Open();&lt;br /&gt;// Call the overload that takes a connection in place of the connection string&lt;br /&gt;return ExecuteDataset(connection, commandType, commandText, commandParameters);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in&lt;br /&gt;/// the connection string using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteDataset(connection, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Create the DataAdapter &amp;amp; DataSet&lt;br /&gt;using( SqlDataAdapter da = new SqlDataAdapter(cmd) )&lt;br /&gt;{&lt;br /&gt;DataSet ds = new DataSet();&lt;br /&gt;// Fill the DataSet using default values for DataTable names, etc&lt;br /&gt;da.Fill(ds);&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;if( mustCloseConnection )&lt;br /&gt;connection.Close();&lt;br /&gt;// Return the dataset&lt;br /&gt;return ds;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteDataset(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteDataset(transaction, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Create the DataAdapter &amp;amp; DataSet&lt;br /&gt;using( SqlDataAdapter da = new SqlDataAdapter(cmd) )&lt;br /&gt;{&lt;br /&gt;DataSet ds = new DataSet();&lt;br /&gt;// Fill the DataSet using default values for DataTable names, etc&lt;br /&gt;da.Fill(ds);&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;// Return the dataset&lt;br /&gt;return ds;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified&lt;br /&gt;/// SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDataset(SqlTransaction transaction, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion ExecuteDataset&lt;br /&gt;#region ExecuteReader&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// This enum is used to indicate whether the connection was provided by the caller, or created by SqlHelper, so that&lt;br /&gt;/// we can set the appropriate CommandBehavior when calling ExecuteReader()&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;private enum SqlConnectionOwnership&lt;br /&gt;{&lt;br /&gt;/// &lt;summary&gt;Connection is owned and managed by SqlHelper&lt;/summary&gt;&lt;br /&gt;Internal,&lt;br /&gt;/// &lt;summary&gt;Connection is owned and managed by the caller&lt;/summary&gt;&lt;br /&gt;External&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Create and prepare a SqlCommand, and call ExecuteReader with the appropriate CommandBehavior.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// If we created and opened the connection, we want the connection to be closed when the DataReader is closed.&lt;br /&gt;///&lt;br /&gt;/// If the caller provided the connection, we want to leave it to them to manage.&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection, on which to execute this command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction, or 'null'&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParameters to be associated with the command or 'null' if no parameters are required&lt;/param&gt;&lt;br /&gt;/// &lt;param name="connectionOwnership"&gt;Indicates whether the connection parameter was provided by the caller, or created by SqlHelper&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;SqlDataReader containing the results of the command&lt;/returns&gt;&lt;br /&gt;private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Create a reader&lt;br /&gt;SqlDataReader dataReader;&lt;br /&gt;// Call ExecuteReader with the appropriate CommandBehavior&lt;br /&gt;if (connectionOwnership == SqlConnectionOwnership.External)&lt;br /&gt;{&lt;br /&gt;dataReader = cmd.ExecuteReader();&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);&lt;br /&gt;}&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again.&lt;br /&gt;// HACK: There is a problem here, the output parameter values are fletched&lt;br /&gt;// when the reader is closed, so if the parameters are detached from the command&lt;br /&gt;// then the SqlReader can´t set its values.&lt;br /&gt;// When this happen, the parameters can´t be used again in other command.&lt;br /&gt;bool canClear = true;&lt;br /&gt;foreach(SqlParameter commandParameter in cmd.Parameters)&lt;br /&gt;{&lt;br /&gt;if (commandParameter.Direction != ParameterDirection.Input)&lt;br /&gt;canClear = false;&lt;br /&gt;}&lt;br /&gt;if (canClear)&lt;br /&gt;{&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;}&lt;br /&gt;return dataReader;&lt;br /&gt;}&lt;br /&gt;catch&lt;br /&gt;{&lt;br /&gt;if( mustCloseConnection )&lt;br /&gt;connection.Close();&lt;br /&gt;throw;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in&lt;br /&gt;/// the connection string.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteReader(connectionString, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;SqlConnection connection = null;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;connection = new SqlConnection(connectionString);&lt;br /&gt;connection.Open();&lt;br /&gt;// Call the private overload that takes an internally owned connection in place of the connection string&lt;br /&gt;return ExecuteReader(connection, null, commandType, commandText, commandParameters,SqlConnectionOwnership.Internal);&lt;br /&gt;}&lt;br /&gt;catch&lt;br /&gt;{&lt;br /&gt;// If we fail to return the SqlDatReader, we need to close the connection ourselves&lt;br /&gt;if( connection != null ) connection.Close();&lt;br /&gt;throw;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in&lt;br /&gt;/// the connection string using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;// Pass through the call to the private overload using a null transaction value and an externally owned connection&lt;br /&gt;return ExecuteReader(connection, (SqlTransaction)null, commandType, commandText, commandParameters, SqlConnectionOwnership.External);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteReader(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteReader(transaction, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;// Pass through to private overload, indicating that the connection is owned by the caller&lt;br /&gt;return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SqlConnectionOwnership.External);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified&lt;br /&gt;/// SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteReader(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion ExecuteReader&lt;br /&gt;#region ExecuteScalar&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in&lt;br /&gt;/// the connection string.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteScalar(connectionString, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a 1x1 resultset) against the database specified in the connection string&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;// Create &amp;amp; open a SqlConnection, and dispose of it after we are done&lt;br /&gt;using (SqlConnection connection = new SqlConnection(connectionString))&lt;br /&gt;{&lt;br /&gt;connection.Open();&lt;br /&gt;// Call the overload that takes a connection in place of the connection string&lt;br /&gt;return ExecuteScalar(connection, commandType, commandText, commandParameters);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the database specified in&lt;br /&gt;/// the connection string using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlConnection.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteScalar(connection, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Execute the command &amp;amp; return the results&lt;br /&gt;object retval = cmd.ExecuteScalar();&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;if( mustCloseConnection )&lt;br /&gt;connection.Close();&lt;br /&gt;return retval;&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(SqlConnection connection, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteScalar(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlTransaction.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteScalar(transaction, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Execute the command &amp;amp; return the results&lt;br /&gt;object retval = cmd.ExecuteScalar();&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;return retval;&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified&lt;br /&gt;/// SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalar(SqlTransaction transaction, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// PPull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion ExecuteScalar&lt;br /&gt;#region ExecuteXmlReader&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command using "FOR XML AUTO"&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An XmlReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteXmlReader(connection, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command using "FOR XML AUTO"&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An XmlReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Create the DataAdapter &amp;amp; DataSet&lt;br /&gt;XmlReader retval = cmd.ExecuteXmlReader();&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;return retval;&lt;br /&gt;}&lt;br /&gt;catch&lt;br /&gt;{&lt;br /&gt;if( mustCloseConnection )&lt;br /&gt;connection.Close();&lt;br /&gt;throw;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// XmlReader r = ExecuteXmlReader(conn, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure using "FOR XML AUTO"&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An XmlReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReader(SqlConnection connection, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command using "FOR XML AUTO"&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An XmlReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText)&lt;br /&gt;{&lt;br /&gt;// Pass through the call providing null for the set of SqlParameters&lt;br /&gt;return ExecuteXmlReader(transaction, commandType, commandText, (SqlParameter[])null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command using "FOR XML AUTO"&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An XmlReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand cmd = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Create the DataAdapter &amp;amp; DataSet&lt;br /&gt;XmlReader retval = cmd.ExecuteXmlReader();&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;cmd.Parameters.Clear();&lt;br /&gt;return retval;&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified&lt;br /&gt;/// SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// XmlReader r = ExecuteXmlReader(trans, "GetOrders", 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string spName, params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion ExecuteXmlReader&lt;br /&gt;#region FillDataset&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in&lt;br /&gt;/// the connection string.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;/param&gt;&lt;br /&gt;public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( dataSet == null ) throw new ArgumentNullException( "dataSet" );&lt;br /&gt;// Create &amp;amp; open a SqlConnection, and dispose of it after we are done&lt;br /&gt;using (SqlConnection connection = new SqlConnection(connectionString))&lt;br /&gt;{&lt;br /&gt;connection.Open();&lt;br /&gt;// Call the overload that takes a connection in place of the connection string&lt;br /&gt;FillDataset(connection, commandType, commandText, dataSet, tableNames);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;public static void FillDataset(string connectionString, CommandType commandType,&lt;br /&gt;string commandText, DataSet dataSet, string[] tableNames,&lt;br /&gt;params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( dataSet == null ) throw new ArgumentNullException( "dataSet" );&lt;br /&gt;// Create &amp;amp; open a SqlConnection, and dispose of it after we are done&lt;br /&gt;using (SqlConnection connection = new SqlConnection(connectionString))&lt;br /&gt;{&lt;br /&gt;connection.Open();&lt;br /&gt;// Call the overload that takes a connection in place of the connection string&lt;br /&gt;FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in&lt;br /&gt;/// the connection string using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, 24);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;public static void FillDataset(string connectionString, string spName,&lt;br /&gt;DataSet dataSet, string[] tableNames,&lt;br /&gt;params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( dataSet == null ) throw new ArgumentNullException( "dataSet" );&lt;br /&gt;// Create &amp;amp; open a SqlConnection, and dispose of it after we are done&lt;br /&gt;using (SqlConnection connection = new SqlConnection(connectionString))&lt;br /&gt;{&lt;br /&gt;connection.Open();&lt;br /&gt;// Call the overload that takes a connection in place of the connection string&lt;br /&gt;FillDataset (connection, spName, dataSet, tableNames, parameterValues);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;public static void FillDataset(SqlConnection connection, CommandType commandType,&lt;br /&gt;string commandText, DataSet dataSet, string[] tableNames)&lt;br /&gt;{&lt;br /&gt;FillDataset(connection, commandType, commandText, dataSet, tableNames, null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;public static void FillDataset(SqlConnection connection, CommandType commandType,&lt;br /&gt;string commandText, DataSet dataSet, string[] tableNames,&lt;br /&gt;params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;FillDataset(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(conn, "GetOrders", ds, new string[] {"orders"}, 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;public static void FillDataset(SqlConnection connection, string spName,&lt;br /&gt;DataSet dataSet, string[] tableNames,&lt;br /&gt;params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if ( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if (dataSet == null ) throw new ArgumentNullException( "dataSet" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;public static void FillDataset(SqlTransaction transaction, CommandType commandType,&lt;br /&gt;string commandText,&lt;br /&gt;DataSet dataSet, string[] tableNames)&lt;br /&gt;{&lt;br /&gt;FillDataset (transaction, commandType, commandText, dataSet, tableNames, null);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;public static void FillDataset(SqlTransaction transaction, CommandType commandType,&lt;br /&gt;string commandText, DataSet dataSet, string[] tableNames,&lt;br /&gt;params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified&lt;br /&gt;/// SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method provides no access to output parameters or the stored procedure's return value parameter.&lt;br /&gt;///&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(trans, "GetOrders", ds, new string[]{"orders"}, 24, 36);&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;/// &lt;param name="parameterValues"&gt;An array of objects to be assigned as the input values of the stored procedure&lt;/param&gt;&lt;br /&gt;public static void FillDataset(SqlTransaction transaction, string spName,&lt;br /&gt;DataSet dataSet, string[] tableNames,&lt;br /&gt;params object[] parameterValues)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( dataSet == null ) throw new ArgumentNullException( "dataSet" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((parameterValues != null) &amp;amp;&amp;amp; (parameterValues.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Assign the provided values to these parameters based on parameter order&lt;br /&gt;AssignParameterValues(commandParameters, parameterValues);&lt;br /&gt;// Call the overload that takes an array of SqlParameters&lt;br /&gt;FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Otherwise we can just call the SP without params&lt;br /&gt;FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Private helper method that execute a SqlCommand (that returns a resultset) against the specified SqlTransaction and SqlConnection&lt;br /&gt;/// using the provided parameters.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandType"&gt;The CommandType (stored procedure, text, etc.)&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;A dataset wich will contain the resultset generated by the command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableNames"&gt;This array will be used to create table mappings allowing the DataTables to be referenced&lt;br /&gt;/// by a user defined name (probably the actual table name)&lt;br /&gt;/// &lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters used to execute the command&lt;/param&gt;&lt;br /&gt;private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType,&lt;br /&gt;string commandText, DataSet dataSet, string[] tableNames,&lt;br /&gt;params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( dataSet == null ) throw new ArgumentNullException( "dataSet" );&lt;br /&gt;// Create a command and prepare it for execution&lt;br /&gt;SqlCommand command = new SqlCommand();&lt;br /&gt;bool mustCloseConnection = false;&lt;br /&gt;PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );&lt;br /&gt;// Create the DataAdapter &amp;amp; DataSet&lt;br /&gt;using( SqlDataAdapter dataAdapter = new SqlDataAdapter(command) )&lt;br /&gt;{&lt;br /&gt;// Add the table mappings specified by the user&lt;br /&gt;if (tableNames != null &amp;amp;&amp;amp; tableNames.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;string tableName = "Table";&lt;br /&gt;for (int index=0; index &lt; tableNames.Length; index++)&lt;br /&gt;{&lt;br /&gt;if( tableNames[index] == null  tableNames[index].Length == 0 ) throw new ArgumentException( "The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames" );&lt;br /&gt;dataAdapter.TableMappings.Add(tableName, tableNames[index]);&lt;br /&gt;tableName += (index + 1).ToString();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;// Fill the DataSet using default values for DataTable names, etc&lt;br /&gt;dataAdapter.Fill(dataSet);&lt;br /&gt;// Detach the SqlParameters from the command object, so they can be used again&lt;br /&gt;command.Parameters.Clear();&lt;br /&gt;}&lt;br /&gt;if( mustCloseConnection )&lt;br /&gt;connection.Close();&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;#region UpdateDataset&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Executes the respective command for each inserted, updated, or deleted row in the DataSet.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="insertCommand"&gt;A valid transact-SQL statement or stored procedure to insert new records into the data source&lt;/param&gt;&lt;br /&gt;/// &lt;param name="deleteCommand"&gt;A valid transact-SQL statement or stored procedure to delete records from the data source&lt;/param&gt;&lt;br /&gt;/// &lt;param name="updateCommand"&gt;A valid transact-SQL statement or stored procedure used to update records in the data source&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataSet"&gt;The DataSet used to update the data source&lt;/param&gt;&lt;br /&gt;/// &lt;param name="tableName"&gt;The DataTable used to update the data source.&lt;/param&gt;&lt;br /&gt;public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName)&lt;br /&gt;{&lt;br /&gt;if( insertCommand == null ) throw new ArgumentNullException( "insertCommand" );&lt;br /&gt;if( deleteCommand == null ) throw new ArgumentNullException( "deleteCommand" );&lt;br /&gt;if( updateCommand == null ) throw new ArgumentNullException( "updateCommand" );&lt;br /&gt;if( tableName == null  tableName.Length == 0 ) throw new ArgumentNullException( "tableName" );&lt;br /&gt;// Create a SqlDataAdapter, and dispose of it after we are done&lt;br /&gt;using (SqlDataAdapter dataAdapter = new SqlDataAdapter())&lt;br /&gt;{&lt;br /&gt;// Set the data adapter commands&lt;br /&gt;dataAdapter.UpdateCommand = updateCommand;&lt;br /&gt;dataAdapter.InsertCommand = insertCommand;&lt;br /&gt;dataAdapter.DeleteCommand = deleteCommand;&lt;br /&gt;// Update the dataset changes in the data source&lt;br /&gt;dataAdapter.Update (dataSet, tableName);&lt;br /&gt;// Commit all the changes made to the DataSet&lt;br /&gt;dataSet.AcceptChanges();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;#region CreateCommand&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Simplify the creation of a Sql command object by allowing&lt;br /&gt;/// a stored procedure and optional parameters to be provided&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// e.g.:&lt;br /&gt;/// SqlCommand command = CreateCommand(conn, "AddCustomer", "CustomerID", "CustomerName");&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="sourceColumns"&gt;An array of string to be assigned as the source columns of the stored procedure parameters&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A valid SqlCommand object&lt;/returns&gt;&lt;br /&gt;public static SqlCommand CreateCommand(SqlConnection connection, string spName, params string[] sourceColumns)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// Create a SqlCommand&lt;br /&gt;SqlCommand cmd = new SqlCommand( spName, connection );&lt;br /&gt;cmd.CommandType = CommandType.StoredProcedure;&lt;br /&gt;// If we receive parameter values, we need to figure out where they go&lt;br /&gt;if ((sourceColumns != null) &amp;amp;&amp;amp; (sourceColumns.Length &gt; 0))&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Assign the provided source columns to these parameters based on parameter order&lt;br /&gt;for (int index=0; index &lt; sourceColumns.Length; index++)&lt;br /&gt;commandParameters[index].SourceColumn = sourceColumns[index];&lt;br /&gt;// Attach the discovered parameters to the SqlCommand object&lt;br /&gt;AttachParameters (cmd, commandParameters);&lt;br /&gt;}&lt;br /&gt;return cmd;&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;#region ExecuteNonQueryTypedParams&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the database specified in&lt;br /&gt;/// the connection string using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on row values.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQueryTypedParams(String connectionString, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if (dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified SqlConnection&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on row values.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQueryTypedParams(SqlConnection connection, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if (dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified&lt;br /&gt;/// SqlTransaction using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on row values.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An int representing the number of rows affected by the command&lt;/returns&gt;&lt;br /&gt;public static int ExecuteNonQueryTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// Sf the row has values, the store procedure parameters must be initialized&lt;br /&gt;if (dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;#region ExecuteDatasetTypedParams&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in&lt;br /&gt;/// the connection string using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on row values.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;//If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if ( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the dataRow column values as the store procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on row values.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDatasetTypedParams(SqlConnection connection, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on row values.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A dataset containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static DataSet ExecuteDatasetTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;#region ExecuteReaderTypedParams&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in&lt;br /&gt;/// the connection string using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReaderTypedParams(String connectionString, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if ( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0 )&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;A SqlDataReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static SqlDataReader ExecuteReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0 )&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;#region ExecuteScalarTypedParams&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the database specified in&lt;br /&gt;/// the connection string using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalarTypedParams(String connectionString, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalarTypedParams(SqlConnection connection, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An object containing the value in the 1x1 resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static object ExecuteScalarTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;#region ExecuteXmlReaderTypedParams&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An XmlReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction&lt;br /&gt;/// using the dataRow column values as the stored procedure's parameters values.&lt;br /&gt;/// This method will query the database to discover the parameters for the&lt;br /&gt;/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="transaction"&gt;A valid SqlTransaction object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="dataRow"&gt;The dataRow used to hold the stored procedure's parameter values.&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An XmlReader containing the resultset generated by the command&lt;/returns&gt;&lt;br /&gt;public static XmlReader ExecuteXmlReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)&lt;br /&gt;{&lt;br /&gt;if( transaction == null ) throw new ArgumentNullException( "transaction" );&lt;br /&gt;if( transaction != null &amp;amp;&amp;amp; transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;// If the row has values, the store procedure parameters must be initialized&lt;br /&gt;if( dataRow != null &amp;amp;&amp;amp; dataRow.ItemArray.Length &gt; 0)&lt;br /&gt;{&lt;br /&gt;// Pull the parameters for this stored procedure from the parameter cache (or discover them &amp;amp; populate the cache)&lt;br /&gt;SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);&lt;br /&gt;// Set the parameters values&lt;br /&gt;AssignParameterValues(commandParameters, dataRow);&lt;br /&gt;return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// SqlHelperParameterCache provides functions to leverage a static cache of procedure parameters, and the&lt;br /&gt;/// ability to discover parameters for stored procedures at run-time.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public sealed class SqlHelperParameterCache&lt;br /&gt;{&lt;br /&gt;#region private methods, variables, and constructors&lt;br /&gt;//Since this class provides only static methods, make the default constructor private to prevent&lt;br /&gt;//instances from being created with "new SqlHelperParameterCache()"&lt;br /&gt;private SqlHelperParameterCache() {}&lt;br /&gt;private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Resolve at run time the appropriate set of SqlParameters for a stored procedure&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="includeReturnValueParameter"&gt;Whether or not to include their return value parameter&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;The parameter array discovered.&lt;/returns&gt;&lt;br /&gt;private static SqlParameter[] DiscoverSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;SqlCommand cmd = new SqlCommand(spName, connection);&lt;br /&gt;cmd.CommandType = CommandType.StoredProcedure;&lt;br /&gt;connection.Open();&lt;br /&gt;SqlCommandBuilder.DeriveParameters(cmd);&lt;br /&gt;connection.Close();&lt;br /&gt;if (!includeReturnValueParameter)&lt;br /&gt;{&lt;br /&gt;cmd.Parameters.RemoveAt(0);&lt;br /&gt;}&lt;br /&gt;SqlParameter[] discoveredParameters = new SqlParameter[cmd.Parameters.Count];&lt;br /&gt;cmd.Parameters.CopyTo(discoveredParameters, 0);&lt;br /&gt;// Init the parameters with a DBNull value&lt;br /&gt;foreach (SqlParameter discoveredParameter in discoveredParameters)&lt;br /&gt;{&lt;br /&gt;discoveredParameter.Value = DBNull.Value;&lt;br /&gt;}&lt;br /&gt;return discoveredParameters;&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Deep copy of cached SqlParameter array&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="originalParameters"&gt;&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;&lt;/returns&gt;&lt;br /&gt;private static SqlParameter[] CloneParameters(SqlParameter[] originalParameters)&lt;br /&gt;{&lt;br /&gt;SqlParameter[] clonedParameters = new SqlParameter[originalParameters.Length];&lt;br /&gt;for (int i = 0, j = originalParameters.Length; i &lt; j; i++)&lt;br /&gt;{&lt;br /&gt;clonedParameters[i] = (SqlParameter)((ICloneable)originalParameters[i]).Clone();&lt;br /&gt;}&lt;br /&gt;return clonedParameters;&lt;br /&gt;}&lt;br /&gt;#endregion private methods, variables, and constructors&lt;br /&gt;#region caching functions&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Add parameter array to the cache&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandParameters"&gt;An array of SqlParamters to be cached&lt;/param&gt;&lt;br /&gt;public static void CacheParameterSet(string connectionString, string commandText, params SqlParameter[] commandParameters)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( commandText == null  commandText.Length == 0 ) throw new ArgumentNullException( "commandText" );&lt;br /&gt;string hashKey = connectionString + ":" + commandText;&lt;br /&gt;paramCache[hashKey] = commandParameters;&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieve a parameter array from the cache&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="commandText"&gt;The stored procedure name or T-SQL command&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An array of SqlParamters&lt;/returns&gt;&lt;br /&gt;public static SqlParameter[] GetCachedParameterSet(string connectionString, string commandText)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( commandText == null  commandText.Length == 0 ) throw new ArgumentNullException( "commandText" );&lt;br /&gt;string hashKey = connectionString + ":" + commandText;&lt;br /&gt;SqlParameter[] cachedParameters = paramCache[hashKey] as SqlParameter[];&lt;br /&gt;if (cachedParameters == null)&lt;br /&gt;{&lt;br /&gt;return null;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return CloneParameters(cachedParameters);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;#endregion caching functions&lt;br /&gt;#region Parameter Discovery Functions&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieves the set of SqlParameters appropriate for the stored procedure&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method will query the database for this information, and then store it in a cache for future requests.&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An array of SqlParameters&lt;/returns&gt;&lt;br /&gt;public static SqlParameter[] GetSpParameterSet(string connectionString, string spName)&lt;br /&gt;{&lt;br /&gt;return GetSpParameterSet(connectionString, spName, false);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieves the set of SqlParameters appropriate for the stored procedure&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method will query the database for this information, and then store it in a cache for future requests.&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connectionString"&gt;A valid connection string for a SqlConnection&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="includeReturnValueParameter"&gt;A bool value indicating whether the return value parameter should be included in the results&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An array of SqlParameters&lt;/returns&gt;&lt;br /&gt;public static SqlParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)&lt;br /&gt;{&lt;br /&gt;if( connectionString == null  connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;using(SqlConnection connection = new SqlConnection(connectionString))&lt;br /&gt;{&lt;br /&gt;return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieves the set of SqlParameters appropriate for the stored procedure&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method will query the database for this information, and then store it in a cache for future requests.&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An array of SqlParameters&lt;/returns&gt;&lt;br /&gt;internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName)&lt;br /&gt;{&lt;br /&gt;return GetSpParameterSet(connection, spName, false);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieves the set of SqlParameters appropriate for the stored procedure&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;remarks&gt;&lt;br /&gt;/// This method will query the database for this information, and then store it in a cache for future requests.&lt;br /&gt;/// &lt;/remarks&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="includeReturnValueParameter"&gt;A bool value indicating whether the return value parameter should be included in the results&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An array of SqlParameters&lt;/returns&gt;&lt;br /&gt;internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;using (SqlConnection clonedConnection = (SqlConnection)((ICloneable)connection).Clone())&lt;br /&gt;{&lt;br /&gt;return GetSpParameterSetInternal(clonedConnection, spName, includeReturnValueParameter);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieves the set of SqlParameters appropriate for the stored procedure&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="connection"&gt;A valid SqlConnection object&lt;/param&gt;&lt;br /&gt;/// &lt;param name="spName"&gt;The name of the stored procedure&lt;/param&gt;&lt;br /&gt;/// &lt;param name="includeReturnValueParameter"&gt;A bool value indicating whether the return value parameter should be included in the results&lt;/param&gt;&lt;br /&gt;/// &lt;returns&gt;An array of SqlParameters&lt;/returns&gt;&lt;br /&gt;private static SqlParameter[] GetSpParameterSetInternal(SqlConnection connection, string spName, bool includeReturnValueParameter)&lt;br /&gt;{&lt;br /&gt;if( connection == null ) throw new ArgumentNullException( "connection" );&lt;br /&gt;if( spName == null  spName.Length == 0 ) throw new ArgumentNullException( "spName" );&lt;br /&gt;string hashKey = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter":"");&lt;br /&gt;SqlParameter[] cachedParameters;&lt;br /&gt;cachedParameters = paramCache[hashKey] as SqlParameter[];&lt;br /&gt;if (cachedParameters == null)&lt;br /&gt;{&lt;br /&gt;SqlParameter[] spParameters = DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);&lt;br /&gt;paramCache[hashKey] = spParameters;&lt;br /&gt;cachedParameters = spParameters;&lt;br /&gt;}&lt;br /&gt;return CloneParameters(cachedParameters);&lt;br /&gt;}&lt;br /&gt;#endregion Parameter Discovery Functions&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6721503270778376897-2587860752149677697?l=uroojs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uroojs.blogspot.com/feeds/2587860752149677697/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://uroojs.blogspot.com/2009/11/sql-helper.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/2587860752149677697'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/2587860752149677697'/><link rel='alternate' type='text/html' href='http://uroojs.blogspot.com/2009/11/sql-helper.html' title='Sql Helper'/><author><name>Urooj Mahmood</name><uri>http://www.blogger.com/profile/15713414569362527460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6721503270778376897.post-5114376976977974516</id><published>2009-11-05T02:35:00.000-08:00</published><updated>2009-11-05T08:41:52.142-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SqL Connection'/><title type='text'>Dotnet Help...sql connection</title><content type='html'>using System.Data.OleDb;&lt;br /&gt;namespace ConnectOleDbDataSource&lt;br /&gt;&lt;br /&gt;{ class Program&lt;br /&gt;{&lt;br /&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;string oledbConnectString = "Provider=SQLOLEDB;Data Source=(local);" + "Initial Catalog=AdventureWorks;Integrated Security=SSPI";&lt;br /&gt;string sqlSelect = "SELECT TOP 5 Title, FirstName, LastName " + "FROM Person.Contact"; // Create an ODBC Connection using&lt;br /&gt;&lt;br /&gt;(OleDbConnection connection = new OleDbConnection(oledbConnectString)) { OleDbCommand command = new OleDbCommand(sqlSelect, connection);&lt;br /&gt;// Execute the DataReader&lt;br /&gt;connection.Open( );&lt;br /&gt;OleDbDataReader reader = command.ExecuteReader( ); // Output the data from the DataReader to the console&lt;br /&gt;while (reader.Read( ))&lt;br /&gt;Console.WriteLine("{0} {1}", reader[0], reader[1]);&lt;br /&gt;}&lt;br /&gt;Console.WriteLine("\nPress any key to continue."&lt;a name="idx-CHP-1-0182"&gt;&lt;/a&gt;);&lt;br /&gt;Console.ReadKey( );&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;add name="SchoolSys" providername="System.Data.SqlClient" connectionstring="Data Source=FGCDC1;Initial Catalog=SchoolSysNoData;UID=sa;PWD=orion123"&gt;&lt;br /&gt;string con = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SchoolSys"].ToString();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;add name="SchoolSys" providername="System.Data.SqlClient" connectionstring="Data Source=FGCDC1;Initial Catalog=SchoolSysNoData;UID=sa;PWD=orion123"&gt;&lt;br /&gt;add name="name" connectionString="Data Source=servername;Initial Catalog=dbname;UID=uid;PWD=pw" providerName="System.Data.SqlClient"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6721503270778376897-5114376976977974516?l=uroojs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uroojs.blogspot.com/feeds/5114376976977974516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://uroojs.blogspot.com/2009/11/dotnet-helpsql-connection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/5114376976977974516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6721503270778376897/posts/default/5114376976977974516'/><link rel='alternate' type='text/html' href='http://uroojs.blogspot.com/2009/11/dotnet-helpsql-connection.html' title='Dotnet Help...sql connection'/><author><name>Urooj Mahmood</name><uri>http://www.blogger.com/profile/15713414569362527460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
