|
|
|
Click here to copy the following block |
Imports System Imports System.Data Imports System.Data.SqlClient
Public MustInherit Class DbObject
Protected myConnection As SqlConnection Private myConnectionString As String
Public Sub New(ByVal newConnectionString As String) myConnectionString = newConnectionString myConnection = New SqlConnection(myConnectionString) End Sub
Protected Overloads Function RunProcedure(ByVal storedProcName As String, _ ByVal parameters As IDataParameter(), ByRef rowsAffected As Integer) As _ Integer Dim result As Integer
myConnection.Open() Dim command As SqlCommand = BuildIntCommand(storedProcName, parameters) rowsAffected = command.ExecuteNonQuery() result = CInt(command.Parameters("ReturnValue").Value) myConnection.Close()
Return result End Function
Protected Overloads Function RunProcedure(ByVal storedProcName As String, _ ByVal parameters As IDataParameter()) As SqlDataReader Dim returnReader As SqlDataReader
myConnection.Open() Dim command As SqlCommand = BuildQueryCommand(storedProcName, _ parameters) command.CommandType = CommandType.StoredProcedure
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection) Return returnReader End Function
Protected Overloads Function RunProcedure(ByVal storedProcName As String, _ ByVal parameters As IDataParameter(), ByVal tableName As String) As _ DataSet Dim dataSet As New DataSet() myConnection.Open() Dim sqlDA As New SqlDataAdapter() sqlDA.SelectCommand = BuildQueryCommand(storedProcName, parameters) sqlDA.Fill(dataSet, tableName) myConnection.Close() Return dataSet End Function Protected Overloads Sub RunProcedure(ByVal storedProcName As String, _ ByVal parameters As IDataParameter(), ByVal dataSet As DataSet, _ ByVal tableName As String) myConnection.Open() Dim sqlDA As New SqlDataAdapter() sqlDA.SelectCommand = BuildIntCommand(storedProcName, parameters) sqlDA.Fill(dataSet, tableName) myConnection.Close()
End Sub
Protected ReadOnly Property ConnectionString() As String Get Return myConnectionString End Get End Property
Private Function BuildIntCommand(ByVal storedProcName As String, _ ByVal parameters As IDataParameter()) As SqlCommand Dim command As SqlCommand = BuildQueryCommand(storedProcName, _ parameters) Dim parameter As New SqlParameter()
With parameter .ParameterName = "ReturnValue" .DbType = SqlDbType.Int .Size = 4 .Direction = ParameterDirection.ReturnValue .IsNullable = False .Precision = 0 .Scale = 0 .SourceColumn = String.Empty .SourceVersion = DataRowVersion.Default .Value = Nothing End With command.Parameters.Add(parameter)
Return command End Function
Private Function BuildQueryCommand(ByVal storedProcName As String, _ ByVal parameters As IDataParameter()) As SqlCommand Dim command As New SqlCommand(storedProcName, myConnection) command.CommandType = CommandType.StoredProcedure Dim parameter As SqlParameter For Each parameter In parameters command.Parameters.Add(parameter) Next
Return command End Function
End Class
Public Class Categories Inherits Wrox.WebModules.Data.DbObject
Public Sub New(ByVal newConnectionString As String) MyBase.New(newConnectionString) End Sub
Public Function GetCategories() As DataSet Return RunProcedure("sp_Forums_GetCategories", New IDataParameter() {}, _ "Categories") End Function
Public Function Add(ByVal categoryName As String, ByVal categoryImageUrl As _ String, ByVal categoryPosition As Integer) As Integer Dim rowsAffected As Integer
Dim parameters As SqlParameter() = { New SqlParameter("@CategoryName", _ SqlDbType.VarChar, 100), New SqlParameter("@CategoryImageUrl", _ SqlDbType.VarChar, 100), New SqlParameter("@CategoryPosition", _ SqlDbType.Int, 4), New SqlParameter("@CategoryID", SqlDbType.Int, _ 4)}
parameters(0).Value = categoryName.Trim() parameters(1).Value = categoryImageUrl.Trim() parameters(2).Value = categoryPosition parameters(3).Direction = ParameterDirection.Output
RunProcedure("sp_Forums_InsertCategory", parameters, rowsAffected)
Return CInt(parameters(3).Value) End Function
End Class
|
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|