Three ways to connect to a SQL Server database

Source: Internet
Author: User
Tags connectionstrings

Connection methods that involve configuration files:

Way One:

in the configuration file:<configuration> <appSettings> <add key="ConnectionString"Value="The name of the data source= server; Initial catalog= the name of the database to connect to; User id= Database login name; password= Database Login SecretCode"/></appSettings><configuration>to get the database connection string in your code: Public Static ReadOnly stringConnectionString = configurationmanager.appsettings["ConnectionString"];//ConnectionString is the value of the key in the configuration file

Way two:

in the configuration file:<configuration> <connectionStrings> <add name="MyDb"connectionstring="The name of the data source= server; Initial catalog= the name of the database to connect to; User id= Database login name; password= Database Login Password"ProviderName="System.Data.SqlClient"/> </connectionStrings><configuration>Get the database connection string in your code: Public Static ReadOnly stringstr = configurationmanager.connectionstrings["MyDb"]. ToString ();//MyDB is the value of name in the configuration file

Does not involve how the configuration file is connected:

The name of the new@ "data source= server; Initial catalog= the name of the database to which you want to connect; User id= Database login name; password= Database login Password ";

Three ways to connect to a SQL Server database

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.