简体   繁体   English

“?”在查询中的含义是什么?

[英]What does the “?” mean in a query?

In the following query, what does "?" 在以下查询中,“?”是什么 mean? 意思?

this.AdminDelCmd.CommandText =
    "DELETE FROM Admin WHERE (admincd = ?) AND (terminalno = ?)";

This is NOT a ternary operator, and is NOT an object of type Nullable. 这不是三元运算符,也不是Nullable类型的对象。

This is called a parameterized query and is used to help prevent SQL Injection. 这称为参数化查询,用于帮助防止SQL注入。 This is the 'older style' of SQL syntax. 这是SQL语法的“旧风格”。 This can be used when you want your queries to work with multiple different databases (such as MySQL & SQL Server). 当您希望查询与多个不同的数据库(例如MySQL和SQL Server)一起使用时,可以使用此方法。 The new style, also used for SQL Server (as was pointed out to me below) uses a '@' prepended to the parameter name. 同样用于SQL Server的新样式(如下面指出的那样)使用前缀为参数名称的“@”。 MySQL also uses '@' for server-side user variable declarations which can cause some confusion. MySQL还使用“@”作为服务器端用户变量声明,这可能会导致一些混淆。

Later code fills in the question marks. 后来的代码填写了问号。 If you could post the next few lines, that would help us more. 如果你可以发布下几行,那将有助于我们更多。

Here are some links to explain things more thoroughly (the second is for asp but applies): 这里有一些链接可以更彻底地解释事情(第二个是asp,但适用):
http://msdn.microsoft.com/en-us/library/cc296201(v=sql.90).aspx http://msdn.microsoft.com/en-us/library/cc296201(v=sql.90).aspx
http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/ http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/

These are positional parameters in your SQL query (as opposed to named parameters). 这些是SQL查询中的positional parameters (与命名参数相对)。

Your command should have one parameter for each positional parameter (?) in the same order as the positional parameters appear in the command text. 您的命令应该为每个位置参数(?)提供一个参数,其顺序与命令文本中显示的位置参数相同。

You generally use positional parameters 您通常使用位置参数

  • when your provider does not support named parameters. 当您的提供程序不支持命名参数时。 For example, many OleDb providers do not support named parameters. 例如,许多OleDb提供程序不支持命名参数。

  • or when you want interoperability with multiple providers. 或者当您想要与多个提供商进行互操作时。 Different providers may have different conventions for named parameters (eg SQL Server uses an @ prefix for the parameter name, but Oracle doesn't). 不同的提供程序可能对命名参数有不同的约定(例如,SQL Server对参数名称使用@前缀,但Oracle不使用)。 Therefore if you want to use the same query syntax for multiple providers, you are often better using positional parameters rather than named parameters. 因此,如果要对多个提供程序使用相同的查询语法,则通常更好地使用位置参数而不是命名参数。

Depending on the contents of the invisible query, it could be either a part of the ternary operator Asaph mentioned, or the shorthand for a Nullable<type> . 根据不可见查询的内容,它可以是Asaph提到的三元运算符的一部分,也可以是Nullable<type>的简写。

So if it says: 如果它说:

object.hasProperty ? "true" : "false";

it's the ternary operator; 它是三元运算符;

If it says: 如果它说:

int? anInt;

it means Nullable<int> anInt, and you can write anInt = null; 它意味着Nullable<int> anInt,你可以写anInt = null; , which comes very much in handy if you're reading from a database and have a column which accepts integer values, OR a NULL. ,如果你从数据库中读取并且有一个接受整数值的列,或者是一个NULL,它会派上用场。

that's menas that the value of the admincd AND terminalno will be resolved at runtime from the datasource. 这就是管理员AND terminalno的值将在运行时从数据源解析的结果。

i think your datasource would be a DataTable and your trying to update your database with 我认为您的数据源将是一个DataTable,并且您尝试使用更新数据库

DataApapter DataApapter

The "?" “?” mark is a placeholder for a parameter which will be specified later in the code. mark是参数的占位符,稍后将在代码中指定。 In this way your SQL command can be precompiled (parsed) at the beginning as a "prepared statement" and be faster later during execution when parameters are available. 通过这种方式,您的SQL命令可以在开始时作为“预准备语句”进行预编译(解析),并在参数可用时执行期间更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM