using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.SqlClient ;
using System.Diagnostics ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace PSqlSeverWrapper
{
public class PSqlServer
{
public static string ServerName { get ; set ; }
public static string DatabaseName { get ; set ; }
public static string UserId { private get ; set ; }
public static string Password { private get ; set ; }
public static int MinPoolSize { get ; set ; } = 2 0 ;
public static int MaxPoolSize { get ; set ; } = 1 0 0 ;
public static int TimeOut { get ; set ; } = 1 5 ;
private static string ConnString
{
get { return $"server={ServerName}; database={DatabaseName}; uid={UserId}; pwd={Password}; min pool size = {MinPoolSize}; max pool size = {MaxPoolSize}; connection timeout = {TimeOut};" ; }
}
public static SqlConnection Connection
{
get
{
if ( ! CheckBasicInfo ( ) )
throw new ArgumentException ( "Invalid connection info" ) ;
return new SqlConnection ( ConnString ) ;
}
}
public static void ExecuteQuery ( string query )
{
using ( SqlConnection conn = Connection )
{
SqlCommand command = new SqlCommand ( query , conn ) ;
conn . Open ( ) ;
command . ExecuteNonQuery ( ) ;
conn . Close ( ) ;
}
}
public static void ExecuteQuery ( SqlConnection openedConn , string query )
{
SqlCommand command = new SqlCommand ( query , openedConn ) ;
command . ExecuteNonQuery ( ) ;
}
public static void ExecuteQueryAsync ( SqlConnection openedConn , string query )
{
SqlCommand command = new SqlCommand ( query , openedConn ) ;
command . ExecuteNonQueryAsync ( ) ;
}
public static void ExecuteQuery ( string [ ] queries )
{
using ( SqlConnection conn = Connection )
{
conn . Open ( ) ;
SqlTransaction trans = conn . BeginTransaction ( ) ;
try
{
for ( int i = 0 ; i < queries . Length ; i + + )
{
SqlCommand command = new SqlCommand ( queries [ i ] , conn ) ;
command . Transaction = trans ;
command . ExecuteNonQuery ( ) ;
}
trans . Commit ( ) ;
}
catch ( Exception ex )
{
Debug . WriteLine ( ex ) ;
trans . Rollback ( ) ;
}
conn . Close ( ) ;
}
}
public static void BulkCopy ( string tableName , DataTable table , string [ ] dbColumnMap = null )
{
using ( SqlConnection conn = Connection )
{
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy ( conn ) ;
sqlBulkCopy . DestinationTableName = tableName ;
if ( dbColumnMap ! = null & & table . Columns . Count = = dbColumnMap . Length )
{
for ( int i = 0 ; i < table . Columns . Count ; i + + )
{
string colName = table . Columns [ i ] . ColumnName ;
sqlBulkCopy . ColumnMappings . Add ( colName , dbColumnMap [ i ] ) ;
}
}
conn . Open ( ) ;
sqlBulkCopy . WriteToServer ( table ) ;
conn . Close ( ) ;
}
}
public static void BulkCopyAsync ( string tableName , DataTable table , string [ ] dbColumnMap = null )
{
using ( SqlConnection conn = Connection )
{
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy ( conn ) ;
sqlBulkCopy . DestinationTableName = tableName ;
if ( dbColumnMap ! = null & & table . Columns . Count = = dbColumnMap . Length )
{
for ( int i = 0 ; i < table . Columns . Count ; i + + )
{
string colName = table . Columns [ i ] . ColumnName ;
sqlBulkCopy . ColumnMappings . Add ( colName , dbColumnMap [ i ] ) ;
}
}
conn . Open ( ) ;
sqlBulkCopy . WriteToServerAsync ( table ) ;
conn . Close ( ) ;
}
}
public static void ExecuteQueryAsync ( string query )
{
using ( SqlConnection conn = Connection )
{
SqlCommand command = new SqlCommand ( query , conn ) ;
conn . Open ( ) ;
command . ExecuteNonQueryAsync ( ) ;
conn . Close ( ) ;
}
}
public static DataSet ExecuteSelectQuery ( string query )
{
using ( SqlConnection conn = Connection )
{
SqlDataAdapter adapter = new SqlDataAdapter ( query , conn ) ;
DataSet dataSet = new DataSet ( ) ;
conn . Open ( ) ;
adapter . Fill ( dataSet ) ;
conn . Close ( ) ;
return dataSet ;
}
}
public static DataSet ExecuteSelectQuery ( SqlConnection openedConn , string query )
{
DataSet dataSet = new DataSet ( ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( query , openedConn ) ;
adapter . Fill ( dataSet ) ;
return dataSet ;
}
public static SqlDataAdapter GetAdapterSelectQuery ( SqlConnection openedConn , string query , bool withCommandBuilder = true )
{
SqlDataAdapter adapter = new SqlDataAdapter ( query , openedConn ) ;
if ( withCommandBuilder )
{
SqlCommandBuilder builder = new SqlCommandBuilder ( adapter ) ;
adapter . UpdateCommand = builder . GetUpdateCommand ( ) ;
adapter . DeleteCommand = builder . GetDeleteCommand ( ) ;
adapter . InsertCommand = builder . GetInsertCommand ( ) ;
}
return adapter ;
}
private static bool CheckBasicInfo ( )
{
if ( string . IsNullOrEmpty ( ServerName ) )
return false ;
if ( string . IsNullOrEmpty ( DatabaseName ) )
return false ;
if ( string . IsNullOrEmpty ( UserId ) )
return false ;
if ( string . IsNullOrEmpty ( Password ) )
return false ;
return true ;
}
}
}