How To Connect to SQL using C# and SELECT INSERT UPDATE statement

Rajesh081725
Posted by Rajesh081725 under C# category on | Points: 40 | Views : 1811
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// This Class is specifically design to connect with optimized pooling to Microsoft Sql Server.
/// </summary>
public class MySqlConnection
{
public SqlConnection Connection;
public SqlDataReader DataReader;
public SqlCommand Command;
private string mySqlConnectionString = "server=.\\SQLExpress;database=myDatabase;User ID=myDatabaseUser;Password=123456;Trusted_Connection=True;";
public void CloseConn()
{
if (Connection != null)
{
if (Connection.State == ConnectionState.Open)
{
Connection.Close();
}
Connection.Dispose();
}
}
public SqlConnection CreateConn()
{
if (Connection == null) { Connection = new SqlConnection(); };
if (Connection.ConnectionString == string.Empty || Connection.ConnectionString == null)
{
try
{
Connection.ConnectionString = "Min Pool Size=5;Max Pool Size=40;Connect Timeout=4;" + mySqlConnectionString + ";";
Connection.Open();
}
catch (Exception)
{
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
}
Connection.ConnectionString = "Pooling=false;Connect Timeout=45;" + mySqlConnectionString + ";";
Connection.Open();
}
return Connection;
}
if (Connection.State != ConnectionState.Open)
{
try
{
Connection.ConnectionString = "Min Pool Size=5;Max Pool Size=40;Connect Timeout=4;" + mySqlConnectionString + ";";
Connection.Open();
}
catch (Exception)
{
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
}
Connection.ConnectionString = "Pooling=false;Connect Timeout=45;" + mySqlConnectionString + ";";
Connection.Open();
}
}
return Connection;
}
public MySqlConnection()
{

}
}

Comments or Responses

Login to post response