Connection to SQL Server

Source: Internet
Author: User
Tags sql server books

The main challenge is to understand how the code that accesses the database works. the. NET
Technology that permits accessing a database from C # code is called ADO. net. Ado. net
Groups all. Net classes that are related to database access.
Ado. Net is a complex subject that requires a separate book by itself, so we'll cover just
Enough to help you understand how your business tier works. For more information about
Ado. net, refer to beginning C #2008 databases: from novice to professional (apress, 2008 ).
The Data Access class named genericdataaccess that you 'l write will make extensive use
Except ADO. NET features. The genericdataaccess class deals with accessing the database,
Executing stored procedures, and returning the retrieved data. This class will provide generic
Data access functionality for the business tier classes, which need to read or manipulate that
Data.
Each database operation always consists of three steps:
1. open a connection to the SQL Server database.
2. Perform the needed operations with the database and get back the results.
3. Close the connection to the database.

Before you implement the genericdataaccess class itself, which implements all these
Steps, we'll have a quick look at each step individually.

■ Tip always try to make the second step (executing the commands) as fast as possible. Keeping a data
Connection open for too long or having too connected database connections open at the same time is expensive
For your application's performance. The golden rule is to open the connection as late as possible, perform
Necessary operations, and then close it immediately.

The class used to connect to SQL Server is sqlconnection. When creating a new datab
Connection, you always need to specify at least three important pieces of data:
• The name of the SQL server instance you're re connecting
• The authentication information that will permit you to access the server
• The database you want to work
This connection data is grouped in a connection string, which needs to be passed to T
Sqlconnection object. The following code snippet demonstrates how to create and open
Database connection. (You don't need to type anything just yet-here we're re just explaining
Theory, and we'll put it into practice in a step-by-step exercise just a little bit later .)
// Create the connection object
Sqlconnection connection = new sqlconnection ();
// Set the connection string
Connection. connectionstring = "Server = (local) \ sqlexpress;" +
"User ID = balloonshop; Password = eCommerce;" +
"Database = balloonshop ";
// Open the connection
Connection. open ();

 

The code is fairly straightforward: You first create a sqlconnection object, then set its
Connectionstring property, and finally open the connection. A connection needs to be opened
Before it is used for any operations.
Understanding the connection string is important-if your program has problems connecting
To the database, these problems likely can be solved by fixing the connection string (assuming
That SQL Server is properly configured and that you actually have access to it ).
The connection string contains the three important elements. The first is the name of
SQL Server instance you're connecting to. for SQL Server 2008 express edition, the default
Instance name is (local) \ sqlexpress. You'll want to change this if your SQL server instance
Has another name. You can use your computer name instead of (local). Of course, if you
Connect to a remote SQL server instance, you'll need to specify the complete network path
Instead of (local ).
After specifying the server, you need to supply security information needed to log in to
Server. You can log in to SQL Server either by using SQL Server Authentication (in which case
You need to supply a SQL Server username and password as shown in the code snippet) or
By using Windows authentication (also named Windows integrated security). With windows
Integrated Security, you don't have to supply a username and password, because SQL Server
Uses the Windows login information of the currently logged-in user.

To log in using Windows authentication, you'll need to supply integrated security = true
(Or integrated security = sspi) instead of user id = username; Password = password. The final part
Of the connection string specifies the database you'll be working.
Instead of setting the connection string after creating the sqlconnection object, you can
Provide the connection string right when creating the sqlconnection object:
// Create the connection object and set the connection string
Sqlconnection connection = new sqlconnection ("... connection string ...");
// Open the connection
Connection. open ();
A final note about the connection string is that several synonyms can be used inside it;
Example, instead of server, you can use data source or data server, and instead of database,
You can use initial catalog. The list is much longer, and the complete version can be found
In SQL Server books online.

Not original, copied from ,:Beginning ASP. Net e-commerce in C # from novice to professional

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.