OdbcCommand不能使用定义名称的参数,使用?作为占位符。见下文红色部分
微软MSDN:http://msdn.microsoft.com/en-us/library/system.data.odbc.odbccommand.parameters(VS.71).aspx
.NET Framework Class Library
OdbcCommand.Parameters Property
Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.
Gets the OdbcParameterCollection.
[Visual Basic] Public ReadOnly Property Parameters As OdbcParameterCollection [C#] public OdbcParameterCollection Parameters {get;} [C++] public: __property OdbcParameterCollection* get_Parameters(); [JScript] public function get Parameters() : OdbcParameterCollection;
Property Value
The parameters of the SQL statement or stored procedure. The default is an empty collection.
Remarks
When CommandType is set to Text, the .NET Framework Data Provider for ODBC does not support passing named parameters to an SQL statement or to a stored procedure called by an OdbcCommand. In either of these cases, use the question mark (?) placeholder. For example:
SELECT * FROM Customers WHERE CustomerID = ?
The order in which OdbcParameter objects are added to the OdbcParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text. For more information, see Using Stored Procedures with a Command.
Note If the parameters in the collection do not match the requirements of the query to be executed, an error may result.
Example
[Visual Basic, C#, C++] The following example creates an OdbcCommand and displays its parameters. To accomplish this, the method is passed an OdbcConnection, a query string that is an SQL SELECT statement, and an array of OdbcParameter objects.
[Visual Basic] Public Sub CreateMyOdbcCommand(myConnection As OdbcConnection, _ mySelectQuery As String, myParamArray() As OdbcParameter) Dim myCommand As New OdbcCommand(mySelectQuery, myConnection) myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?" myCommand.Parameters.Add(myParamArray) Dim j As Integer For j = 0 To myCommand.Parameters.Count - 1 myCommand.Parameters.Add(myParamArray(j)) Next j Dim myMessage As String = "" Dim i As Integer For i = 0 To myCommand.Parameters.Count - 1 myMessage += myCommand.Parameters(i).ToString() + ControlChars.Cr Next i Console.WriteLine(myMessage) End Sub [C#] public void CreateMyOdbcCommand(OdbcConnection myConnection, string mySelectQuery, OdbcParameter[] myParamArray) { OdbcCommand myCommand = new OdbcCommand(mySelectQuery, myConnection); myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?"; myCommand.Parameters.Add(myParamArray); for (int j=0; j<myParamArray.Length; j++) { myCommand.Parameters.Add(myParamArray[j]) ; } string myMessage = ""; for (int i = 0; i < myCommand.Parameters.Count; i++) { myMessage += myCommand.Parameters[i].ToString() + "\n"; } MessageBox.Show(myMessage); } [C++] public: void CreateMyOdbcCommand(OdbcConnection* myConnection, String* mySelectQuery, OdbcParameter* myParamArray[]) { OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection); myCommand->CommandText = S"SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?"; myCommand->Parameters->Add(myParamArray); for (int j=0; j<myParamArray->Length; j++) { myCommand->Parameters->Add(myParamArray[j]); } String* myMessage = S""; for (int i = 0; i < myCommand->Parameters->Count; i++) { myMessage = String::Concat(myMessage, myCommand->Parameters->Item[i], S"\n"); } MessageBox::Show(myMessage); };
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
OdbcCommand Class | OdbcCommand Members | System.Data.Odbc Namespace | OdbcParameter
Syntax based on .NET Framework version 1.1.
Documentation version 1.1.1.