Command and parameters

F

Frédéric Mayot

Hi,
I'm trying to make a little wrapper to execute sql commands with parameters.

Execute("SELECT * FROM Table WHERE Stuff=@p1", somevar)

Public Shared Function Execute(ByVal sSQL As String, _
ByVal ParamArray params() As Object) As
Object
[...]
Dim oCmd As New SqlCommand(sSQL, oSQLConn)
[...]
For Each obj As Object In params
Dim param As New SqlParameter
param.Value = obj
oCmd.Parameters.Add(param)
Next
[...]
End Function

I don't want to pass the name of the parameters to the Execute function.
It seems that I can't do that.
Any idea ?

Thanks.
Fred
 
C

Cor Ligthert

"Frédéric

You should provide in this case all the parameter properties (types etc).

I make in this case just an overloaded Fucntion one with a simple
selectstring and one with a complete command and add to that the connection
in what is with you the execute.function with

cmd.connection = myconnectionstring

As a second point is that I would transfer the objects as original classes.

By instance this is in my opinion much nicer

Public Shared Function Execute(ByVal sSQL As String, _
ByVal ParamArray params() As SqlParameter) As WhateverClass

I hope this helps?

Cor
 
G

Guest

TRY THIS
Execute("SELECT * FROM Table WHERE Stuff=@p1", ctype(nothing,sqlparameter()))

Hi,
I'm trying to make a little wrapper to execute sql commands with parameters.

Execute("SELECT * FROM Table WHERE Stuff=@p1", somevar)

Public Shared Function Execute(ByVal sSQL As String, _
ByVal ParamArray params() As Object) As
Object
[...]
Dim oCmd As New SqlCommand(sSQL, oSQLConn)
[...]
For Each obj As Object In params
Dim param As New SqlParameter
param.Value = obj
oCmd.Parameters.Add(param)
Next
[...]
End Function

I don't want to pass the name of the parameters to the Execute function.
It seems that I can't do that.
Any idea ?

Thanks.
Fred

User submitted from AEWNET (http://www.aewnet.com/)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top