I am making the transition from classic ASP to ASP.NET. And I have some problems doing some basic things that I usually did with the old method. The following is a handy ASP function that I used to execute scalar queries with multiple lines.
FUNCTION ExecuteScalarParams(SQLQuery, Parameter_Array) Set cmd1 = Server.CreateObject("ADODB.Command") cmd1.ActiveConnection = con cmd1.CommandText = SQLQuery cmd1.CommandType = 1 FOR ParamCount = 0 TO UBOUND(Parameter_Array) cmd1.Parameters(ParamCount) = Parameter_Array(ParamCount) NEXT 'ParamCount Set rstScalar = cmd1.Execute() IF NOT rstScalar.EOF THEN arrScalar = rstScalar.GetRows() IF UBOUND(arrScalar,2) = 0 THEN ExecuteScalarParams = arrScalar(0,0) ELSE ExecuteScalarParams = NULL END IF ELSE ExecuteScalarParams = NULL END IF rstScalar.Close Set rstScalar = Nothing Set cmd1 = Nothing END FUNCTION
I used to pass an SQL query with question marks as place holders for parameters like this:
SELECT TOP 1 UserName FROM Members WHERE (Created>?) AND (AdminLevel=?);
Then I would set up an array of parameters and pass it to the function:
MyArray = ARRAY("1-JAN-2012",1)
The parameters in the array will replace the question marks in the query string in the order in which they appear.
I am trying to reproduce this function in C #, but I am stuck in the part where I need to pass parameters. So far, I have come to the point that I need to use nominal names such as @Created and @AdminLevel instead of question marks, and then I need to configure parameter objects such as this:
SqlParameter param = new SqlParameter(); param.ParameterName = "@AdminLevel"; param.Value = 1;
Is there a way to pass parameters without having to set parameter names and just use question marks and the order in which they appear, indicate which parameter goes where?
Osprey
source share