Database connection string

Source: Internet
Author: User

One: You can write directly:

String connectionString = "data Source = (local); Database = TEMP; Integrated Security = true ";


Two: can be written using SqlConnectionStringBuilder:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder ();

Builder. DataSource = "";
Builder. InitialCatalog = "";
Builder. IntegratedSecurity = true;

String connectionString = Builder. ConnectionString;


Three: You can also write in the Web. config file:

First introduce a namespace:

Using System.Configuration;

Web. config:

<connectionStrings>
<add name= "connectionString" connectionstring= "data Source = (local); Database = TEMP; Integrated Security = True "/>
</connectionStrings>


Connection string:

String connectionString = configurationmanager.connectionstrings["connectionString"]. ConnectionString;


This method is written, you can modify the database account password at any time.


IV: Another Web. config file is written:

First, write the following code in the configuration file, Web. config, and note that the connection string is different from the original writing:

1 <connectionStrings>
2 <clear/>
3 <add name= "partialconnectstring"
4 connectionstring= "Initial catalog=northwind;"
5 providername= "System.Data.SqlClient"/>
6 </connectionStrings>

Where the ProviderName field is written, which is different from the original.

You can then write the connector in the. cs file:

You can customize a connection string method Buildconnectionstring ():

1 public string void buildconnectionstring (String datasource,string userName, String userpassword)
2 {
3 Connectionstringsettings settings =
4 configurationmanager.connectionstrings["partialconnectstring"];
5
6 if (null! = settings)
7 {
8 String connectstring = settings. ConnectionString;
9
Ten SqlConnectionStringBuilder builder =
One new SqlConnectionStringBuilder (connectstring);
12
Builder. DataSource = DataSource;
Builder. UserID = UserName;
Builder. Password = UserPassword;
Return (builder. ConnectionString);
17}
18}

Where, the data source, user name, and password are passed in. The connection string is then returned.

This is how you write a connection string using a configuration file.


V: Prevents parameter injection of the connection string:

Use SqlConnectionStringBuilder to handle the additional values inserted by the Initial Catalog settings:

1 System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder () ;
2 builder["Data Source"] = "(local)";
3 builder["Integrated Security"] = true;
4 builder["Initial Catalog"] = "AdventureWorks; Newvalue=bad ";
5 string connectionString = Builder. ConnectionString;

Among them,; Newvalue=bad is the other string that is injected, and if it runs directly without processing, an error occurs. In this case, you can construct the link string using Builder.

Database connection string

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.