Danh mục

Using a SqlConnection Object to Connect to a SQL Server Database phần 1

Số trang: 7      Loại file: pdf      Dung lượng: 22.37 KB      Lượt xem: 13      Lượt tải: 0    
10.10.2023

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Using a SqlConnection Object to Connect to a SQL Server Database You create a SqlConnection object using the SqlConnection() constructor. This constructor is overloaded
Nội dung trích xuất từ tài liệu:
Using a SqlConnection Object to Connect to a SQL Server Database phần 1Using a SqlConnection Object to Connect to a SQL Server DatabaseYou create a SqlConnection object using the SqlConnection() constructor. Thisconstructor is overloaded, meaning that there are multiple versions of the constructor thatyou can call. The SqlConnection() constructors are as follows:SqlConnection()SqlConnection(string connectionString)where connectionString contains the details for the database connection. Youll learn thedetails of the connectionString in this section.Assuming youve imported the System.Data.SqlClient namespace, you can create a newSqlConnection object using the following statement:SqlConnection mySqlConnection = new SqlConnection();You can then set the details for the database connection using the ConnectionStringproperty of mySqlConnection. For example:mySqlConnection.ConnectionString = server=localhost;database=Northwind;uid=sa;pwd=sa;whereserver specifies the name of the computer on which SQL Server is running.database specifies the name of the database.uid specifies the name of the database user.pwd specifies the password for the user.Warning For security reasons, do not include the username password in your production code. Instead ask the user to enter their name and password-or use integrated security, which youll learn about shortly.One thing you need to bear in mind is that you can set the ConnectionString propertyonly when your Connection object is closed.You can also pass a connection string directly to the SqlConnection() constructor. Forexample:string connectionString = server=localhost;database=Northwind;uid=sa;pwd=sa; SqlConnection mySqlConnection = new SqlConnection(connectionString);Or more simply:SqlConnection mySqlConnection = new SqlConnection( server=localhost;database=Northwind;uid=sa;pwd=sa );You can set an optional connection timeout, which specifies the number of seconds thatthe Open() method will wait for a connection to the database. You do this by specifying aconnection timeout in your connection string. For example, the following string specifiesa connection timeout of 10 seconds:string connectionString = server=localhost;database=Northwind;uid=sa;pwd=sa; + connection timeout=10;Note The default connection timeout is 15 seconds. A connection timeout of 0 seconds means the connection attempt will wait indefinitely. Avoid setting your connection timeout to 0.Before starting a Windows session, you typically log in to Windows with a username andpassword. If youre using Windows integrated security, you can pass your username andpassword to SQL Server and use those credentials to connect to the database. This savesyou from providing a separate username and password to SQL Server. You can useintegrated security in your program by specifying integrated security=SSPI in yourconnection string. For example:string connectionString = server=localhost;database=Northwind;integrated security=SSPI;Notice that you dont provide the username and password. Instead, the username andpassword you used when logging into Windows is passed to SQL Server. SQL Serverwill then check its list of users to see if you have permission to access the database. (Forfurther details on integrated security, consult the SQL Server Books Onlinedocumentation.)Youve now seen how to create a Connection object using program statements. Youll seehow to create a Connection object visually using Visual Studio .NET later in theCreating a Connection Object using Visual Studio .NET section. Next, youll see howto open and close a connection.Opening and Closing a Database ConnectionOnce youve created your Connection object and set its ConnectionString property to theappropriate details for your database connection, you can open the connection to thedatabase. You do this by calling the Open() method of your Connection object. Thefollowing example calls the Open() method of mySqlConnection:mySqlConnection.Open();Once youve finished with your database connection, you call the Close() method of yourConnection object. For example:mySqlConnection.Close();Listing 7.1 illustrates how to connect to the SQL Server Northwind database using aSqlConnection object and display some of the properties of that object.Listing 7.1: MYSQLCONNECTION.CS/* MySqlConnection.cs illustrates how to use a SqlConnection object to connect to a SQL Server database*/using System;using System.Data;using System.Data.SqlClient;class MySqlConnection{ public static void Main() { // formulate a string containing the details of the // database connection string connectionString = server=localhost;database=Northwind;uid=sa;pwd=sa; // create a SqlConnection object to connect to the // database, passing the connection string to the constructor SqlConnection myS ...

Tài liệu được xem nhiều: