using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace MCO.ADO.NET { class SqlServerHelper { #region 該類的核心代碼
/// <summary>
/// 私有化構造器(單例模式開發數據庫查詢工具類) /// </summary>
private SqlServerHelper() { } /// <summary>
/// 程序執行前實例化一個數據庫幫助類 /// </summary>
private static SqlServerHelper sqlServer = new SqlServerHelper(); /// <summary>
/// 數據庫連接字符串 /// </summary>
private string connection; /// <summary>
/// 數據庫命令執行方法(SQL語句) /// </summary>
private int Command(string sql) { SqlConnection conn = new SqlConnection(connection); try { SqlCommand command = new SqlCommand(sql, conn); conn.Open(); return command.ExecuteNonQuery(); } catch { return 0; } finally { conn.Close(); } } /// <summary>
/// 查詢(SQL語句) /// </summary>
private DataTable GetList(string sql) { SqlConnection conn = new SqlConnection(connection); try { SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); DataTable data = new DataTable(); adapter.Fill(data); return data; } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion
/// <summary>
/// 創建該單例類的方法(數據庫連接字符串) /// </summary>
public static SqlServerHelper GetSqlServer(string connection) { sqlServer.connection = connection; return sqlServer; } /// <summary>
/// 完全查詢(表名, 字段) //表名和字段最好加上中括號[Id] /// </summary>
public DataTable GetList(string tableName, string field) { string sql = string.Format("select {0} from {1}", field, tableName); return GetList(sql); } /// <summary>
/// 條件查詢(表名, 字段, 條件) /// </summary>
public DataTable GetList(string tableName, string field, string where) { string sql = string.Format("select {0} form {1} where {2}", field, tableName, where); return GetList(sql); } /// <summary>
/// 分頁查詢(表名, 字段, 條件, 主鍵, 頁碼, 條數) /// </summary>
public DataTable GetList(string tableName, string field, string where, string idField, int page, int size) { string sql = string.Format("select top {0} {1} from {2} where {3} not in (select top {4} {5} from {6} where {7}) and ({8})", size, field, tableName, idField, (page - 1) * size, idField, tableName, where, where ); return GetList(sql); } /// <summary>
/// 排序查詢(表名, 字段, 條件, 排序) /// </summary>
public DataTable GetList(string tableName, string field, string where, string order) { string sql = string.Format("select {0} form {1} where {2} order by {3}", field, tableName, where, order); return GetList(sql); } /// <summary>
/// 分頁排序查詢(表名, 字段, 條件, 主鍵, 頁碼, 條數, 排序) /// </summary>
public DataTable GetList(string tableName, string field, string where, string idField, int page, int size, string order) { string sql = string.Format("select top {0} {1} from {2} where {3} not in (select top {4} {5} from {6} where {7}) and ({8}) order by {9}", size, field, tableName, idField, (page - 1) * size, idField, tableName, where, where, order ); return GetList(sql); } /// <summary>
/// 條件刪除(表名, 條件) //返回受影響的行數, 0 表示失敗 /// </summary>
public int Delete(string tableName, string where) { string sql = string.Format("delete from {0} where {1}", tableName, where); return Command(sql); } /// <summary>
/// 條件修改(表名, 更新的數據, 條件) /// </summary>
public int Update(string tableName, string updateData, string where) { string sql = string.Format("update {0} set {1} where {2}", tableName, updateData, where); return Command(sql); } } }