@ -31,136 +31,132 @@ namespace PSqlSeverWrapper
if ( ! CheckBasicInfo ( ) )
throw new ArgumentException ( "Invalid connection info" ) ;
try
{
return new SqlConnection ( ConnString ) ;
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
}
}
}
public static void ExecuteQuery ( string query )
{
try
{
using ( SqlConnection conn = Connection )
{
conn . Open ( ) ;
SqlCommand command = new SqlCommand ( query , conn ) ;
conn . Open ( ) ;
command . ExecuteNonQuery ( ) ;
Connection . Close ( ) ;
}
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
conn . Close ( ) ;
}
}
public static void ExecuteQuery ( SqlConnection openedConn , string query )
{
try
{
SqlCommand command = new SqlCommand ( query , openedConn ) ;
command . ExecuteNonQuery ( ) ;
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
}
}
public static void ExecuteQueryAsync ( SqlConnection openedConn , string query )
{
try
{
SqlCommand command = new SqlCommand ( query , openedConn ) ;
command . ExecuteNonQueryAsync ( ) ;
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
}
}
public static void ExecuteQuery ( string [ ] queries )
{
try
{
using ( SqlConnection conn = Connection )
{
conn . Open ( ) ;
ExecuteQuery ( conn , "BEGIN TRAN;" ) ;
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 ( ) ;
}
ExecuteQuery ( conn , "COMMIT TRAN;" ) ;
Connection . Close ( ) ;
}
trans . Commit ( ) ;
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
Debug . WriteLine ( ex ) ;
trans . Rollback ( ) ;
}
conn . Close ( ) ;
}
}
public static void ExecuteQueryAsync ( string query )
public static void BulkCopy ( string tableName , DataTable table , string [ ] dbColumnMap = null )
{
try
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 ( ) ;
SqlCommand command = new SqlCommand ( query , conn ) ;
command . ExecuteNonQueryAsync ( ) ;
Connection . Close ( ) ;
sqlBulkCopy . WriteToServerAsync ( table ) ;
conn . Close ( ) ;
}
}
catch ( Exception ex )
public static void ExecuteQueryAsync ( string query )
{
using ( SqlConnection conn = Connection )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
SqlCommand command = new SqlCommand ( query , conn ) ;
conn . Open ( ) ;
command . ExecuteNonQueryAsync ( ) ;
conn . Close ( ) ;
}
}
public static DataSet ExecuteSelectQuery ( string query )
{
try
{
using ( SqlConnection conn = Connection )
{
SqlDataAdapter adapter = new SqlDataAdapter ( query , conn ) ;
DataSet dataSet = new DataSet ( ) ;
conn . Open ( ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( query , conn ) ;
adapter . Fill ( dataSet ) ;
Conne cti on. Close ( ) ;
con n . Close ( ) ;
return dataSet ;
}
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
}
}
public static DataSet ExecuteSelectQuery ( SqlConnection openedConn , string query )
{
try
{
DataSet dataSet = new DataSet ( ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( query , openedConn ) ;
@ -168,16 +164,8 @@ namespace PSqlSeverWrapper
return dataSet ;
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
}
}
public static SqlDataAdapter GetAdapterSelectQuery ( SqlConnection openedConn , string query , bool withCommandBuilder = true )
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter ( query , openedConn ) ;
if ( withCommandBuilder )
@ -190,12 +178,6 @@ namespace PSqlSeverWrapper
return adapter ;
}
catch ( Exception ex )
{
Debug . WriteLine ( ex . ToString ( ) ) ;
throw ;
}
}
private static bool CheckBasicInfo ( )
{