13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12: ADO.<strong>NET</strong>: Hello Database 297SqlConnection: The Command to Hook You UpYou’re familiar with connections to a web server, and connecting to a SQL server is thesame idea but a bit more involved. Using the SqlConnection <strong>com</strong>mand, you canconnect to your SQL server that’s built into Visual Studio 2008 or into the SQL Expressthat you can download from Microsoft. (This example uses the Visual Studio 2008application as a database target.)Depending on how your database is arranged, you have different ways to assign theSqlConnection parameters. For example, I have SQL server set up at a remote sitewith my database protected by a username and password. My connect parameter mustinclude the following:1. Server name (for example, myPlace.<strong>com</strong>)2. User ID (the username for the database)3. Password (the password for the database)4. Name of database (only the database name—not a table name)At the point of connection, I do not need the name of any tables. So when I get ready towork with my remote database, my focus is on the URL to the SQL server and database.I use the following:private SqlConnection hookUp;......hookUp = new SqlConnection("Server=myPlace.<strong>com</strong>;uid=uname; pwd=pass;database=zero");However, if using Visual Studio 2008, all I need is my localhost address and areference to my SqlExpress server built into Visual Studio 2008. My connection targetand database would beServer=localhost\SqlExpress;Database=VoteNow;However, because the expression will use a string, I have to place a backslash escapecharacter in front of the backslash (\\), making it appear as double backslashes. Also, in thedatabase creation process the logon selection is Windows Authentication (see Figure 12-1);the code must contain Integrated Security set to True to avoid a security problemwhen using Visual Studio 2008 on your <strong>com</strong>puter. So now the connection statement is thefollowing:hookUp = new SqlConnection("Server=localhost\\SqlExpress;Database=VoteNow;"+ "Integrated Security=True");Once that has been <strong>com</strong>pleted, the C# operation is set to connect to the database.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!