|
-
September 19th, 2009, 07:47 PM
#1
Trouble passing parameters to array of sqlparameters
I have this code
Code:
Public Function ExecuteDataReaderSP( _
ByVal storedProcedureName As String, _
ByVal ParamArray arrParam() As SqlParameter) As SqlDataReader
Dim reader As SqlDataReader
' Open the connection
If m_cn.State <> ConnectionState.Open Then m_cn.Open()
' Define the command
Dim cmd As New SqlCommand
cmd.Connection = m_cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = storedProcedureName
' Handle the parameters
If arrParam IsNot Nothing Then
For Each param As SqlParameter In arrParam
cmd.Parameters.Add(param)
Next
End If
' Execute the reader
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return reader
End Function
how I pass parameters
im using ExecuteDataReaderSP("Name_Of_SP",parameter1,parameter2)
I try various ways to pass the parameters, every time it return a different error.
How is the correct way to pass those parameters?
thanks!
-
September 19th, 2009, 07:54 PM
#2
Re: Trouble passing parameters to array of sqlparameters
Sorry, but I have this in VB6, but it should help:
Code:
Option Explicit
Private Sub Form_Load()
ParamAdd
End Sub
Sub ParamAdd()
Dim cmdSQLInsert As ADODB.Command
Set cmdSQLInsert = New ADODB.Command
'Create the query
cmdSQLInsert.CommandText = "Insert Into Table1(ID, NAME, AGE) Values(?,?,?)"
cmdSQLInsert.CommandType = adCmdText
cmdSQLInsert.Prepared = True
'Create the parameters
'in this case we will create three parameters
'-----Param 1 (for Field ID)-------------
Dim gParam As ADODB.Parameter
Set gParam = New ADODB.Parameter
With gParam
.Name = "ID"
.Direction = adParamInput
.Type = adChar
.Size = 10
.Value = "xxxxxxxxxx"
End With
cmdSQLInsert.Parameters.Append gParam
'-----Param 2 (for Field Name)-------------
Set gParam = Nothing
Set gParam = New ADODB.Parameter
With gParam
.Name = "NAME"
.Direction = adParamInput
.Type = adVarChar
.Size = 50
.Value = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
End With
cmdSQLInsert.Parameters.Append gParam
'-----Param 3 (for Field AGE)-------------
Set gParam = Nothing
Set gParam = New ADODB.Parameter
With gParam
.Name = "AGE"
.Direction = adParamInput
.Type = adChar
.Size = 2
.Value = "xx"
End With
cmdSQLInsert.Parameters.Append gParam
'Set the connection property of the command object
Set cmdSQLInsert.ActiveConnection = mySQLConnection
'pass the values that need to be inserted to specific parameters that we created above
cmdSQLInsert("ID") = txtID.Text
cmdSQLInsert("NAME") = txtID.Text
cmdSQLInsert("AGE") = txtAge.Text
'Execute the command
cmdSQLInsert.Execute
End Sub
-
September 20th, 2009, 12:30 PM
#3
Re: Trouble passing parameters to array of sqlparameters
Thanks dglienna, I was trying to pass the parameters inline...now it works!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|