ASP. NET database connection

Source: Internet
Author: User

(1) In the Web. create a connection string in Config: 1. <add name = "ConnectionString" connectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ grade. mdf; Integrated Security = True; User Instance = True "providerName =" System. data. sqlClient "/> 2. <add name =" sampleConnectionString "connectionString =" server =. \ SQLEXPRESS; database = sample; uid = sa; pwd = 123456; "providerName =" System. data. sqlClient "/> 3. <add name =" testConnect IonString "connectionString =" Data Source =. \ SQLEXPRESS; Initial Catalog = sample; Integrated Security = True; Persist Security Info = True; User ID = sa; Password = 123456 "providerName =" System. data. sqlClient "/> (2), in aspx. obtain the connection string in cs: 1. string strCon = System. configuration. configurationManager. connectionStrings ["sampleConnectionString"]. connectionString; 2. string strCon = System. configuration. configurationManager. C OnnectionStrings ["sampleConnectionString"]. toString (); 3. string strCon = System. configuration. configurationManager. connectionStrings ["testConnectionString"]. toString (); (3). Write the connection string directly to the page. 1. string strCon = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ grade. mdf; Integrated Security = True; User Instance = True "; 2. string strCon =" Data Source =. \ SQLEXPRESS; Initial Catalog = sample; Integrated Sec Urity = True; Persist Security Info = True; User ID = sa; Password = 123456 "; 3. string strCon =" server =. \ SQLEXPRESS; database = sample; uid = sa; pwd = 123456 "; connect to the Access database (yx. mdb) method: (1) in the Web. create a connection string in Config: <add name = "accessConnectionString" connectionString = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = | DataDirectory | \ yx. mdb "providerName =" System. data. oleDb "/> (2), in aspx. obtain the connection string in cs: 1. string strCon = System. configu Ration. configurationManager. connectionStrings ["accessConnectionString"]. toString (); 2. string strCon = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = | Datadirectory | \ yx. mdb "; 3, string strCon =" Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Server. mapPath ("~ /App_Data/yx. mdb "); Note: If you want to connect to the database (Database1.accdb) in Access2007, use the following method to connect: string strCon =" Provider = Microsoft. ace. OLEDB.12.0; Data Source = "+ Server. mapPath ("~ /App_Data/Database1.accdb ");

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.