First, Common properties
[1] ConnectionString Gets or sets the string that is used to open the SQL Server database. (Rewrite dbconnection.connectionstring. )
[2] ConnectionTimeout gets the time to wait before terminating an attempt and generating an error when attempting to establish a connection.
[3] Database gets the names of the databases to use when the current database or connection is opened.
[4] DataSource gets the name of the instance of SQL Server to connect to.
[5] PacketSize the size, in bytes, of the network packet used to communicate with an instance of SQL Server.
[6] ServerVersion gets a string that contains the version of the SQL Server instance of the client connection.
[7] State indicates the status of the SqlConnection when the network operation was recently performed on the connection.
[8] statisticsenabled if set to True, statistics collection is enabled for the current connection.
[9] Workstationid gets a string that identifies the database client.
Example:
Static voidMain (string[] args) { stringstr ="Data source= (local); Initial catalog=basicdb; User Id=sa; password=123;"; SqlConnection Conn=NewSqlConnection (str); //database name of the connection, return basicdb stringRETURN1 =Conn. Database; //The maximum attempt time to establish a connection (in seconds) is returned stringRETURN2 =Conn. ConnectionTimeout; //database connection string, return data source= (local); Initial catalog=basicdb; User Id=sa; password=123; stringRETURN3 =Conn. ConnectionString; //The value of the DB instance, returned (local), the value of the data source in the connection string stringRETURN4 =Conn. DataSource; //Size of the packet to be communicated with SQL Server, 8000 bytes returned intReturn5 =Conn. PacketSize; Conn. Open (); //to connect the SQL Server version information, return 10.50.1617,2008R2 stringReturn6 =Conn. ServerVersion; Conn. Close (); //The return value is an enumeration type, open opens, closed is closed stringRETURN7 = conn. State.tostring ();//Closed, closed//whether to count current connection information, true statistics, false not statistics BOOLRETURN8 =Conn. statisticsenabled; //Gets the client identity string, returning YIYI-PC stringReturn9 =Conn. Workstationid; }
Second, common methods
[1] Open () Connect
[2] Close () Closes the connection
[3] CreateCommand () Create SqlCommand object
[4] GetSchema () Get database schema information
Static voidMain (string[] args) { stringstr ="Data source= (local); Initial catalog=basicdb; User Id=sa; password=123;"; SqlConnection Conn=NewSqlConnection (str);//Create a connectionSqlCommand cmd = conn. CreateCommand ();//Create a SqlCommand objectConn. Open ();//Open ConnectionDataTable DT = conn. GetSchema ();//schema information for the output database foreach(DataRow Drinchdt. Rows) {Console.WriteLine (dr[0]); } conn. Close (); //Close ConnectionSqlconnection.clearpool (conn);//emptying the conn connection poolSqlconnection.clearallpools ();//Empty Connection PoolConsole.readkey (); }
SqlConnection Class of C #