SQL Server stored procedure dynamic parameter call implementation code _MSSQL

Source: Internet
Author: User
Just taking notes, nothing!!
Copy Code code as follows:

--Create a test table
CREATE TABLE [dbo]. [Student] (
[ID] [int] IDENTITY (1,1) not NULL PRIMARY KEY,
[Name] [nvarchar] () not NULL DEFAULT ('),
[Age] [INT] Not NULL DEFAULT (0),
[Sex] [Bit] Not NULL DEFAULT (0),
[Address] [nvarchar] () not NULL DEFAULT (')
)
-for example, a query stored procedure
Create PROC Getstudentbytype
@type int = 0,--1 according to ID query, 2 according to sex
@args XML--The parameters are written here.
As
BEGIN
DECLARE @id INT, @sex BIT
SET @id = @args. Value (' (ARGS/ID) [1] ', ' int ')--parameters can be written here, if not passed over, the big deal is null value, anyway, it doesn't matter.
SET @sex = @args. Value (' (args/sex) [1] ', ' bit ')
IF (@type =1)
BEGIN
SELECT * FROM dbo. Student WHERE id= @id
End
IF (@type =2)
BEGIN
SELECT * FROM dbo. Student WHERE sex= @sex
End
End

Parameter writing in XML is much better than using strings, so the call time parameter is not organized, so here's a help class Xmlargs
Copy Code code as follows:

public class Xmlargs
{
private string _strargs = String. Empty;
private bool _iscreate = false;
Private dictionary<string, string> _args;
public string Args
{
Get
{
if (!_iscreate)
{
_strargs = _createargs ();
_iscreate = true;
}
return _strargs;
}
}
Public Xmlargs ()
{
_args = new dictionary<string, string> ();
}
public void Add (string key, Object value)
{
_args. ADD (key, value. ToString ());
_iscreate = false;
}
public void Remove (string key)
{
_args. Remove (key);
_iscreate = false;
}
public void Clear ()
{
_args. Clear ();
_iscreate = false;
}
private String _createargs ()
{
if (_args. Count = 0)
{
return string. Empty;
}
StringBuilder sb = new StringBuilder ();
foreach (String key in _args. Keys)
{
Sb. AppendFormat ("<{0}>{1}</{0}>", Key, _args[key]);
}
Return SB. ToString ();
}
}

Call:
Copy Code code as follows:

private void Binddata ()
{
Xmlargs args = new Xmlargs ();
args. ADD ("id", 1);
System.Data.DataTable dt = getstudentbytype (1, args);
Gridview1.datashow (DT);
}
Private System.Data.DataTable getstudentbytype (int type, Xmlargs args)
{
SqlHelper helper = new Sq Lhelper ();
Helper. Params.add ("type", type);
Helper. Params.add ("args", args. Args);
System.Data.DataTable dt = helper. Rundatatable ("Getstudentbytype");
Return DT;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.