Answered by:
Best way to connect to SQL database

Question
-
User1733586255 posted
Hi,
I just want to know what is the difference and what is a good practice of connecting to database:
1. Using ASP.NET Expression:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Weekly_QAConnectionString %>" SelectCommand="SELECT * FROM [tbl_tests]"></asp:SqlDataSource>
or
2. sqlconnection:
Try myConnection.Open() Dim myCommand As New SqlCommand(mySelectQuery, myConnection) myConnection.Close() Catch Ex As Exception myConnection.Close() End Try
The former is very easy to write, visual studio creates ir automatically.Monday, October 7, 2013 8:12 AM
Answers
-
User-718146471 posted
While option #1 gives you the ease of drag and drop pretty much, the second block is actually better as it allows for separation of concerns. It is alwasy best practice to separate the visual from the business logic. Intermingling them can create confusion, plus the second option allows you to debug by setting break points in your code behind.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 7, 2013 8:22 AM -
User839260933 posted
Hi
This might help you
http://geekphilosopher.com/view/sqldatasource-vs-objectdatasource.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 7, 2013 8:23 AM
All replies
-
User-718146471 posted
While option #1 gives you the ease of drag and drop pretty much, the second block is actually better as it allows for separation of concerns. It is alwasy best practice to separate the visual from the business logic. Intermingling them can create confusion, plus the second option allows you to debug by setting break points in your code behind.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 7, 2013 8:22 AM -
User839260933 posted
Hi
This might help you
http://geekphilosopher.com/view/sqldatasource-vs-objectdatasource.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 7, 2013 8:23 AM -
User-1716253493 posted
Option1 is fastest way to build web form. Drag and drop, wizard, little modification. Very productive. It's to simplify writing code behind every time.Monday, October 7, 2013 8:34 AM