asp.net中OdbcParameter的问题

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 Language Filter 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.

Send comments on this topic.

© Microsoft Corporation. All rights reserved.

转载于:https://www.cnblogs.com/jybuding/archive/2009/11/26/1611070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值