开发者

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.

This is called a parameterized query and is used to help prevent SQL Injection. This is the 'older style' of SQL syntax. This can be used when you want your queries to work with multiple different databases (such as 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. MySQL also uses '@' for server-side user variable declarations which can cause some confusion.

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):
http://msdn.microsoft.com/en-us/library/cc296201(v=sql.90).aspx
http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/


These are positional parameters in your SQL query (as opposed to named 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.

  • or when you want interoperability with multiple providers. Different providers may have different conventions for named parameters (e.g. SQL Server uses an @ prefix for the parameter name, but Oracle doesn't). 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>.

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;, which comes very much in handy if you're reading from a database and have a column which accepts integer values, OR a NULL.


that's menas that the value of the admincd AND terminalno will be resolved at runtime from the datasource.

i think your datasource would be a DataTable and your trying to update your database with

DataApapter


The "?" mark is a placeholder for a parameter which will be specified later in the code. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜