The following method is found in actual development and can simplify the calling of the stored procedure to a large extent.Code.
First, let's take a look at the general process of calling the stored procedure in C:
1. Open the database connection sqlconnection;
2. Generate a sqlcommand;
3. Fill in parameters to the command object;
4. Execute the stored procedure;
5. Close the connection;
6. other operations.
I will focus on simplifying Step 1. When calling a stored procedure, you only need to pass the name of the stored procedure and the corresponding parameter values. Call example:
Dbaccess. Run ("p_am_deletefile", new object [] {loginid, request. userhostaddress, fileid });
because two values are required during parameter filling, one is the parameter name and the other is the parameter value. Parameter values are imported from outside, so you do not need to consider them. The parameter name is related to the stored procedure and should be determined by the stored procedure name instead of being written to each call. If you can save the stored procedure parameters to a global location, you only need to be able to index the stored procedure according to its name when calling the stored procedure. In actual implementation, I save the information in the database access component and use the name/value pair method. The Code is as follows:
public class infotable: nameobjectcollectionbase
{< br> Public object this [String key]
{< br> Get
{< br> return (this. baseget (key);
}< br> set
{< br> This. baseset (Key, value);
}< BR >}< br> ......
protected static infotable procinfotable = new infotable ();
......
Public static infotable procinfotable
{< br> Get
{< br> return procinfotable;
}< BR >}
In this way, you only need to check the table to know the parameter name of the stored procedure when calling the stored procedure. The implementation code is as follows:
Public datatable run (string procname, object [] parms, ref int retvalue)
{
String [] paraminfo = (string []) (procinfotable [procname]);
If (paraminfo = NULL)
{
Errorinfo. seterrorinfo ("not obtained" + procname +! ");
Return NULL;
}
Bool bopened = (dbconn. State = connectionstate. Open );
If (! Bopened &&! Connect ())
{
Return NULL;
}
Dataset DS = new dataset ();
Try
{
Sqlcommand cmd = new sqlcommand (procname, dbconn );
Cmd. commandtype = commandtype. storedprocedure;
For (INT I = 0; I <parms. Length & I <paraminfo. length; ++ I)
{
Cmd. Parameters. Add (New sqlparameter (paraminfo [I], parms [I]);
}
Sqlparameter parmsr = new sqlparameter ("return", sqldbtype. INT );
Parmsr. Direction = parameterdirection. returnvalue;
Cmd. Parameters. Add (parmsr );
Sqldataadapter ADP = new sqldataadapter (CMD );
ADP. Fill (DS );
Retvalue = (INT) (CMD. Parameters ["return"]. value );
}
Catch (exception ex)
{
Errorinfo. seterrorinfo (ex. Message );
Retvalue =-1;
}
If (! Bopened)
Close ();
If (Ds. Tables. Count> 0)
Return Ds. Tables [0];
Else
Return NULL;
}
We can see that the parameter list of each stored procedure is stored as a string []. The next step is to fill in many stored procedure parameters in the system into the procinfotable table. The database I use is SQL Server 2000. Here is a stored procedure to solve this annoying problem:
Create procedure DBO. p_am_procinfo
(
@ Procname t_str64 -- Name of the stored procedure
)
As
Begin
Set nocount on
If @ procname = ''begin
Select name as procname
From sysobjects
Where substring (sysobjects. Name, 1, 5) = 'P _ am _'
End
Else begin
Select
Syscolumns. Name as paramname
From sysobjects, syscolumns
Where sysobjects. ID = syscolumns. ID
And sysobjects. Name = @ procname
Order by colid
End
End
This stored procedure has two functions. When the name of a stored procedure is not passed, the stored procedure returns the names of all stored procedures starting with "p_am; after a stored procedure name is input, the stored procedure returns the parameter list of the stored procedure. In this way, weProgramYou can retrieve the stored procedure parameter list from the system and save it to the procinfotable attribute of the database access component. The Code is as follows:
Span. dbaccess = new span. dbaccess ();
//
// Construct a parameter table for the stored procedure
//
Span. dbaccess. procinfotable ["p_am_procinfo"] = new string [] {"@ procname "};
//
// Obtain the list of other stored procedures
//
Datatable dt = dbaccess. Run ("p_am_procinfo", new object [] {""});
If (Dt = NULL | DT. Rows. Count <= 0)
{
Return;
}
//
// Obtain parameters for other stored procedures
//
Foreach (datarow DR in DT. Rows)
{
Datatable dtparams = dbaccess. Run ("p_am_procinfo", new object [] {Dr ["procname"]});
If (dtparams! = NULL)
{
String [] paraminfo = new string [dtparams. Rows. Count];
For (INT I = 0; I <dtparams. Rows. Count; ++ I)
Paraminfo [I] = dtparams. Rows [I] ["paramname"]. tostring ();
Span. dbaccess. procinfotable [Dr ["procname"]. tostring ()] = paraminfo;
}
}
So far, all technical details have been introduced. In addition, several interface functions of the database access object are also provided:
// Open or close the database connection
Public bool connect (string strconn)
Public bool connect ()
Public bool close ()
// Execute the SQL command (only one int is returned)
Public int exec (string procname, object [] parms)
Public int exec (string SQL)
// Run (return a datatable)
Public datatable run (string procname, object [] parms, ref int retvalue)
Public datatable run (string procname, object [] parms)
Public datatable run (string SQL)
// Query by PAGE (page number starts from 1 and returns a datatable)
Public datatable pagequery
(
String selectcmd,
Int pagesize,
Int pagenumber
)