Connection Class
Connection class is essential component of ADO.Net. An application connect database with the connection class.There are two type of connection class, Sql connection and Oledb connection. SQL connection is for connecting Microsoft SQL server instance and Oledb for Acess ,Excel etc.
Instance of a connection class is created with a new operator. Connection string tell connection class to which server and database to connect. Open method create a channel between database and an application. Here is some most commonly used attribute of a connection class .
Connection pool : Connection pool increase the performance of an application. Instead of creating a new connection, connection pool try to reuse existing connection to reduce overhead and time. To use connection pull , you need to do pooling true and set Max pool size and Mean pool size properly. At the initial stage, SQL server create new connection when connection is required. When this connection are free , connection pool put them in a queue. When request for a connection comes, connection pool check its Queu, if there is any connection available , the existing connection assign immediately , either a new connection is established again . This makes an application is faster in performance as no connection established , resources and time saved, and as it is using the existing resources, no extra overhead on the application.
1)Example of Sql Coneection to connect a Sql Server
2) Example of Sql Coneection to connect a Sql Server with connection pull
Connection class is essential component of ADO.Net. An application connect database with the connection class.There are two type of connection class, Sql connection and Oledb connection. SQL connection is for connecting Microsoft SQL server instance and Oledb for Acess ,Excel etc.
Instance of a connection class is created with a new operator. Connection string tell connection class to which server and database to connect. Open method create a channel between database and an application. Here is some most commonly used attribute of a connection class .
string
ConncetionString = "Data
Source= xxx.xxx.xx.xx; initial catalog=xxxx; Connect
Timeout=6000; User ID= xxx;Password=xxxx;";
SqlConnection
con = new
SqlConnection(ConncetionString);
- Constructor
- SqlConnection : New instance of an connection class is created with parameter connection string, string connection , Sql credential integrated security to true user id password is needed.
- Property
- Connection String : Get or Set string to connect the database.
- Connection Timeout : Waiting time , will try to connect database, after that error is generated.
- Database: Get the name of a database, when the connection is open.
- Data sources : Get the name of the Sql server instance which to connect.
- Packet size : Network packet size in byte, while communicating with the data source.
- Server version : Get information about the version of SQL server.
- State : Get the state of SQL connection. Open close.
- Method
- Beginconnection : Starts transaction.
- Clear pools : All connection pool associate with the current connection is cleared.
- Close : Close a connection to the database.
- Dispose : Release all resources.
- Getschema : Return the schema of the data source
- Open : Open a connection to the database.
Connection pool : Connection pool increase the performance of an application. Instead of creating a new connection, connection pool try to reuse existing connection to reduce overhead and time. To use connection pull , you need to do pooling true and set Max pool size and Mean pool size properly. At the initial stage, SQL server create new connection when connection is required. When this connection are free , connection pool put them in a queue. When request for a connection comes, connection pool check its Queu, if there is any connection available , the existing connection assign immediately , either a new connection is established again . This makes an application is faster in performance as no connection established , resources and time saved, and as it is using the existing resources, no extra overhead on the application.
1)Example of Sql Coneection to connect a Sql Server
using
System;
using
System.Data;
using
System.Data.SqlClient;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
string
conStr = "Data
Source= ***.***.**.**; initial catalog=*****;Connect Timeout=6000;
User ID= *****;Password=****;";
string
cmd = "select
top 5 * from tbl_student";
using
(SqlConnection
con = new
SqlConnection(conStr))
{
SqlCommand
command =new
SqlCommand(cmd,
con);
con.Open();
SqlDataReader
reader = command.ExecuteReader();
DataTable
schemaTable = reader.GetSchemaTable();
foreach
(DataColumn
column in
schemaTable.Columns)
{
Console.WriteLine(column.ColumnName
+ ":"
+ schemaTable.Rows[0][0]);
}
reader.Close();
}
Console.ReadKey();
}
}
}
2) Example of Sql Coneection to connect a Sql Server with connection pull
using
System;
using
System.Data;
using
System.Data.SqlClient;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
string
conStr = "Data
Source= ****.***.***.***; initial catalog=*****;Connect
Timeout=6000; User ID= *****;Password=****;Min Pool
Size=0;Max Pool Size=100;Pooling=true;";
string
cmd = "select
top 5 * from tbl_member";
using
(SqlConnection
con = new
SqlConnection(conStr))
{
SqlCommand
command =new
SqlCommand(cmd,
con);
con.Open();
SqlDataReader
reader = command.ExecuteReader();
DataTable
schemaTable = reader.GetSchemaTable();
foreach
(DataColumn
column in
schemaTable.Columns)
{
Console.WriteLine(column.ColumnName
+ ":"
+ schemaTable.Rows[0][0]);
}
reader.Close();
}
Console.ReadKey();
}
}
}
No comments:
Post a Comment