aspnetvbnetnerd
Programmer
Is it possible for me to execute multiple stored procedure and returning SqlDataReader without having to open a connection to the database.
What I understand I have to open a connection to the database every time I want to executure a stored procedure. I'm I correct with this?
Class1
What I understand I have to open a connection to the database every time I want to executure a stored procedure. I'm I correct with this?
Code:
Private mDataAdapter As Class1
Private mSqlDrCustomerAssignment As SqlDataReader
Private mSqlDr As SqlDataReader
mDataAdapter = New Class1
Call mDataAdapter.Init()
mSqlDr = mDataAdapter.GetAllCustomer()
While mSqlDr.Read()
mSqlDrCustomerAssignment = mDataAdapter.GetCustomerAssignment(CType(mSqlDr.Item("CustomerID"), Integer))
While mSqlDrCustomerAssignment.Read()
End While
End While
Class1
Code:
Private mDbConn As SqlConnection
Private mSqlCmd As SqlCommand
Private mSqlDr As SqlDataReader
Public Overridable Sub Init()
mDbConn = New SqlConnection(ConnectionString)
mDbConn.Open()
End Sub
Public ReadOnly Property ConnectionString() As String
Get
Return "Data Source=(local);Initial Catalog=DAarchiving;User Id=sa;Password=ett2tre;"
End Get
End Property
Public Function GetAllCustomer() As System.Data.SqlClient.SqlDataReader
mSqlCmd = New SqlCommand("usp_GetAllCustomer", mDbConn)
mSqlCmd.CommandType = Data.CommandType.StoredProcedure
mSqlDr = mSqlCmd.ExecuteReader()
Return mSqlDr
End Function
Public Function GetCustomerAssignment(ByRef CustomerID As Integer) As System.Data.SqlClient.SqlDataReader
Dim mSqlCustomerAssignment As SqlDataReader
mSqlCmd = New SqlCommand("usp_GetCustomerAssignment", mDbConn)
mSqlCmd.CommandType = Data.CommandType.StoredProcedure
mSqlCmd.Parameters.Add("@CustomerID", Data.SqlDbType.Int, 5)
mSqlCmd.Parameters("@CustomerID").Value = CustomerID
mSqlCustomerAssignment = mSqlCmd.ExecuteReader() [COLOR=red]<-- There is already an open DataReader associated with this Command which must be closed first.[/color]
Return mSqlCustomerAssignment
End Function