B
bradmarcus
18 months ago I re-wrote an entire order entry system with an Access
2002 front end (original the system was all Access) and SQL Server
stored procedures for all data retrievals, updates, etct. This was to
prepare for a time when I could re-write the front-end in VB.net and
use all of the existing stored procedures. So, I started down the path
of moving the system to .NET a week ago. In my experimentation, I wrote
some code like the following to call a stored procedure, then read
through the results and parse the results for values in the form
controls, just to see if this will work:
Private Sub btnGetPersonID_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnGetPersonID.Click
Dim strConnectionString As String = "Data Source=Win2kserver;
Initial Catalog=BCORDERSYS;" & _
"User ID=XXXXXXX;PWD=XXXXXXXX"
Dim oSQLConn As SqlConnection = New SqlConnection()
Dim cmd As New SqlCommand
Dim custIdParam As New SqlParameter
Dim reader As SqlDataReader
'load parameter for the stored procedures
custIdParam.ParameterName = "@customerid"
custIdParam.SqlDbType = Int
custIdParam.Direction = ParameterDirection.Input
custIdParam.Value = CInt(Me.txtEmailAddress.Text)
cmd.Parameters.Add(custIdParam)
'set desired connection for the cmd object
cmd.Connection = oSQLConn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "procGetCustomer"
oSQLConn.ConnectionString = strConnectionString
oSQLConn.Open()
reader = cmd.ExecuteReader()
' Call Read before accessing data.
'This should only return one row, but checking for more.
If reader.HasRows Then
Do While reader.HasRows
While reader.Read()
'Load fields into labels
Me.lblFirstName.Text = reader("FirstName").ToString
Me.lblLastName.Text = reader("LastName").ToString
End While
reader.NextResult()
Loop
Else
MsgBox("Sorry Monkey! no records")
End If
' Call Close when done reading.
reader.Close()
oSQLConn.Close()
End Sub
This appears to work fine (although a lot of handpoked code), and I
would be happy to re-rewrite all the code to be similar to this as most
of the database work is done with the existing stored procedures.
My questions are:
1. Is this a good approach to take for the entire project? Basically,
loading the parameters, calling the stored procedures and loading
controls with those results.
2. Will a similar approach work for UPDATE, and INSERT stored
procedures, not just SELECT?
3. Basically, I am using the SQLDATAREADER like the old ADO
recordsets....is that right?
4. I am guessing that VB Express has a limitation that won't let you
connect to a remote SQL SERVER and use the GUI database tools?
2002 front end (original the system was all Access) and SQL Server
stored procedures for all data retrievals, updates, etct. This was to
prepare for a time when I could re-write the front-end in VB.net and
use all of the existing stored procedures. So, I started down the path
of moving the system to .NET a week ago. In my experimentation, I wrote
some code like the following to call a stored procedure, then read
through the results and parse the results for values in the form
controls, just to see if this will work:
Private Sub btnGetPersonID_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnGetPersonID.Click
Dim strConnectionString As String = "Data Source=Win2kserver;
Initial Catalog=BCORDERSYS;" & _
"User ID=XXXXXXX;PWD=XXXXXXXX"
Dim oSQLConn As SqlConnection = New SqlConnection()
Dim cmd As New SqlCommand
Dim custIdParam As New SqlParameter
Dim reader As SqlDataReader
'load parameter for the stored procedures
custIdParam.ParameterName = "@customerid"
custIdParam.SqlDbType = Int
custIdParam.Direction = ParameterDirection.Input
custIdParam.Value = CInt(Me.txtEmailAddress.Text)
cmd.Parameters.Add(custIdParam)
'set desired connection for the cmd object
cmd.Connection = oSQLConn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "procGetCustomer"
oSQLConn.ConnectionString = strConnectionString
oSQLConn.Open()
reader = cmd.ExecuteReader()
' Call Read before accessing data.
'This should only return one row, but checking for more.
If reader.HasRows Then
Do While reader.HasRows
While reader.Read()
'Load fields into labels
Me.lblFirstName.Text = reader("FirstName").ToString
Me.lblLastName.Text = reader("LastName").ToString
End While
reader.NextResult()
Loop
Else
MsgBox("Sorry Monkey! no records")
End If
' Call Close when done reading.
reader.Close()
oSQLConn.Close()
End Sub
This appears to work fine (although a lot of handpoked code), and I
would be happy to re-rewrite all the code to be similar to this as most
of the database work is done with the existing stored procedures.
My questions are:
1. Is this a good approach to take for the entire project? Basically,
loading the parameters, calling the stored procedures and loading
controls with those results.
2. Will a similar approach work for UPDATE, and INSERT stored
procedures, not just SELECT?
3. Basically, I am using the SQLDATAREADER like the old ADO
recordsets....is that right?
4. I am guessing that VB Express has a limitation that won't let you
connect to a remote SQL SERVER and use the GUI database tools?