ADO.NET对象的构造(7)_OleDbParameter(下)

类别:.NET开发 点击:0 评论:0 推荐:

n          Overloads Public Overridable Function Add(ByVal value As Object) As Integer Implements IList.Add

n          Overloads Public Function Add(ByVal parameterName As String,ByVal value As Object) As OleDbParameter

n          Overloads Public Function Add(ByVal value As OleDbParameter) As OleDbParameter

n          Overloads Public Function Add(ByVal parameterName As String,ByVal oleDbType As OleDbType) As OleDbParameter

n          Overloads Public Function Add(ByVal parameterName As String,ByVal oleDbType As OleDbType,ByVal size As Integer) As OleDbParameter

n          Overloads Public Function Add(ByVal parameterName As String,ByVal oleDbType As OleDbType,ByVal size As Integer,ByVal sourceColumn As String) As OleDbParameter

 

示例

Public Sub CreateOleDbParamColl(myConn As OleDbConnection)

  Dim myCommand As OleDbCommand = New OleDbCommand("SELECT * FROM Customers WHERE CustomerID = ?", myConn)

  Dim myParamCollection As OleDbParameterCollection = myCommand.Parameters

 

  Dim myParm As Object = New OleDbParameter("CustomerID", OleDbType.VarChar)

  Dim pIndex As Integer = myParamCollection.Add(myParm)

 

  Dim myParm As OleDbParameter = myParamCollection.Add(New OleDbParameter("CustomerID", OleDbType.VarChar))

 

  Dim myParm As OleDbParameter = myParamCollection.Add("CustomerID", “CustomerIDValue”);

 

  Dim myParm As OleDbParameter = myParamCollection.Add("CustomerID", OleDbType.VarChar)

 

  Dim myParm As OleDbParameter = myParamCollection.Add("CustomerID", OleDbType.VarChar, 5)

 

  Dim myParm As OleDbParameter = myParamCollection.Add("CustomerID", OleDbType.VarChar, 5, "CustomerID")

 

End Sub

 

示例

Public Shared Function CreateCustomerAdapter(conn As OleDbConnection) As OleDbDataAdapter

  Dim da As OleDbDataAdapter = New OleDbDataAdapter()

  Dim cmd As OleDbCommand

  Dim parm As OleDbParameter

 

  ' Create the SelectCommand.

  cmd = New OleDbCommand("SELECT * FROM Customers WHERE Country = @Country AND City = @City", conn)

  cmd.Parameters.Add("@Country", OleDbType.VarChar, 15)

  cmd.Parameters.Add("@City", OleDbType.VarChar, 15)

  da.SelectCommand = cmd

 

  ' Create the InsertCommand.

  cmd = New OleDbCommand("INSERT INTO Customers (CustomerID, CompanyName) VALUES (@CustomerID, @CompanyName)", conn)

  cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")

  cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName")

  da.InsertCommand = cmd

 

  ' Create the DeleteCommand.

  cmd = New OleDbCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", conn)

  parm = cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")

  parm.SourceVersion = DataRowVersion.Original

  da.DeleteCommand = cmd

 

  ' Create the UpdateCommand.

  cmd = New OleDbCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName WHERE CustomerID = @oldCustomerID", conn)

  cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")

  cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName")

  parm = cmd.Parameters.Add("@oldCustomerID", OleDbType.Char, 5, "CustomerID")

  parm.SourceVersion = DataRowVersion.Original

  da.UpdateCommand = cmd

 

  Return da

End Function

 

以下两行

parm = cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")

parm.SourceVersion = DataRowVersion.Original

可以缩写为

cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original

类似地当添加参数后马上进行设置参数的值时,也可以进行类似以下缩写

myDataAdapter.SelectCommand.Parameters.Add("@CategoryName", OleDbType.VarChar, 80).Value = "toasters"

myDataAdapter.SelectCommand.Parameters.Add("@SerialNum", OleDbType.Integer).Value = 239

(信息整理来自MSDN)

本文地址:http://com.8s8s.com/it/it45021.htm