In Asp.net, the Web. config connection string and the configuration database sqlserver,

Source: Internet
Author: User

In Asp.net, the Web. config connection string and the configuration database sqlserver,

In ASP. NET web. config, you can write the configuration of the connection string in two ways.

<configuration>   <appSettings>      <add key="connstr1" value="Data Source=.;Initial Catalog=DBName;Integrated Security=true"/>      <add key="connstr2" value=".........">   </appSettings>   <connectionStrings>      <add name="connstr3" connectionString="........" />      <add name="connstr4" connectionString="......" providerName="System.Data.Sqlclient"   </connectionStrings></configuration>

As shown in the code above: two methods are available: appSettings and connectionStrings.

AppSettings:

① It is used in asp. net1.1 and in vs2003

② The key-value pairs are saved in the form of keys and values. You can not only store connection strings, but also some configuration items. The value includes the database server address, user name and password, and database name.

③ ProviderName = "System. data ....... "(if you want to use it, you only need to write it in the value, and pass it as a value)

④ Use the code in the background value mode:

 string conn=System.Configuration.ConfigurationManager.AppSettings["connstr";]

ConnectionStrings:

① It is added in asp. net2.0.

② The key-value pairs are also stored in the format of name and connectionString. Generally, the connection string is saved.

③ In connectionStrings, you can use providerName.

④ In the background code, the value method is as follows:

string conn=System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

  Other Instructions:

① Initial Catalog = Database is the Database name. According to the information collected from the network, the two seem to be completely common and basically have no difference.
② Integrated Security option settings of Integrated Security, which can be identified as true, false, yes, no, and sspi equivalent to true. If this parameter is set to false, the user ID and password are specified in the connection. If this parameter is set to true, the current Windows Account creden。 are used for authentication. SSPI is the Security Supoort Provider Interface, that is, the Security support Provider Interface. The Microsoft Security Support Provider Interface (SSPI) is a fully-defined public API, it is used to obtain integrated security services such as verification, information integrity, and information privacy, as well as security services for all distributed application protocols. The application protocol designer can use this interface to obtain different security services without modifying the Protocol itself. In the SQL Server database connection string, Integrated Security = SSPI indicates that Windows authentication is used, that is, the user running the current application is used to connect to SQL Server. For the web page, it is generally an anonymous user in IIS.
 
 

Since connectionStrings comes out of connectionStrings 2.0, it must be better than appsettings.:

The Internet says:

① You can encrypt the connection string by using an encryption tool of MS.
② Data source controls that can be directly attached without having to write code to read them and then assign them to the controls.
③ You can easily change the database platform. For example, if you change to an Oracle database, you only need to modify the providerName.

 

What is the role of providerName?

Let's take a look at the parameter value of providerName.

① ProviderName = "System. data. sqlClient "---- indicates that MSSQLServer database ② providerName =" System. data. sqlLite "---- indicates that SQLLite database ③ providerName =" System. data. oracleClient "---- indicates that Oracle database or providerName =" System. data. oracle. dataAccess. client "---- same as ④ providerName =" System. data. oleDb "---- indicates that the Access database is used.

ProviderName can be written or not.

When can we use providerName?

For example, if we want to create A project, we will sell two Enterprises: A and B. There are uncertainties. A uses Oracle and B uses SQLserver.

So,

① Database: we need to create two databases: oracle and SQL Server.

② Program: we generally do not write two systems for them to use. We will certainly judge what database they are using first, and then execute what database scripts in the program.

③ Web. config code:

<configuration>  <connectionStrings>    <add name="connStr" connectionString="Data Source=.;Initial Catalog=mydb;Integrated Security=true" providerName="System.Data.SqlClient"/>  </connectionStrings></configuration>

  

④ Program code: make a judgment. If providerName = "System. Data. SqlClient" is used, the SQL server script is executed. If providerName = "System. Data. OracleClient" is used, the Oracle database script is called.

Public static readonly string connStr = System. configuration. configurationManager. connectionStrings ["connStr"]. providerName; public static string databaseType = System. configuration. configurationManager. connectionStrings ["connStr"]. providerName; public static int ExecuteNonQuery (CommandType commandType, string commandText, params System. data. oleDb. oleDbParameter [] parm) {int num = 0; if (databaseType = "System. data. sqlClient ") {// run the Microsoft SQLServer database script here} else if (databaseType =" System. data. oracleClient ") {// execute the Oracle database script here} return num ;}

 

Common Database Connection code (C #):
SqlConnection conn = new SqlConnection( “Server=(local);Integrated Security=SSPI;database=Pubs“);SqlConnection conn = new SqlConnection(“server=(local)\NetSDK;database=pubs;Integrated Security=SSPI“);SqlConnection conn = new SqlConnection(“Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;“);SqlConnection conn = new SqlConnection(“ data source=(local);initial catalog=xr;integrated security=SSPI;persist security info=False;workstation id=XURUI;packet size=4096; “);SqlConnection myConn  = new System.Data.SqlClient.SqlConnection(“Persist Security Info=False;IntegratedSecurity=SSPI;database=northwind;server=mySQLServer“);SqlConnection conn = new SqlConnection( “ uid=sa;pwd=passwords;initial catalog=pubs;data source=127.0.0.1;Connect Timeout=900“); 

  

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.