So I’m not sure if there is a better way to do this, but here is my current setup.
- Created an XSD file that represents my table structure from my database.
- Created a class file to hold my functions such as the one below.
Code sample:
Public Function GetUser(ByVal UserID As String) As xsdUser.UserDataTable
Dim SqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString)
Dim SqlCom = New SqlCommand("User_Retrieve", SqlConn)
Dim Adapter As SqlDataAdapter
Dim UserDT As New xsdUser.UserDataTable
Using SqlCom
SqlCom.Parameters.Add(New SqlParameter("@I_UserID", SqlDbType.NVarChar, 4)).Value = UserID
Adapter = New SqlDataAdapter(SqlCom)
Adapter.Fill(UserDT)
End Using
If UserDT IsNot Nothing AndAlso UserDT.Rows.Count > 0 Then
'Return DataTable here or I guess DataRow since there should just be one.
Else
'Handle Error here if there is no row
End If
End Function
Now it seems odd to me to have two files doing what should be contained within just one file. I feel like I should have one class file called “User” and have member variables in there along with the class functions like the one above. But all the tutorials I see are using these data tables. On top of that I’m not sure how to handle the if statement at the end of the function. I don’t think I would want to return a whole DataTable since I should only have one row, and I also don’t know how I would handle an error if my function is supposed to return a data table. I suppose I could return an empty one and then if the table is empty in my code then I would handle it there, but this also seems messy.
So can anyone clear up some of these things? Is this the common way of retrieving information from a stored procedure? Or is there a more up-to-date method that I’m not seeing in my search results?
Thanks!
Returning the UserDataTable is perfectly acceptable. Even if it contains just one row.
The only thing that seem out of order is your check the result inside this method that belongs to a lower layer than the user interface. Also note that you don’t need to check if the UserDataTable is nothing.
And I think this line is required