C
Cirene
I know that there are several ways to access/query data. (Stored procs,
direct SQL statements, etc...)
Typically I write a simple Stored Procedure, then access it from my webform
like this...
Dim MyConnection As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString)
MyConnection.Open()
Dim MyCommand As SqlCommand
Try
MyCommand = New SqlCommand("GetTimesFlaggedByThisUser",
MyConnection)
MyCommand.CommandType = Data.CommandType.StoredProcedure
MyCommand.Parameters.AddWithValue("@PostingId",
Request.QueryString("Id"))
MyCommand.Parameters.AddWithValue("@PostTitle", lblTitle.Text)
MyCommand.Parameters.AddWithValue("@FlaggedById",
Membership.GetUser(HttpContext.Current.User.Identity.Name.ToString).ProviderUserKey.ToString)
i = MyCommand.ExecuteScalar
Catch ex As Exception
lblStatus.Text = "ERROR WITH GetTimesFlaggedByThisUser: " &
ex.ToString
Exit Sub
Finally
MyConnection.Close()
End Try
I find that much of the code is redundant - the connection, declaring the
sqlcommand, the catch ex, etc...
Where do you all put this type/level of code? Do you usually just drop it
in a class/business object and access it like MyObj.GetTimesFlagged ?
Any suggestions. I'm always looking to do things better and using the more
"suggested" method.
Thanks!
direct SQL statements, etc...)
Typically I write a simple Stored Procedure, then access it from my webform
like this...
Dim MyConnection As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString)
MyConnection.Open()
Dim MyCommand As SqlCommand
Try
MyCommand = New SqlCommand("GetTimesFlaggedByThisUser",
MyConnection)
MyCommand.CommandType = Data.CommandType.StoredProcedure
MyCommand.Parameters.AddWithValue("@PostingId",
Request.QueryString("Id"))
MyCommand.Parameters.AddWithValue("@PostTitle", lblTitle.Text)
MyCommand.Parameters.AddWithValue("@FlaggedById",
Membership.GetUser(HttpContext.Current.User.Identity.Name.ToString).ProviderUserKey.ToString)
i = MyCommand.ExecuteScalar
Catch ex As Exception
lblStatus.Text = "ERROR WITH GetTimesFlaggedByThisUser: " &
ex.ToString
Exit Sub
Finally
MyConnection.Close()
End Try
I find that much of the code is redundant - the connection, declaring the
sqlcommand, the catch ex, etc...
Where do you all put this type/level of code? Do you usually just drop it
in a class/business object and access it like MyObj.GetTimesFlagged ?
Any suggestions. I'm always looking to do things better and using the more
"suggested" method.
Thanks!