(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 ");