The following method is that I have explored in the actual development, which can a large extent simplify the code of calling the stored procedure. First, look at the general process of the C # call the stored procedure: 1. Open the database to connect SQLConnection; 2, generate a SQLCommand; 3, fill the parameters to the command object; 4, perform the stored procedure; 5, turn off the connection; 6, other operations. I am mainly simplified here to simplify the 3rd step, and ultimately you only need to pass the name of the stored procedure and the corresponding parameter values when the stored procedure is called. Calling examples are as follows: dbaccess.run ("p_am_deletefile", new object [] {loginid, refress.userhostaddress, fileId}); because two values must be two values during the fill parameters, one is the name of the parameter, one is the value of the parameter . The parameter value is incorporated by external, no consideration; and the parameter name is related to the stored procedure, it should be able to determine by the stored procedure name without having to write it once. For this problem, if you can save the parameters of the stored procedure to a global place, you can use it according to the name of the stored procedure when calling the stored procedure. When I implement, I am saving this information in the database access component, using the name / value. The code is as follows: public class infotable: NameObjectCollectionBase {public object this [string key] {get {return (this.baseget (key));} set {this.baseset (key, value);}}}. . . . . . protected static infotable procinfotable = new infintable () ;. . . . . . Public static infotable procinfotable;}} This, when you actually call the stored procedure, you can know the parameter name of the stored procedure when you actually call the stored procedure.
The code is as follows: Public DataTable Run (String Procname, Object [] PARMS, REF INT RETVALUE) {string [] paraminfo = (string []); if (paraminfo == null) {ErrorInfo.seterrorInfo "Nothing" procname "parameters!"); 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
The database I use is SQL Server 2000, and a stored procedure is given to solve this annoying problem: create procedure dbo.p_am_procinfo (@ProcName T_STR64 - stored procedure name) Asbegin 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, and when the name of the stored procedure is not passed, the stored procedure returns the name of all stored procedures starting with "p_am_"; after the corresponding stored procedure name, the storage The process returns the parameter list of the stored procedure. In this way, we can take the stored procedure parameters in the system in the program and save it to the procinfotable property of the database access component. The specific code is as follows: span.dbaccess dbaccess = new span.dbaccess (); //// Configuration Table //span.dbaccess.procinfotable ["p_am_procinfo"] = new string [] {"@ procname"} ; /// Other stored procedure list // datatable dt = dbaccess.run ("p_am_procinfo", new object [] {"}); if (dt == null || dt.rows.count <= 0) {RETURN;} /// Number of other stored procedures to get other stored procedures //5 (DATAROW DR IN DT.ROWS) {dataable 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