-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3b0027
commit 6b67135
Showing
10 changed files
with
635 additions
and
562 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace ACUtils | ||
{ | ||
public static class SqlDb_BulkInsert | ||
{ | ||
public static void BulkInsert<T>(this SqlDb self, string tablename, IEnumerable<T> records) | ||
{ | ||
using (var bc = new SqlBulkCopy(self.ConnectionString)) | ||
{ | ||
bc.WriteToServer(self.BulkInsertPrepare(bc, tablename, records)); | ||
} | ||
} | ||
public static async System.Threading.Tasks.Task BulkInsertAsync<T>(this SqlDb self, string tablename, IEnumerable<T> records) | ||
{ | ||
using (var bc = new SqlBulkCopy(self.ConnectionString)) | ||
{ | ||
await bc.WriteToServerAsync(self.BulkInsertPrepare(bc, tablename, records)); | ||
} | ||
} | ||
|
||
internal static DataTable BulkInsertPrepare<T>(this SqlDb self, SqlBulkCopy bc, string tablename, IEnumerable<T> records) | ||
{ | ||
bc.DestinationTableName = tablename; | ||
var properties = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); | ||
foreach (var property in properties) | ||
{ | ||
bc.ColumnMappings.Add(property.Name, property.Name); | ||
} | ||
return SqlDb.ToDataTable(records); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
|
||
namespace ACUtils | ||
{ | ||
public static class SqlDb_Execute | ||
{ | ||
public static bool Execute(this SqlDb self, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
//queryString = queryString.Trim().Replace(System.Environment.NewLine, " "); | ||
//queryString = System.Text.RegularExpressions.Regex.Replace(queryString, @"\s+", " "); | ||
using (SqlConnection connection = new SqlConnection(self.ConnectionString)) | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
var value = self.Execute(connection, queryString, queryParams); | ||
return value; | ||
} | ||
catch (Exception ex) | ||
{ | ||
self.WriteLog(ex, queryString, queryParams); | ||
throw; | ||
} | ||
finally | ||
{ | ||
try { connection.Close(); } catch { } | ||
} | ||
} | ||
} | ||
|
||
public static bool Execute(this SqlDb self, SqlConnection connection, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
//queryString = queryString.Trim().Replace(System.Environment.NewLine, " "); | ||
//queryString = System.Text.RegularExpressions.Regex.Replace(queryString, @"\s+", " "); | ||
using (SqlCommand selectCommand = SqlDb.generateCommand(connection, queryString, queryParams)) | ||
{ | ||
var value = selectCommand.ExecuteNonQuery() > 0; | ||
return value; | ||
} | ||
} | ||
|
||
public static bool Execute(this SqlDb self, string queryString, params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
using (SqlConnection connection = new SqlConnection(self.ConnectionString)) | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
var value = self.Execute(connection, queryString, queryParams); | ||
return value; | ||
} | ||
catch (Exception ex) | ||
{ | ||
self.WriteLog(ex, queryString, queryParams); | ||
throw; | ||
} | ||
finally | ||
{ | ||
try { connection.Close(); } catch { } | ||
} | ||
} | ||
} | ||
|
||
public static bool Execute(this SqlDb self, SqlConnection connection, string queryString, params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
using (SqlCommand selectCommand = SqlDb.generateCommand(connection, queryString, queryParams)) | ||
{ | ||
var value = selectCommand.ExecuteNonQuery() > 0; | ||
return value; | ||
} | ||
} | ||
|
||
public static bool Execute(this SqlDb self, string queryString) | ||
{ | ||
using (SqlConnection connection = new SqlConnection(self.ConnectionString)) | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
self.WriteLog(queryString); | ||
var value = self.Execute(connection, queryString); | ||
return value; | ||
} | ||
catch (Exception ex) | ||
{ | ||
self.WriteLog(ex, queryString); | ||
throw; | ||
} | ||
finally | ||
{ | ||
try { connection.Close(); } catch { } | ||
} | ||
} | ||
} | ||
|
||
public static bool Execute(this SqlDb self, SqlConnection connection, string queryString) | ||
{ | ||
using (SqlCommand selectCommand = SqlDb.generateCommand(connection, queryString)) | ||
{ | ||
return selectCommand.ExecuteNonQuery() > 0; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace ACUtils | ||
{ | ||
public static class SqlDb_QueryDataRow | ||
{ | ||
public static DataRow QueryDataRow(this SqlDb self, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
DataTable dt = self.QueryDataTable( | ||
queryString, | ||
queryParams | ||
); | ||
|
||
if (dt.Rows.Count == 0) | ||
{ | ||
throw new Exceptions.NotFoundException("nessun risultato ottenuto"); | ||
} | ||
|
||
if (dt.Rows.Count > 1) | ||
{ | ||
throw new Exceptions.TooMuchResultsException("ottenuto più valori"); | ||
} | ||
return dt.Rows[0]; | ||
} | ||
|
||
public static DataRow QueryDataRow(this SqlDb self, SqlConnection connection, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
DataTable dt = self.QueryDataTable( | ||
connection, | ||
queryString, | ||
queryParams | ||
); | ||
|
||
if (dt.Rows.Count == 0) | ||
{ | ||
throw new Exceptions.NotFoundException("nessun risultato ottenuto"); | ||
} | ||
|
||
if (dt.Rows.Count > 1) | ||
{ | ||
throw new Exceptions.TooMuchResultsException("ottenuto più valori"); | ||
} | ||
|
||
return dt.Rows[0]; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace ACUtils | ||
{ | ||
public static class SqlDb_QueryDataSet | ||
{ | ||
public static DataSet QueryDataSet(this SqlDb self, string queryString, params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
using (SqlConnection connection = new SqlConnection(self.ConnectionString)) | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
self.WriteLog(queryString, queryParams); | ||
var ds = self.QueryDataSet(connection, queryString, queryParams); | ||
return ds; | ||
} | ||
catch (Exception ex) | ||
{ | ||
self.WriteLog(ex, queryString, queryParams); | ||
throw; | ||
} | ||
finally | ||
{ | ||
try { connection.Close(); } catch { } | ||
} | ||
} | ||
} | ||
|
||
public static DataSet QueryDataSet(this SqlDb self, SqlConnection connection, string queryString, params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
using (SqlCommand selectCommand = SqlDb.generateCommand(connection, queryString, queryParams)) | ||
{ | ||
using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommand)) | ||
{ | ||
DataSet ds = new DataSet(); | ||
adapter.MissingSchemaAction = SqlDb.MissingSchemaAction; | ||
adapter.Fill(ds); | ||
return ds; | ||
} | ||
} | ||
} | ||
|
||
public static DataSet QueryDataSet(this SqlDb self, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
using (SqlConnection connection = new SqlConnection(self.ConnectionString)) | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
self.WriteLog(queryString, queryParams); | ||
var ds = self.QueryDataSet(connection, queryString, queryParams); | ||
return ds; | ||
} | ||
catch (Exception ex) | ||
{ | ||
self.WriteLog(ex, queryString, queryParams); | ||
throw; | ||
} | ||
finally | ||
{ | ||
try { connection.Close(); } catch { } | ||
} | ||
} | ||
} | ||
|
||
public static DataSet QueryDataSet(this SqlDb self, SqlConnection connection, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
using (SqlCommand selectCommand = SqlDb.generateCommand(connection, queryString, queryParams)) | ||
{ | ||
using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommand)) | ||
{ | ||
DataSet ds = new DataSet(); | ||
adapter.MissingSchemaAction = SqlDb.MissingSchemaAction; | ||
adapter.Fill(ds); | ||
return ds; | ||
} | ||
} | ||
} | ||
|
||
public static DataSet QueryDataSet(this SqlDb self, string queryString) | ||
{ | ||
using (SqlConnection connection = new SqlConnection(self.ConnectionString)) | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
self.WriteLog(queryString); | ||
var ds = self.QueryDataSet(connection, queryString); | ||
return ds; | ||
} | ||
catch (Exception ex) | ||
{ | ||
self.WriteLog(ex, queryString); | ||
throw; | ||
} | ||
finally | ||
{ | ||
try { connection.Close(); } catch { } | ||
} | ||
} | ||
} | ||
|
||
public static DataSet QueryDataSet(this SqlDb self, SqlConnection connection, string queryString) | ||
{ | ||
using (SqlCommand selectCommand = SqlDb.generateCommand(connection, queryString)) | ||
{ | ||
using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommand)) | ||
{ | ||
DataSet ds = new DataSet(); | ||
adapter.MissingSchemaAction = SqlDb.MissingSchemaAction; | ||
adapter.Fill(ds); | ||
return ds; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace ACUtils | ||
{ | ||
public static class SqlDb_QueryDataTable | ||
{ | ||
/// <summary> | ||
/// Esegue la query e restituisce il DataTable del risultato | ||
/// </summary> | ||
/// <example> | ||
/// db.QueryDataTable("SELECT * FROM A WHERE B = @B", "@B".WithValue("1")); | ||
/// </example> | ||
/// <param name="queryString">stringa contenente la stringa di interrogazione SQL</param> | ||
/// <param name="queryParams">nomi dei parametri associati a i loro valori. utilizzare <see cref="KeyValuePairExt"/> come helper </param> | ||
/// <returns></returns> | ||
public static DataTable QueryDataTable(this SqlDb self, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
DataSet ds = self.QueryDataSet(queryString, queryParams); | ||
return ds.Tables[0]; | ||
} | ||
|
||
public static DataTable QueryDataTable(this SqlDb self, SqlConnection connection, string queryString, params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
DataSet ds = self.QueryDataSet(connection, queryString, queryParams); | ||
return ds.Tables[0]; | ||
} | ||
|
||
public static DataTable QueryDataTable(this SqlDb self, string queryString, params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
DataSet ds = self.QueryDataSet(queryString, queryParams); | ||
return ds.Tables[0]; | ||
} | ||
|
||
public static DataTable QueryDataTable(this SqlDb self, SqlConnection connection, string queryString, params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
DataSet ds = self.QueryDataSet(connection, queryString, queryParams); | ||
return ds.Tables[0]; | ||
} | ||
|
||
public static DataTable QueryDataTable(this SqlDb self, string queryString) | ||
{ | ||
DataSet ds = self.QueryDataSet(queryString); | ||
return ds.Tables[0]; | ||
} | ||
|
||
public static DataTable QueryDataTable(this SqlDb self, SqlConnection connection, string queryString) | ||
{ | ||
DataSet ds = self.QueryDataSet(connection, queryString); | ||
return ds.Tables[0]; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.