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;
}