If you get sick of typing quotes, ampersands, and underscores, you can combine
the three bold strings in the above code into a single string. However, Ill continue
to present connection strings as above throughout this booknot only are they
more readable that way, but they fit on the page, too!
If youre using C#, your code should look like this:
C# File: AccessingData.aspx (excerpt)
protected void Page_Load(object sender, EventArgs e)
{
// Define database connection
SqlConnection conn = new SqlConnection(
"Server=localhost\\SqlExpress;Database=Dorknozzle;" +
"Integrated Security=True");
}
Be aware that, in C#, the backslash (\) character has a special meaning when it
appears inside a string, so, when we wish to use one, we have to use the double
backslash (\\) shown above.
Preparing the Command
Now were at step three, in which we create a SqlCommand object and pass in our
SQL statement. The SqlCommand object accepts two parameters: the first is the
SQL statement, and the second is the connection object that we created in the
previous step.
Visual Basic File: AccessingData.aspx (excerpt)
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Define database connection
Dim conn As New SqlConnection("Server=localhost\SqlExpress;" & _
"Database=Dorknozzle;Integrated Security=True")
' Create command
Dim comm As New SqlCommand( _
"SELECT EmployeeID, Name FROM Employees", conn)
End Sub
C# File: AccessingData.aspx (excerpt)
protected void Page_Load(object sender, EventArgs e)
{
// Define database connection
SqlConnection conn = new SqlConnection(
"Server=localhost\\SqlExpress;Database=Dorknozzle;" +
336
Chapter 9: ADO.NET

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.