Learn to connect to a database using connection objects today
- SQL Server. NET Data provider is the SqlConnection of the link object.
Common parameters and descriptions of database connection strings
- Connection Timeout: The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an exception. The default value is 15 seconds.
- Initial catalog or database: Names of databases.
- Data souse or server: The name of the SQL Server used when the connection is opened, or the file name of a Microsoft Access database.
- The login password for the password or pwd:sql Server account.
- User ID or uid:sql server login account.
- Integrated Security: The secondary parameter determines whether the connection is a secure connection, the possible values are true, FALSE, and SSPI (SSPI is synonymous with true)
The SqlConnection class is used to establish a class that is connected to a SQL Server server with the following syntax format:
SqlConnection con = new SqlConnection ("server= server name; User id= users; pwd= password; database= database name ");
//Create a string to connect to the database stringSqlstr ="server= (local); User Id=sa; Pwd=123456;database=xiaohudui"; //Create a SqlConnection object//set the string for the SqlConnection object connection databaseSqlConnection con =NewSqlConnection (SQLSTR); //Open Databasecon. Open ();
.... //Close the databaseCon. Close ();
Skills
(1) Configuring a string to connect to a database in the Web. config file
<configuration> <appsettings > <add key="Conn" value= " server= (local);d atabase=xiaohudui;uid=sa;pwd=123456 "/> </appSettings></configuration>
(2) Gets the string in the Web. config file that is connected to the database
Public SqlConnection getconnection () { // Get connection string in Web. config file string mystr = configurationmanager.appsettings["Conn"]. ToString (); New SqlConnection (mystr); return myconn; }
Learning from ADO (ii)