Asp. The various ways to connect a database in net
ways to connect to a SQL database: (i), create a connection string in Web. 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 = "testconnectionstring" connectionString = "Data source=.\sqlexpress;initial catalog=sample;integrated Security=true; Persist Security info=true; User Id=sa; password=123456 "providerName =" System.Data.SqlClient "/>(b), get the connection string in Aspx.cs: 1. String Strcon = system.configuration.configurationmanager.connectionstrings["sampleconnectionstring"]. ConnectionString; 2. String Strcon = system.configuration.configurationmanager.connectionstrings["sampleconnectionstring"]. ToString (); 3. String Strcon = system.configuration.configurationmanager.connectionstrings["testconnectionstring"]. ToString ();(c), 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 security=true; Persist Security info=true; User Id=sa; password=123456 "; 3. String Strcon = "server=.\\sqlexpress;database=sample;uid=sa;pwd=123456"; ways to connect to an Access database (Yx.mdb): (i), create a connection string in Web. config: <add name = "accessconnectionstring" connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=| Datadirectory|\yx.mdb "providerName =" System.Data.OleDb "/>(b), get the connection string in Aspx.cs: 1. String Strcon = system.configuration.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 are connecting to a database (DATABASE1.ACCDB) in the Access2007 version, connect using the following method: String Strcon = "Provider=microsoft.ace.oledb.12.0;data source=" + Server.MapPath ("~/app_data/database1.accdb"); |
Asp. The various ways to connect a database in net