andrew4582

ADOHelper

Aug 11th, 2011
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5.  
  6. namespace Core.Tests {
  7.  
  8.     public class ADOHelper {
  9.  
  10.         public static void SetTableToAllowNulls(DataTable t) {
  11.             foreach(DataColumn col in t.Columns)
  12.                 col.AllowDBNull = true;
  13.         }
  14.        
  15.         public static bool IsDifferent(DataRow fromRow,DataRow toRow) {
  16.             DataColumnCollection col = fromRow.Table.Columns;
  17.             foreach(DataColumn dc in toRow.Table.Columns) {
  18.                 if(col.Contains(dc.ColumnName)) {
  19.                     if(!toRow[dc.ColumnName].ToString().Equals(fromRow[dc.ColumnName].ToString()))
  20.                         return true;
  21.                 }
  22.             }
  23.             return false;
  24.         }
  25.         public static bool IsDataInTable(DataTable table) {
  26.             if(table == null)
  27.                 return false;
  28.             if(table.Rows.Count == 0)
  29.                 return false;
  30.             return true;
  31.         }
  32.         public static bool TryScalarResultInt(object ret,out int val) {
  33.             val = 0;
  34.             if(ret == null)
  35.                 return false;
  36.             return int.TryParse(ret.ToString(),out val);
  37.         }
  38.         public static T GetFirstItem<T>(Array results) {
  39.             if(HasAnyRecords(results) == false)
  40.                 return default(T);
  41.             return (T)results.GetValue(0);
  42.         }
  43.         public static bool HasAnyRecords(Array results) {
  44.             if(results == null)
  45.                 return false;
  46.             if(results.Length == 0)
  47.                 return false;
  48.             return true;
  49.         }
  50.         public static DataRow CopyRow(DataRow row) {
  51.             DataRow newRow = row.Table.NewRow();
  52.             newRow.ItemArray = row.ItemArray;
  53.             return newRow;
  54.         }
  55.         public static void MapRow(DataRow fromRow,DataRow toRow) {
  56.             DataColumnCollection col = fromRow.Table.Columns;
  57.             foreach(DataColumn dc in toRow.Table.Columns) {
  58.                 if(col.Contains(dc.ColumnName))
  59.                     toRow[dc.ColumnName] = fromRow[dc.ColumnName];
  60.             }
  61.         }
  62.         public static int ExceuteCommandText(string sql) {
  63.             return ExceuteCommandText(null,sql);
  64.         }
  65.         public static int ExceuteCommandText(string dbName,string sql) {
  66.             using(SqlConnection cn = (SqlConnection)CreateConnection(dbName)) {
  67.                 cn.Open();
  68.                 using(SqlTransaction tx = cn.BeginTransaction()) {
  69.                     try {
  70.                         using(SqlCommand cmd = new SqlCommand(sql,cn,tx)) {
  71.                             int ret = cmd.ExecuteNonQuery();
  72.                             tx.Commit();
  73.                             return ret;
  74.                         }
  75.                     }
  76.                     catch {
  77.                         tx.Rollback();
  78.                         throw;
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         public static IDbCommand CreateCommand(string dbName,string sql) {
  84.             SqlConnection cn = (SqlConnection)CreateConnection(dbName);
  85.             cn.Open();
  86.             SqlCommand cmd = new SqlCommand(sql,cn);
  87.             return cmd;
  88.         }
  89.         public static DataTable FetchDataTable(string sql) {
  90.             return FetchDataTable(null,sql);
  91.         }
  92.         public static DataTable FetchDataTable(string sql,int startRecord,int maxRecords) {
  93.             return FetchDataTable(null,sql,startRecord,maxRecords);
  94.         }
  95.         public static DataTable FetchDataTable(string dbName,string sql) {
  96.             using(SqlConnection cn = (SqlConnection)CreateConnection(dbName)) {
  97.                 cn.Open();
  98.                 DataTable table = new DataTable();
  99.                 using(SqlDataAdapter da = new SqlDataAdapter(sql,cn)) {
  100.                     da.Fill(table);
  101.                     return table;
  102.                 }
  103.             }
  104.         }
  105.         public static DataTable FetchDataTable(string dbName,string sql,int startRecord,int maxRecords) {
  106.             using(SqlConnection cn = (SqlConnection)CreateConnection(dbName)) {
  107.                 cn.Open();
  108.                 DataTable table = new DataTable();
  109.                 using(SqlDataAdapter da = new SqlDataAdapter(sql,cn)) {
  110.                     da.Fill(startRecord,maxRecords,table);
  111.                     return table;
  112.                 }
  113.             }
  114.         }
  115.         public static SqlConnection CreateConnection(string dbName) {
  116.             if(dbName.IsNull())
  117.                 return CreateConnection();
  118.             ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings[dbName];
  119.             if(setting == null)
  120.                 throw new ArgumentNullException("dbName","DataTableHelper.CreateDatabase " + dbName);
  121.             return new SqlConnection(setting.ConnectionString);
  122.         }
  123.         public static SqlConnection CreateConnection() {
  124.             if(ConfigurationManager.ConnectionStrings.Count == 0)
  125.                 throw new ArgumentException("SqlConnection CreateDatabase - No ConfigurationManager.ConnectionStrings");
  126.             ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings[0];
  127.             return new SqlConnection(setting.ConnectionString);
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment