T
tshad
I am trying to setup a database class and curious how you would deal with
parameters?
Part of the class looks like:
***********************************************************************
'
' This Constructor accepts a connection string as input
' and makes a connection to that SQL Server.
'
Public Sub New(ByVal ConnectionString As String)
m_cnDB = New SqlConnection(ConnectionString)
m_cnDB.Open()
End Sub
'
' In case there are other objects that need the live connection,
' make it available through a read-only property.
'
Public ReadOnly Property Connection() As SqlConnection
Get
Return m_cnDB
End Get
End Property
'
' Run a query that does not return records
'
Public Function Execute(ByVal SQL As String) As Integer
Dim lngRecords As Integer
Dim cmdQuery As New SqlCommand()
cmdQuery.Connection = m_cnDB
cmdQuery.CommandText = SQL
cmdQuery.CommandType = CommandType.Text
lngRecords = cmdQuery.ExecuteNonQuery()
End Function
'
' Run a Stored Procedure that does not return records
'
Public Function ExecuteStoredProc(ByVal SQL As String) As Integer
Dim lngRecords As Integer
Dim cmdQuery As New SqlCommand()
cmdQuery.Connection = m_cnDB
cmdQuery.CommandText = SQL
cmdQuery.CommandType = CommandType.StoredProcedure
lngRecords = cmdQuery.ExecuteNonQuery()
End Function
*******************************************************
How do you handle parameters or can you?
Normally you would do something like:
****************************************************
Dim sQuery as String = "Update Vendor set FullName = @CompanyName where
VendorID = @VendorID"
Dim objCmd as New SqlCommand(sQuery,objConn)
with objcmd.Parameters
.Add("@VendorID",SqlDbType.bigInt).value = session("VendorID")
.Add("@CompanyName",SqlDbType.VarChar,30).value = CompanyName.text
end with
***************************************************************************
In my class, I would send sQuery to the class - but I can't do that here.
Thanks,
Tom
parameters?
Part of the class looks like:
***********************************************************************
'
' This Constructor accepts a connection string as input
' and makes a connection to that SQL Server.
'
Public Sub New(ByVal ConnectionString As String)
m_cnDB = New SqlConnection(ConnectionString)
m_cnDB.Open()
End Sub
'
' In case there are other objects that need the live connection,
' make it available through a read-only property.
'
Public ReadOnly Property Connection() As SqlConnection
Get
Return m_cnDB
End Get
End Property
'
' Run a query that does not return records
'
Public Function Execute(ByVal SQL As String) As Integer
Dim lngRecords As Integer
Dim cmdQuery As New SqlCommand()
cmdQuery.Connection = m_cnDB
cmdQuery.CommandText = SQL
cmdQuery.CommandType = CommandType.Text
lngRecords = cmdQuery.ExecuteNonQuery()
End Function
'
' Run a Stored Procedure that does not return records
'
Public Function ExecuteStoredProc(ByVal SQL As String) As Integer
Dim lngRecords As Integer
Dim cmdQuery As New SqlCommand()
cmdQuery.Connection = m_cnDB
cmdQuery.CommandText = SQL
cmdQuery.CommandType = CommandType.StoredProcedure
lngRecords = cmdQuery.ExecuteNonQuery()
End Function
*******************************************************
How do you handle parameters or can you?
Normally you would do something like:
****************************************************
Dim sQuery as String = "Update Vendor set FullName = @CompanyName where
VendorID = @VendorID"
Dim objCmd as New SqlCommand(sQuery,objConn)
with objcmd.Parameters
.Add("@VendorID",SqlDbType.bigInt).value = session("VendorID")
.Add("@CompanyName",SqlDbType.VarChar,30).value = CompanyName.text
end with
***************************************************************************
In my class, I would send sQuery to the class - but I can't do that here.
Thanks,
Tom