Learning from ADO (ii)

Source: Internet
Author: User

Learn to connect to a database using connection objects today

    • SQL Server. NET Data provider is the SqlConnection of the link object.

Common parameters and descriptions of database connection strings

    1. Connection Timeout: The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an exception. The default value is 15 seconds.
    2. Initial catalog or database: Names of databases.
    3. Data souse or server: The name of the SQL Server used when the connection is opened, or the file name of a Microsoft Access database.
    4. The login password for the password or pwd:sql Server account.
    5. User ID or uid:sql server login account.
    6. Integrated Security: The secondary parameter determines whether the connection is a secure connection, the possible values are true, FALSE, and SSPI (SSPI is synonymous with true)

The SqlConnection class is used to establish a class that is connected to a SQL Server server with the following syntax format:

SqlConnection con = new SqlConnection ("server= server name; User id= users; pwd= password; database= database name ");

            //Create a string to connect to the database            stringSqlstr ="server= (local); User Id=sa; Pwd=123456;database=xiaohudui"; //Create a SqlConnection object//set the string for the SqlConnection object connection databaseSqlConnection con =NewSqlConnection (SQLSTR); //Open Databasecon. Open ();
.... //Close the databaseCon. Close ();

Skills

(1) Configuring a string to connect to a database in the Web. config file

<configuration>  <appsettings >    <add key="Conn" value= " server= (local);d atabase=xiaohudui;uid=sa;pwd=123456 "/>  </appSettings></configuration>

(2) Gets the string in the Web. config file that is connected to the database

         Public SqlConnection getconnection ()        {            // Get connection string            in Web. config file string mystr = configurationmanager.appsettings["Conn"]. ToString ();             New SqlConnection (mystr);             return myconn;        }

Learning from ADO (ii)

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.