SQL 参数和问号

发布于 2025-01-02 10:20:34 字数 1331 浏览 0 评论 0原文

我正在从经典 ASP 切换到 ASP.NET。我在做一些我以前用旧方法可以轻松完成的基本事情时遇到了一些麻烦。下面是一个方便的 ASP 函数,我用它来执行几行标量查询。

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

我曾经传递一个带有问号作为参数占位符的 SQL 查询,如下所示:

SELECT TOP 1 UserName FROM Members WHERE (Created>?) AND (AdminLevel=?);

然后我会设置一个参数数组并将其传递给函数:

MyArray = ARRAY("1-JAN-2012",1)

数组中的参数将替换查询字符串中的问号它们出现的顺序。

我试图在 C# 中模仿这个函数,但我陷入了必须传递参数的部分。到目前为止,我已经到了必须使用命名占位符(例如 @Created 和 @AdminLevel)而不是问号的地步,然后我必须像这样设置参数对象:

SqlParameter param = new SqlParameter();
param.ParameterName = "@AdminLevel";
param.Value = 1;

是否有一种方法可以传递参数而无需设置参数名称并简单地使用问号和它们出现的顺序来指定哪个参数在哪里?

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.

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 a SQL query with question marks as place holders for the parameters like this:

SELECT TOP 1 UserName FROM Members WHERE (Created>?) AND (AdminLevel=?);

I would then set up a parameters array and pass it on to the function:

MyArray = ARRAY("1-JAN-2012",1)

The parameters in the array would replace the question marks in the query string in the order they appear.

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:

SqlParameter param = new SqlParameter();
param.ParameterName = "@AdminLevel";
param.Value = 1;

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拍不死你 2025-01-09 10:20:34

编辑:正如 Dana 所指出的,MSDN 参数文档 显示您需要对 SqlClient 使用命名参数,但可以对 OleDb/ODBC 使用位置参数。

您可以使用下面的代码更轻松地添加参数;这是我使用的骨架,但我确信有更好的方法。

您仍然需要使用命名参数,但是您可以通过将它们命名为 @a、@b、@c 来在一定程度上模拟您的问号。 - 位置参数很好,直到您获得多个参数并且您必须不断地计算问号的数量来确定在何处应用哪个参数值,这通常会导致错误。

using (var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
    con.Open();
    {
        using (var command = con.CreateCommand())
        {
            command.Connection = conn;
            command.CommandText = "SELECT * FROM [dbo].[Table] WHERE [c1] = @a AND [c2] = @b";
            command.Parameters.AddWithValue("@a", aVal);
            command.Parameters.AddWithValue("@b", bVal);
            command.CommandType = CommandType.Text;

            using (var reader = command.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ///
                    }
                }
                else
                {
                    ///
                }
            }
        }
    }
}

edit: as pointed out by Dana the MSDN Docs for Parameters shows you need to use named parameters for SqlClient but can use positional parameters for OleDb/ODBC.

You can make adding parameters a lot easier by using the code below; it's the skeleton I use but I'm sure there's a better way of doing it.

You still need to used named parameters, but you can simulate your question marks to an extent by naming them @a, @b, @c.. - positional parameters are fine until you get more than a handful of parameters and you have to constantly count the number of question marks to figure out which parameter value is being applied where, often resulting in mistakes.

using (var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
    con.Open();
    {
        using (var command = con.CreateCommand())
        {
            command.Connection = conn;
            command.CommandText = "SELECT * FROM [dbo].[Table] WHERE [c1] = @a AND [c2] = @b";
            command.Parameters.AddWithValue("@a", aVal);
            command.Parameters.AddWithValue("@b", bVal);
            command.CommandType = CommandType.Text;

            using (var reader = command.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ///
                    }
                }
                else
                {
                    ///
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文