Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Parameters and Question Marks
    primarykey
    data
    text
    <p>I am making the switch from classic ASP to ASP.NET. And I am having some trouble doing some basic stuff that I used to do easily with the old method. Below is a handy ASP function that I used to execute scalar queries with a few lines.</p> <pre><code>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 </code></pre> <p>I used to pass a SQL query with question marks as place holders for the parameters like this:</p> <pre><code>SELECT TOP 1 UserName FROM Members WHERE (Created&gt;?) AND (AdminLevel=?); </code></pre> <p>I would then set up a parameters array and pass it on to the function:</p> <pre><code>MyArray = ARRAY("1-JAN-2012",1) </code></pre> <p>The parameters in the array would replace the question marks in the query string in the order they appear.</p> <p>I am trying to mimic this function in C# but I am stuck in the part where I have to pass the parameters. So far I got to the point where I have to used named place holders such as @Created and @AdminLevel instead of the question marks and then I have to set up parameter objects like this:</p> <pre><code>SqlParameter param = new SqlParameter(); param.ParameterName = "@AdminLevel"; param.Value = 1; </code></pre> <p>Is there a way to pass the parameters without having to set the parameter names and simply use question marks and the order in which they appear to specify which parameter goes where?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload