做了以下的修改
1.介面宣告重新命名
2.移動介面宣告
3.實作介面 & 明確實做介面
做了以下的修改
1.介面宣告重新命名
2.移動介面宣告
3.實作介面 & 明確實做介面
修改如下
Interface.cs
using ...System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace DAL.DataObject
...{
/// <!--IQuery介面-->
/**//// <summary>
/// IQuery介面 - design By Phoenix 2008
/// </summary>
public interface IQuery
...{
///<!--執行SQL命令-->
/// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <returns>受影響的行數</returns>
int RunQuery(string SqlQuery);
///<!--執行SQL命令-->
/// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>受影響的行數</returns>
int RunQuery(string SqlQuery, SqlParameter[] Parameters);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>DataTable資料表</returns>
DataTable GetTable(string SqlQuery);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>DataTable資料表</returns>
DataTable GetTable(string SqlQuery, SqlParameter[] Parameters);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <returns>ataTable資料表</returns>
DataTable GetTable(string[] SelectField, DbName Name);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>ataTable資料表</returns>
DataTable GetTable(string[] SelectField, DbName Name, List<DataField> Condition);
///<!--Insert介面 - 執行Query-->
/// <summary>
/// Insert介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="InsertList">Insert清單</param>
/// <returns>受影響的行數</returns>
int AddNew(DbName Name, List<DataField> InsertList);
///<!--Delete介面 - 執行Query-->
/// <summary>
/// Delete介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="DeleteList">刪除清單</param>
/// <returns>受影響的行數</returns>
int Delete(DbName Name, List<DataField> DeleteList);
///<!--Update介面 - 執行Query-->
/// <summary>
/// Update介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="UpdateList">更新清單</param>
/// <returns>受影響的行數</returns>
int Update(DbName Name, List<DataField> UpdateList);
}
}
using ...System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace DAL.DataObject
...{
/// <!--IQuery介面-->
/**//// <summary>
/// IQuery介面 - design By Phoenix 2008
/// </summary>
public interface IQuery
...{
///<!--執行SQL命令-->
/// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <returns>受影響的行數</returns>
int RunQuery(string SqlQuery);
///<!--執行SQL命令-->
/// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>受影響的行數</returns>
int RunQuery(string SqlQuery, SqlParameter[] Parameters);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>DataTable資料表</returns>
DataTable GetTable(string SqlQuery);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>DataTable資料表</returns>
DataTable GetTable(string SqlQuery, SqlParameter[] Parameters);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <returns>ataTable資料表</returns>
DataTable GetTable(string[] SelectField, DbName Name);
///<!--Select介面 - 取得資料表-->
/// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>ataTable資料表</returns>
DataTable GetTable(string[] SelectField, DbName Name, List<DataField> Condition);
///<!--Insert介面 - 執行Query-->
/// <summary>
/// Insert介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="InsertList">Insert清單</param>
/// <returns>受影響的行數</returns>
int AddNew(DbName Name, List<DataField> InsertList);
///<!--Delete介面 - 執行Query-->
/// <summary>
/// Delete介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="DeleteList">刪除清單</param>
/// <returns>受影響的行數</returns>
int Delete(DbName Name, List<DataField> DeleteList);
///<!--Update介面 - 執行Query-->
/// <summary>
/// Update介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="UpdateList">更新清單</param>
/// <returns>受影響的行數</returns>
int Update(DbName Name, List<DataField> UpdateList);
}
}
DataObject.cs
using ...System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace DAL.DataObject
...{
///<!--資料存取層核心元件-->
/**//// <summary>
/// 資料存取層核心元件 - design By Phoenix 2008
/// </summary>
public class MySqlServer : IQuery
...{
//宣告連線字串物件
private ConnectionString ConnStr;
組合SQL片段#region 組合SQL片段
///<!--去片段多餘的字元-->
/**//// <summary>
/// 去片段多餘的字元 - design By Phoenix 2008
/// </summary>
/// <param name="Part">片段</param>
/// <returns>Part.Trim().TrimEnd(char.Parse(","))</returns>
private string TrimPart(string Part)
...{
//去掉開頭多餘的空白
//去除結尾多餘的逗號
//回傳結果
return Part.Trim().TrimEnd(char.Parse(","));
}
///<!--產生加入參數的片段-->
/**//// <summary>
/// 產生加入參數的片段 - design By Phoenix 2008
/// </summary>
/// <param name="FieldName">要加上參數的強型別物件清單</param>
/// <returns>DataField1 = @DataField1, DataField2 = @DataField2, … DataFieldN = @DataFieldN</returns>
private string SetParameterPart(List<DataField> FieldName)
...{
//建立回傳暫存
string result = "";
//一一取出清單內的值,並且加上參數
foreach (DataField s in FieldName)
result += string.Format(" ...{0} = @{1},", s.ColumnName.Replace("Original_", ""), s.ColumnName);
//呼叫TrimPart
//回傳結果
return TrimPart(result);
}
///<!--產生Insert參數片段-->
/**//// <summary>
/// 產生Insert參數片段 - design By Phoenix 2008
/// </summary>
/// <param name="FieldName">要加入參數的強型別物件清單</param>
/// <returns>(DataField1, DataField2, … DataFieldN) VALUES (@DataField1, @DataField2, … @DataFieldN)</returns>
private string SetInsertPart(List<DataField> FieldName)
...{
//建立回傳暫存(欄位片段、參數片段)
string insertField = "";
string insertParameter = "";
//一一取出清單內的值,並且產生欄位片段以及參數片段
foreach (DataField s in FieldName)
...{
insertField += string.Format(" ...{0},", s.ColumnName);
insertParameter += string.Format(" @...{0},", s.ColumnName);
}
//呼叫TrimPart
//將欄位片段以及參數片段組合成Insert片段
//回傳結果
return string.Format("(...{0}) VALUES ({1})", TrimPart(insertField), TrimPart(insertParameter));
}
///<!--產生 Select 敘述片段-->
/**//// <summary>
/// 產生 Select 敘述片段 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <returns>DataField1, DataField2, … DataFieldN</returns>
private string SetSelectPart(string[] SelectField)
...{
//建立回傳暫存
string result = "";
//一一取出清單內的值,加上逗號
foreach (string s in SelectField)
result += string.Format(" ...{0},", s);
//呼叫TrimPart
//回傳結果
return TrimPart(result);
}
///<!--產生Select`From 敘述-->
/**//// <summary>
/// 產生Select`From 敘述 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <returns>SELECT SelectField FORM DbName</returns>
private string GetSelectQuery(string[] SelectField, DbName Name)
...{
//將 SelectField 和 DbName 加入到敘述內
//回傳結果。
return string.Format("SELECT ...{0} FROM {1}", SetSelectPart(SelectField), Name.ToString());
}
///<!--產生Select`From`Where 敘述-->
/**//// <summary>
/// 產生Select`From`Where 敘述 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>SELECT SelectField FORM DbName WHERE (DataField1 = @DataField1, DataField2 = @DataField2, … DataFieldN = @DataFieldN)</returns>
private string GetSelectWhereQuery(string[] SelectField, DbName Name, List<DataField> Condition)
...{
//呼叫GetSelectQuery,取得 敘述片段
//呼叫SetParameterPart,取得 參數片段
//組合兩片段
//回傳結果
return string.Format("...{0} WHERE ({1})", GetSelectQuery(SelectField, Name), SetParameterPart(Condition));
}
///<!--產生Insert`Into``Values` 敘述-->
/**//// <summary>
/// 產生Insert`Into``Values` 敘述 - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="DataField">新增清單</param>
/// <returns>INSERT INTO DbName (DataField1,DataField2…DataFieldN) VALUES (@DataField1, @DataField2, … @DataFieldN)</returns>
private string GetInsertQuery(DbName Name, List<DataField> DataField)
...{
//呼叫SetInsertPart 取得 Insert片段
//將 DbName 和 Insert片段 放入敘述內。
//回傳結果
return string.Format("INSERT INTO ...{0} {1}", Name.ToString(), SetInsertPart(DataField));
}
///<!--產生Delete From`Where`敘述-->
/**//// <summary>
/// 產生Delete From`Where`敘述 - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>DELETE FROM DbName WHERE(Condition1 = @Condition1, Condition2 = @Condition2, … ConditionN = @ConditionN)</returns>
private string GetDeleteQuery(DbName Name, List<DataField> Condition)
...{
//呼叫SetParameterPart,取得參數片段
//將 DbName 和 參數片段 放入敘述內。
//回傳結果
return string.Format("DELETE FROM ...{0} WHERE ({1})", Name.ToString(), SetParameterPart(Condition));
}
///<!--產生Update`Set`Where`敘述-->
/**//// <summary>
/// 產生Update`Set`Where`敘述 - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Data">DataField強行別物件清單</param>
/// <returns></returns>
private string GetUpdateQuery(DbName Name, List<DataField> Data)
...{
//取得條件式(以Original開頭)
List<DataField> Condition = Data.FindAll(c => c.ColumnName.StartsWith("Original"));
//取得新的值(除Original以外)
List<DataField> UpdateField = Data.FindAll(c => !(c.ColumnName.StartsWith("Original")));
//呼叫SetParameterPart,取得 參數片段(新值)
//呼叫SetParameterPart,取得 參數片段(條件)
//組合兩片段
//回傳結果
return string.Format("UPDATE ...{0} SET {1} WHERE ({2})", Name.ToString(), SetParameterPart(UpdateField), SetParameterPart(Condition));
}
#endregion
///<!--創造Parameters-->
/**//// <summary>
/// 創造Parameters - design By Phoenix 2008
/// </summary>
/// <param name="Data">DataField強行別物件清單</param>
/// <returns>Parameters陣列</returns>
private SqlParameter[] CreateParameters(List<DataField> Data)
...{
//建立回傳暫存
SqlParameter[] result = new SqlParameter[Data.Count];
//一一取出清單內的名字和值,放入result集合
for (int i = 0; i < Data.Count; i++)
result[i] = new SqlParameter("@" + Data[i].ColumnName, Data[i].ColumnValue);
//回傳結果
return result;
}
執行SQL指令#region 執行SQL指令
///<!--執行ExecuteReader - 取得DataTable-->
/**//// <summary>
/// 執行ExecuteReader - 取得DataTable - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>DataTable資料表</returns>
private DataTable ExecuteReader(string SqlQuery)
...{
//例外處理
try
...{
//建立回傳暫存
DataTable dt = new DataTable();
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//開啟連線
SqlConn.Open();
//載入DataTable
dt.Load(SqlCmd.ExecuteReader());
}
//回傳結果
return dt;
}
catch
...{
//擲回例外
throw new InvalidOperationException("ExecuteReader區段執行失敗");
}
}
///<!--執行ExecuteReader - 取得DataTable-->
/**//// <summary>
/// 執行ExecuteReader - 取得DataTable - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數</param>
/// <returns>DataTable資料表</returns>
private DataTable ExecuteReader(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//建立回傳暫存
DataTable dt = new DataTable();
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//將參數加入SqlCommand
SqlCmd.Parameters.AddRange(Parameters);
//開啟連線
SqlConn.Open();
//載入DataTable
dt.Load(SqlCmd.ExecuteReader());
}
//回傳結果
return dt;
}
catch
...{
throw new InvalidOperationException("ExecuteReader區段執行失敗");
}
}
///<!--執行ExecuteNonQuery-->
/**//// <summary>
/// 執行ExecuteNonQuery - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>受影響的行數</returns>
private int ExecuteNonQuery(string SqlQuery)
...{
//例外處理
try
...{
//建立回傳暫存
int result = 0;
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//開啟連線
SqlConn.Open();
//執行ExecuteNonQuery,傳回受影響的行數
result = SqlCmd.ExecuteNonQuery();
}
//傳回受影響的行數
return result;
}
catch (SqlException se)
...{
//擲回例外
throw se;
}
catch
...{
throw new InvalidOperationException("ExecuteNonQuery區段執行失敗");
}
}
///<!--執行ExecuteNonQuery-->
/**//// <summary>
/// 執行ExecuteNonQuery - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>受影響的行數</returns>
private int ExecuteNonQuery(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//建立回傳暫存
int result = 0;
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//將參數加入SqlCommand
SqlCmd.Parameters.AddRange(Parameters);
//開啟連線
SqlConn.Open();
//執行ExecuteNonQuery,傳回受影響的行數
result = SqlCmd.ExecuteNonQuery();
}
//傳回受影響的行數
return result;
}
catch
...{
throw new InvalidOperationException("ExecuteNonQuery區段執行失敗");
}
}
#endregion
IQuery 成員 - 實作介面#region IQuery 成員 - 實作介面
///<!--執行SQL命令-->
/**//// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <returns>受影響的行數</returns>
public int RunQuery(string SqlQuery)
...{
//例外處理
try
...{
//呼叫ExecuteNonQuery(傳入Sql命令)(傳回受影響的行數)。
//傳回受影響的行數
return ExecuteNonQuery(SqlQuery);
}
catch
...{
//擲出例外
throw;
}
}
///<!--執行SQL命令-->
/**//// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>受影響的行數</returns>
public int RunQuery(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//呼叫ExecuteNonQuery(傳入Sql命令)(傳回受影響的行數)。
//傳回受影響的行數
return ExecuteNonQuery(SqlQuery, Parameters);
}
catch
...{
//擲出例外
throw;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string SqlQuery)
...{
//例外處理
try
...{
//呼叫 ExecuteReader(傳入Sql命令)(回傳DataTable資料表)
//回傳DataTable資料表。
return ExecuteReader(SqlQuery);
}
catch
...{
//擲出例外
throw;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//呼叫 ExecuteReader(傳入Sql命令)(回傳DataTable資料表)
//回傳DataTable資料表。
return ExecuteReader(SqlQuery,Parameters);
}
catch
...{
//擲出例外
throw;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string[] SelectField, DbName Name)
...{
//例外處理
try
...{
//呼叫GetSelectQuery(傳入Select欄位、資料表)(傳回Sql命令)
//呼叫ExecuteReader(傳入Sql命令)(傳回DataTable資料表)
//傳回DataTable資料表
return ExecuteReader(GetSelectQuery(SelectField, Name));
}
catch (Exception ex)
...{
//擲出例外
throw ex;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string[] SelectField, DbName Name, List<DataField> Condition)
...{
//例外處理
try
...{
//呼叫GetSelectWhereQuery(傳入Select欄位、資料表、條件清單)(傳回Sql命令)
//呼叫CreateParameters(傳入條件清單)(傳回Parameters陣列)
//呼叫ExecuteReader(傳入Sql命令、Parameters陣列)(回傳DataTable資料表)
//傳回DataTable資料表
return ExecuteReader(GetSelectWhereQuery(SelectField, Name, Condition), CreateParameters(Condition));
}
catch
...{
//擲出例外
throw;
}
}
///<!--Insert介面 - 執行Query-->
/**//// <summary>
/// Insert介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="InsertList">Insert清單</param>
/// <returns>受影響的行數</returns>
public int AddNew(DbName Name, List<DataField> InsertList)
...{
//例外處理
try
...{
//呼叫GetInsertQuery(傳入資料表、Insert清單)(傳回Sql命令)
//呼叫CreateParameters(傳入Insert清單)(傳回Parameters陣列)
//呼叫ExecuteNonQuery(傳入Sql命令、Parameters陣列)(傳回影響的行數)
//傳回影響的行數
return ExecuteNonQuery(GetInsertQuery(Name, InsertList), CreateParameters(InsertList));
}
catch
...{
//擲出例外
throw;
}
}
///<!--Delete介面 - 執行Query-->
/**//// <summary>
/// Delete介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="DeleteList">刪除清單</param>
/// <returns>受影響的行數</returns>
public int Delete(DbName Name, List<DataField> DeleteList)
...{
//例外處理
try
...{
//呼叫GetDeleteQuery(傳入資料表、刪除清單)(傳回Sql命令)
//呼叫CreateParameters(傳入刪除清單)(傳回Parameters陣列)
//呼叫ExecuteNonQuery(傳入Sql命令、Parameters陣列)(傳回受影響的行數)
//傳回受影響的行數
return ExecuteNonQuery(GetDeleteQuery(Name, DeleteList), CreateParameters(DeleteList));
}
catch
...{
//擲出例外
throw;
}
}
///<!--Update介面 - 執行Query-->
/**//// <summary>
/// Update介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="UpdateList">更新清單</param>
/// <returns>受影響的行數</returns>
public int Update(DbName Name, List<DataField> UpdateList)
...{
//例外處理
try
...{
//呼叫GetUpdateQuery(傳入資料表、更新清單)(傳回Sql命令)
//呼叫CreateParameters(傳入更新清單)(傳回Parameters陣列)
//呼叫ExecuteNonQuery(傳入Sql命令、Parameters陣列)(傳回受影響的行數)
//傳回受影響的行數
return ExecuteNonQuery(GetUpdateQuery(Name, UpdateList), CreateParameters(UpdateList));
}
catch
...{
//擲出例外
throw;
}
}
#endregion
IQuery 成員 - 明確實做介面#region IQuery 成員 - 明確實做介面
int IQuery.RunQuery(string SqlQuery)
...{
try
...{
return RunQuery(SqlQuery);
}
catch
...{
throw;
}
}
int IQuery.RunQuery(string SqlQuery, SqlParameter[] Parameters)
...{
try
...{
return RunQuery(SqlQuery, Parameters);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string SqlQuery)
...{
try
...{
return GetTable(SqlQuery);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string SqlQuery, SqlParameter[] Parameters)
...{
try
...{
return GetTable(SqlQuery, Parameters);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string[] SelectField, DbName Name)
...{
try
...{
return GetTable(SelectField, Name);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string[] SelectField, DbName Name, List<DataField> Condition)
...{
try
...{
return GetTable(SelectField, Name, Condition);
}
catch
...{
throw;
}
}
int IQuery.AddNew(DbName Name, List<DataField> InsertList)
...{
try
...{
return AddNew(Name, InsertList);
}
catch
...{
throw;
}
}
int IQuery.Delete(DbName Name, List<DataField> DeleteList)
...{
try
...{
return Delete(Name, DeleteList);
}
catch
...{
throw;
}
}
int IQuery.Update(DbName Name, List<DataField> UpdateList)
...{
try
...{
return Update(Name, UpdateList);
}
catch
...{
throw;
}
}
#endregion
///<!--初始化 DAL.DataObject.SqlServer 類別的新執行個體-->
/**//// <summary>
/// 初始化 DAL.DataObject.SqlServer 類別的新執行個體 - design By Phoenix 2008
/// </summary>
public MySqlServer()
...{
//初始化 ConnStr 物件
ConnStr = new ConnectionString();
}
}
}
using ...System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace DAL.DataObject
...{
///<!--資料存取層核心元件-->
/**//// <summary>
/// 資料存取層核心元件 - design By Phoenix 2008
/// </summary>
public class MySqlServer : IQuery
...{
//宣告連線字串物件
private ConnectionString ConnStr;
組合SQL片段#region 組合SQL片段
///<!--去片段多餘的字元-->
/**//// <summary>
/// 去片段多餘的字元 - design By Phoenix 2008
/// </summary>
/// <param name="Part">片段</param>
/// <returns>Part.Trim().TrimEnd(char.Parse(","))</returns>
private string TrimPart(string Part)
...{
//去掉開頭多餘的空白
//去除結尾多餘的逗號
//回傳結果
return Part.Trim().TrimEnd(char.Parse(","));
}
///<!--產生加入參數的片段-->
/**//// <summary>
/// 產生加入參數的片段 - design By Phoenix 2008
/// </summary>
/// <param name="FieldName">要加上參數的強型別物件清單</param>
/// <returns>DataField1 = @DataField1, DataField2 = @DataField2, … DataFieldN = @DataFieldN</returns>
private string SetParameterPart(List<DataField> FieldName)
...{
//建立回傳暫存
string result = "";
//一一取出清單內的值,並且加上參數
foreach (DataField s in FieldName)
result += string.Format(" ...{0} = @{1},", s.ColumnName.Replace("Original_", ""), s.ColumnName);
//呼叫TrimPart
//回傳結果
return TrimPart(result);
}
///<!--產生Insert參數片段-->
/**//// <summary>
/// 產生Insert參數片段 - design By Phoenix 2008
/// </summary>
/// <param name="FieldName">要加入參數的強型別物件清單</param>
/// <returns>(DataField1, DataField2, … DataFieldN) VALUES (@DataField1, @DataField2, … @DataFieldN)</returns>
private string SetInsertPart(List<DataField> FieldName)
...{
//建立回傳暫存(欄位片段、參數片段)
string insertField = "";
string insertParameter = "";
//一一取出清單內的值,並且產生欄位片段以及參數片段
foreach (DataField s in FieldName)
...{
insertField += string.Format(" ...{0},", s.ColumnName);
insertParameter += string.Format(" @...{0},", s.ColumnName);
}
//呼叫TrimPart
//將欄位片段以及參數片段組合成Insert片段
//回傳結果
return string.Format("(...{0}) VALUES ({1})", TrimPart(insertField), TrimPart(insertParameter));
}
///<!--產生 Select 敘述片段-->
/**//// <summary>
/// 產生 Select 敘述片段 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <returns>DataField1, DataField2, … DataFieldN</returns>
private string SetSelectPart(string[] SelectField)
...{
//建立回傳暫存
string result = "";
//一一取出清單內的值,加上逗號
foreach (string s in SelectField)
result += string.Format(" ...{0},", s);
//呼叫TrimPart
//回傳結果
return TrimPart(result);
}
///<!--產生Select`From 敘述-->
/**//// <summary>
/// 產生Select`From 敘述 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <returns>SELECT SelectField FORM DbName</returns>
private string GetSelectQuery(string[] SelectField, DbName Name)
...{
//將 SelectField 和 DbName 加入到敘述內
//回傳結果。
return string.Format("SELECT ...{0} FROM {1}", SetSelectPart(SelectField), Name.ToString());
}
///<!--產生Select`From`Where 敘述-->
/**//// <summary>
/// 產生Select`From`Where 敘述 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>SELECT SelectField FORM DbName WHERE (DataField1 = @DataField1, DataField2 = @DataField2, … DataFieldN = @DataFieldN)</returns>
private string GetSelectWhereQuery(string[] SelectField, DbName Name, List<DataField> Condition)
...{
//呼叫GetSelectQuery,取得 敘述片段
//呼叫SetParameterPart,取得 參數片段
//組合兩片段
//回傳結果
return string.Format("...{0} WHERE ({1})", GetSelectQuery(SelectField, Name), SetParameterPart(Condition));
}
///<!--產生Insert`Into``Values` 敘述-->
/**//// <summary>
/// 產生Insert`Into``Values` 敘述 - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="DataField">新增清單</param>
/// <returns>INSERT INTO DbName (DataField1,DataField2…DataFieldN) VALUES (@DataField1, @DataField2, … @DataFieldN)</returns>
private string GetInsertQuery(DbName Name, List<DataField> DataField)
...{
//呼叫SetInsertPart 取得 Insert片段
//將 DbName 和 Insert片段 放入敘述內。
//回傳結果
return string.Format("INSERT INTO ...{0} {1}", Name.ToString(), SetInsertPart(DataField));
}
///<!--產生Delete From`Where`敘述-->
/**//// <summary>
/// 產生Delete From`Where`敘述 - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>DELETE FROM DbName WHERE(Condition1 = @Condition1, Condition2 = @Condition2, … ConditionN = @ConditionN)</returns>
private string GetDeleteQuery(DbName Name, List<DataField> Condition)
...{
//呼叫SetParameterPart,取得參數片段
//將 DbName 和 參數片段 放入敘述內。
//回傳結果
return string.Format("DELETE FROM ...{0} WHERE ({1})", Name.ToString(), SetParameterPart(Condition));
}
///<!--產生Update`Set`Where`敘述-->
/**//// <summary>
/// 產生Update`Set`Where`敘述 - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Data">DataField強行別物件清單</param>
/// <returns></returns>
private string GetUpdateQuery(DbName Name, List<DataField> Data)
...{
//取得條件式(以Original開頭)
List<DataField> Condition = Data.FindAll(c => c.ColumnName.StartsWith("Original"));
//取得新的值(除Original以外)
List<DataField> UpdateField = Data.FindAll(c => !(c.ColumnName.StartsWith("Original")));
//呼叫SetParameterPart,取得 參數片段(新值)
//呼叫SetParameterPart,取得 參數片段(條件)
//組合兩片段
//回傳結果
return string.Format("UPDATE ...{0} SET {1} WHERE ({2})", Name.ToString(), SetParameterPart(UpdateField), SetParameterPart(Condition));
}
#endregion
///<!--創造Parameters-->
/**//// <summary>
/// 創造Parameters - design By Phoenix 2008
/// </summary>
/// <param name="Data">DataField強行別物件清單</param>
/// <returns>Parameters陣列</returns>
private SqlParameter[] CreateParameters(List<DataField> Data)
...{
//建立回傳暫存
SqlParameter[] result = new SqlParameter[Data.Count];
//一一取出清單內的名字和值,放入result集合
for (int i = 0; i < Data.Count; i++)
result[i] = new SqlParameter("@" + Data[i].ColumnName, Data[i].ColumnValue);
//回傳結果
return result;
}
執行SQL指令#region 執行SQL指令
///<!--執行ExecuteReader - 取得DataTable-->
/**//// <summary>
/// 執行ExecuteReader - 取得DataTable - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>DataTable資料表</returns>
private DataTable ExecuteReader(string SqlQuery)
...{
//例外處理
try
...{
//建立回傳暫存
DataTable dt = new DataTable();
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//開啟連線
SqlConn.Open();
//載入DataTable
dt.Load(SqlCmd.ExecuteReader());
}
//回傳結果
return dt;
}
catch
...{
//擲回例外
throw new InvalidOperationException("ExecuteReader區段執行失敗");
}
}
///<!--執行ExecuteReader - 取得DataTable-->
/**//// <summary>
/// 執行ExecuteReader - 取得DataTable - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數</param>
/// <returns>DataTable資料表</returns>
private DataTable ExecuteReader(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//建立回傳暫存
DataTable dt = new DataTable();
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//將參數加入SqlCommand
SqlCmd.Parameters.AddRange(Parameters);
//開啟連線
SqlConn.Open();
//載入DataTable
dt.Load(SqlCmd.ExecuteReader());
}
//回傳結果
return dt;
}
catch
...{
throw new InvalidOperationException("ExecuteReader區段執行失敗");
}
}
///<!--執行ExecuteNonQuery-->
/**//// <summary>
/// 執行ExecuteNonQuery - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>受影響的行數</returns>
private int ExecuteNonQuery(string SqlQuery)
...{
//例外處理
try
...{
//建立回傳暫存
int result = 0;
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//開啟連線
SqlConn.Open();
//執行ExecuteNonQuery,傳回受影響的行數
result = SqlCmd.ExecuteNonQuery();
}
//傳回受影響的行數
return result;
}
catch (SqlException se)
...{
//擲回例外
throw se;
}
catch
...{
throw new InvalidOperationException("ExecuteNonQuery區段執行失敗");
}
}
///<!--執行ExecuteNonQuery-->
/**//// <summary>
/// 執行ExecuteNonQuery - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>受影響的行數</returns>
private int ExecuteNonQuery(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//建立回傳暫存
int result = 0;
//使用 SqlConnection 建立 SqlConnection 執行個體(傳入連線字串)
using (SqlConnection SqlConn = new SqlConnection(ConnStr.GetConnStr()))
...{
//宣告 SqlCommand ,建立 SqlCommand 執行個體(傳入Sql命令、SqlConnection)
SqlCommand SqlCmd = new SqlCommand(SqlQuery, SqlConn);
//將參數加入SqlCommand
SqlCmd.Parameters.AddRange(Parameters);
//開啟連線
SqlConn.Open();
//執行ExecuteNonQuery,傳回受影響的行數
result = SqlCmd.ExecuteNonQuery();
}
//傳回受影響的行數
return result;
}
catch
...{
throw new InvalidOperationException("ExecuteNonQuery區段執行失敗");
}
}
#endregion
IQuery 成員 - 實作介面#region IQuery 成員 - 實作介面
///<!--執行SQL命令-->
/**//// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <returns>受影響的行數</returns>
public int RunQuery(string SqlQuery)
...{
//例外處理
try
...{
//呼叫ExecuteNonQuery(傳入Sql命令)(傳回受影響的行數)。
//傳回受影響的行數
return ExecuteNonQuery(SqlQuery);
}
catch
...{
//擲出例外
throw;
}
}
///<!--執行SQL命令-->
/**//// <summary>
/// 執行SQL命令 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">SQL命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>受影響的行數</returns>
public int RunQuery(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//呼叫ExecuteNonQuery(傳入Sql命令)(傳回受影響的行數)。
//傳回受影響的行數
return ExecuteNonQuery(SqlQuery, Parameters);
}
catch
...{
//擲出例外
throw;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string SqlQuery)
...{
//例外處理
try
...{
//呼叫 ExecuteReader(傳入Sql命令)(回傳DataTable資料表)
//回傳DataTable資料表。
return ExecuteReader(SqlQuery);
}
catch
...{
//擲出例外
throw;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SqlQuery">Sql命令</param>
/// <param name="Parameters">參數陣列</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string SqlQuery, SqlParameter[] Parameters)
...{
//例外處理
try
...{
//呼叫 ExecuteReader(傳入Sql命令)(回傳DataTable資料表)
//回傳DataTable資料表。
return ExecuteReader(SqlQuery,Parameters);
}
catch
...{
//擲出例外
throw;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string[] SelectField, DbName Name)
...{
//例外處理
try
...{
//呼叫GetSelectQuery(傳入Select欄位、資料表)(傳回Sql命令)
//呼叫ExecuteReader(傳入Sql命令)(傳回DataTable資料表)
//傳回DataTable資料表
return ExecuteReader(GetSelectQuery(SelectField, Name));
}
catch (Exception ex)
...{
//擲出例外
throw ex;
}
}
///<!--Select介面 - 取得資料表-->
/**//// <summary>
/// Select介面 - 取得資料表 - design By Phoenix 2008
/// </summary>
/// <param name="SelectField">Select欄位(*,表示全部)</param>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="Condition">條件清單</param>
/// <returns>DataTable資料表</returns>
public DataTable GetTable(string[] SelectField, DbName Name, List<DataField> Condition)
...{
//例外處理
try
...{
//呼叫GetSelectWhereQuery(傳入Select欄位、資料表、條件清單)(傳回Sql命令)
//呼叫CreateParameters(傳入條件清單)(傳回Parameters陣列)
//呼叫ExecuteReader(傳入Sql命令、Parameters陣列)(回傳DataTable資料表)
//傳回DataTable資料表
return ExecuteReader(GetSelectWhereQuery(SelectField, Name, Condition), CreateParameters(Condition));
}
catch
...{
//擲出例外
throw;
}
}
///<!--Insert介面 - 執行Query-->
/**//// <summary>
/// Insert介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="InsertList">Insert清單</param>
/// <returns>受影響的行數</returns>
public int AddNew(DbName Name, List<DataField> InsertList)
...{
//例外處理
try
...{
//呼叫GetInsertQuery(傳入資料表、Insert清單)(傳回Sql命令)
//呼叫CreateParameters(傳入Insert清單)(傳回Parameters陣列)
//呼叫ExecuteNonQuery(傳入Sql命令、Parameters陣列)(傳回影響的行數)
//傳回影響的行數
return ExecuteNonQuery(GetInsertQuery(Name, InsertList), CreateParameters(InsertList));
}
catch
...{
//擲出例外
throw;
}
}
///<!--Delete介面 - 執行Query-->
/**//// <summary>
/// Delete介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="DeleteList">刪除清單</param>
/// <returns>受影響的行數</returns>
public int Delete(DbName Name, List<DataField> DeleteList)
...{
//例外處理
try
...{
//呼叫GetDeleteQuery(傳入資料表、刪除清單)(傳回Sql命令)
//呼叫CreateParameters(傳入刪除清單)(傳回Parameters陣列)
//呼叫ExecuteNonQuery(傳入Sql命令、Parameters陣列)(傳回受影響的行數)
//傳回受影響的行數
return ExecuteNonQuery(GetDeleteQuery(Name, DeleteList), CreateParameters(DeleteList));
}
catch
...{
//擲出例外
throw;
}
}
///<!--Update介面 - 執行Query-->
/**//// <summary>
/// Update介面 - 執行Query - design By Phoenix 2008
/// </summary>
/// <param name="Name">列舉-資料表名稱</param>
/// <param name="UpdateList">更新清單</param>
/// <returns>受影響的行數</returns>
public int Update(DbName Name, List<DataField> UpdateList)
...{
//例外處理
try
...{
//呼叫GetUpdateQuery(傳入資料表、更新清單)(傳回Sql命令)
//呼叫CreateParameters(傳入更新清單)(傳回Parameters陣列)
//呼叫ExecuteNonQuery(傳入Sql命令、Parameters陣列)(傳回受影響的行數)
//傳回受影響的行數
return ExecuteNonQuery(GetUpdateQuery(Name, UpdateList), CreateParameters(UpdateList));
}
catch
...{
//擲出例外
throw;
}
}
#endregion
IQuery 成員 - 明確實做介面#region IQuery 成員 - 明確實做介面
int IQuery.RunQuery(string SqlQuery)
...{
try
...{
return RunQuery(SqlQuery);
}
catch
...{
throw;
}
}
int IQuery.RunQuery(string SqlQuery, SqlParameter[] Parameters)
...{
try
...{
return RunQuery(SqlQuery, Parameters);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string SqlQuery)
...{
try
...{
return GetTable(SqlQuery);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string SqlQuery, SqlParameter[] Parameters)
...{
try
...{
return GetTable(SqlQuery, Parameters);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string[] SelectField, DbName Name)
...{
try
...{
return GetTable(SelectField, Name);
}
catch
...{
throw;
}
}
DataTable IQuery.GetTable(string[] SelectField, DbName Name, List<DataField> Condition)
...{
try
...{
return GetTable(SelectField, Name, Condition);
}
catch
...{
throw;
}
}
int IQuery.AddNew(DbName Name, List<DataField> InsertList)
...{
try
...{
return AddNew(Name, InsertList);
}
catch
...{
throw;
}
}
int IQuery.Delete(DbName Name, List<DataField> DeleteList)
...{
try
...{
return Delete(Name, DeleteList);
}
catch
...{
throw;
}
}
int IQuery.Update(DbName Name, List<DataField> UpdateList)
...{
try
...{
return Update(Name, UpdateList);
}
catch
...{
throw;
}
}
#endregion
///<!--初始化 DAL.DataObject.SqlServer 類別的新執行個體-->
/**//// <summary>
/// 初始化 DAL.DataObject.SqlServer 類別的新執行個體 - design By Phoenix 2008
/// </summary>
public MySqlServer()
...{
//初始化 ConnStr 物件
ConnStr = new ConnectionString();
}
}
}
ConnectionString.cs
using ...System.Web.Configuration;
namespace DAL
...{
///<!--連線字串元件 -->
/**//// <summary>
/// 連線字串元件 - design By Phoenix 2008
/// </summary>
public class ConnectionString
...{
///<!--取得預設連線字串-->
/**//// <summary>
/// 取得預設連線字串,於web.config內 - design By Phoenix 2008
/// </summary>
/// <returns>Data Source=?;Initial Catalog=?;Persist Security Info=?;User ID=?;Password=?</returns>
public string GetConnStr()
...{
return GetConnStr(0);
}
///<!--依照索引值,取得連線字串-->
/**//// <summary>
/// 依照索引值,取得連線字串,於web.config內 - design By Phoenix 2008
/// </summary>
/// <param name="index">連線字串索引值。</param>
/// <returns>Data Source=?;Initial Catalog=?;Persist Security Info=?;User ID=?;Password=?</returns>
public string GetConnStr(int index)
...{
return WebConfigurationManager.ConnectionStrings[index].ConnectionString.ToString();
}
///<!--依照名稱,取得連線字串-->
/**//// <summary>
/// 依照名稱,取得連線字串,於web.config內 - design By Phoenix 2008
/// </summary>
/// <param name="name">連線字串名稱</param>
/// <returns>Data Source=?;Initial Catalog=?;Persist Security Info=?;User ID=?;Password=?</returns>
public string GetConnStr(string name)
...{
return WebConfigurationManager.ConnectionStrings[name].ConnectionString.ToString();
}
}
}
using ...System.Web.Configuration;
namespace DAL
...{
///<!--連線字串元件 -->
/**//// <summary>
/// 連線字串元件 - design By Phoenix 2008
/// </summary>
public class ConnectionString
...{
///<!--取得預設連線字串-->
/**//// <summary>
/// 取得預設連線字串,於web.config內 - design By Phoenix 2008
/// </summary>
/// <returns>Data Source=?;Initial Catalog=?;Persist Security Info=?;User ID=?;Password=?</returns>
public string GetConnStr()
...{
return GetConnStr(0);
}
///<!--依照索引值,取得連線字串-->
/**//// <summary>
/// 依照索引值,取得連線字串,於web.config內 - design By Phoenix 2008
/// </summary>
/// <param name="index">連線字串索引值。</param>
/// <returns>Data Source=?;Initial Catalog=?;Persist Security Info=?;User ID=?;Password=?</returns>
public string GetConnStr(int index)
...{
return WebConfigurationManager.ConnectionStrings[index].ConnectionString.ToString();
}
///<!--依照名稱,取得連線字串-->
/**//// <summary>
/// 依照名稱,取得連線字串,於web.config內 - design By Phoenix 2008
/// </summary>
/// <param name="name">連線字串名稱</param>
/// <returns>Data Source=?;Initial Catalog=?;Persist Security Info=?;User ID=?;Password=?</returns>
public string GetConnStr(string name)
...{
return WebConfigurationManager.ConnectionStrings[name].ConnectionString.ToString();
}
}
}
DataField.cs
namespace DAL
...{
///<!--DataField物件模型-->
/**//// <summary>
/// DataField物件模型 - design By Phoenix 2008
/// </summary>
public class DataField
...{
///<!--ColumnName 屬性-->
/**//// <summary>
/// ColumnName 屬性 - design By Phoenix 2008
/// </summary>
public string ColumnName ...{ get; set; }
///<!--ColumnValue 屬性-->
/// <summary>
/// ColumnValue 屬性 - design By Phoenix 2008
/// </summary>
public object ColumnValue ...{ get; set; }
///<!--初始化 DAL.DataField 類別的新執行個體-->
/// <summary>
/// 初始化 DAL.DataField 類別的新執行個體 - design By Phoenix 2008
/// </summary>
public DataField() ...{ }
///<!--初始化 DAL.DataField 類別的新執行個體-->
/// <summary>
/// 初始化 DAL.DataField 類別的新執行個體 - design By Phoenix 2008
/// </summary>
/// <param name="Name">ColumnName 屬性的值</param>
/// <param name="Value">ColumnValue 屬性的值</param>
public DataField(string Name, object Value)
...{
//寫入新的值
ColumnName = Name;
ColumnValue = Value;
}
}
}
namespace DAL
...{
///<!--DataField物件模型-->
/**//// <summary>
/// DataField物件模型 - design By Phoenix 2008
/// </summary>
public class DataField
...{
///<!--ColumnName 屬性-->
/**//// <summary>
/// ColumnName 屬性 - design By Phoenix 2008
/// </summary>
public string ColumnName ...{ get; set; }
///<!--ColumnValue 屬性-->
/// <summary>
/// ColumnValue 屬性 - design By Phoenix 2008
/// </summary>
public object ColumnValue ...{ get; set; }
///<!--初始化 DAL.DataField 類別的新執行個體-->
/// <summary>
/// 初始化 DAL.DataField 類別的新執行個體 - design By Phoenix 2008
/// </summary>
public DataField() ...{ }
///<!--初始化 DAL.DataField 類別的新執行個體-->
/// <summary>
/// 初始化 DAL.DataField 類別的新執行個體 - design By Phoenix 2008
/// </summary>
/// <param name="Name">ColumnName 屬性的值</param>
/// <param name="Value">ColumnValue 屬性的值</param>
public DataField(string Name, object Value)
...{
//寫入新的值
ColumnName = Name;
ColumnValue = Value;
}
}
}
Enum.cs
namespace DAL
...{
///<!--列舉-資料庫名稱-->
/**//// <summary>
/// 列舉-資料庫名稱 - design By Phoenix 2008
/// </summary>
public enum DbName
...{
///<!--News資料表-->
/// <summary>
/// News資料表
/// </summary>
News = 0
// TODO:加入新資料表名稱
}
}
namespace DAL
...{
///<!--列舉-資料庫名稱-->
/**//// <summary>
/// 列舉-資料庫名稱 - design By Phoenix 2008
/// </summary>
public enum DbName
...{
///<!--News資料表-->
/// <summary>
/// News資料表
/// </summary>
News = 0
// TODO:加入新資料表名稱
}
}
News.cs
using ...System;
using System.Collections.Generic;
using System.Data;
using DAL.DataObject;
namespace BLL
...{
///<!--News資料表,商業邏輯元件-->
/**//// <summary>
/// News資料表,商業邏輯元件 - design By Phoenix 2008 -
/// </summary>
public class News
...{
//宣告資料存取元件
private MySqlServer objData;
/// <!--建立News物件-->
/**//// <summary>
/// 建立News物件 - design By Phoenix 2008 -
/// </summary>
/// <param name="row">資料行</param>
/// <returns>News物件</returns>
private DOM.News CreateObject(DataRow row)
...{
//例外處理
try
...{
//Object and Collection Initializers
return new DOM.News()
...{
NewsID = (int)row[0],
NewsDate = (DateTime)row[1],
NewsSubject = row[2].ToString(),
NewsContent = row[3].ToString(),
Del = (bool)row[4],
Chger = row[5].ToString(),
ChgTime = (DateTime)row[6]
};
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateObject區段執行失敗<參數錯誤>");
}
}
///<!--由DataTable(資料表)轉換成List(強型別物件清單)-->
/**//// <summary>
/// 由DataTable(資料表)轉換成List(強型別物件清單) - design By Phoenix 2008 -
/// </summary>
/// <param name="dt">要轉換的DataTable(資料表)</param>
/// <returns>News強型別物件清單</returns>
private List<DOM.News> ToList(DataTable dt)
...{
//例外處理
try
...{
//建立回傳暫存(News強型別物件清單)
List<DOM.News> result = new List<DOM.News>();
result.Clear();
//將DataTable一一取出
//呼叫CreateObject(傳入資料行)(傳回News物件)
//將News物件加入到回傳暫存
foreach (DataRow row in dt.Rows)
result.Add(CreateObject(row));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("ToList區段執行失敗<參數錯誤>");
}
}
///<!--建立Insert強型別物件清單-->
/**//// <summary>
/// 建立Insert強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>DataField強型別物件清單</returns>
private List<DAL.DataField> CreateInsertList(DOM.News oNews)
...{
//例外處理
try
...{
//建立回傳暫存(DataField強型別物件清單)
List<DAL.DataField> result = new List<DAL.DataField>();
result.Clear();
//將要寫入的欄位、值 加入清單
result.Add(new DAL.DataField("NewsSubject", oNews.NewsSubject));
result.Add(new DAL.DataField("NewsContent", oNews.NewsContent));
result.Add(new DAL.DataField("Chger", oNews.Chger));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateInsertList區段執行失敗<參數錯誤>");
}
}
///<!--建立Delete強型別物件清單-->
/**//// <summary>
/// 建立Delete強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>DataField強型別物件清單</returns>
private List<DAL.DataField> CreateDeleteList(DOM.News oNews)
...{
//例外處理
try
...{
//建立回傳暫存(DataField強型別物件清單)
List<DAL.DataField> result = new List<DAL.DataField>();
result.Clear();
//加入條件式
result.Add(new DAL.DataField("Original_NewsID", oNews.NewsID));
//加入刪除標記
result.Add(new DAL.DataField("Del", oNews.Del));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateDeleteList區段執行失敗<參數錯誤>");
}
}
///<!--建立Update強型別物件清單-->
/**//// <summary>
/// 建立Update強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>DataField強型別物件清單</returns>
private List<DAL.DataField> CreateUpdateList(DOM.News oNews)
...{
//例外處理
try
...{
//建立回傳暫存(DataField強型別物件清單)
List<DAL.DataField> result = new List<DAL.DataField>();
result.Clear();
//加入(Original)條件限制
result.Add(new DAL.DataField("Original_NewsID", oNews.NewsID));
//加入新的值
result.Add(new DAL.DataField("NewsSubject", oNews.NewsSubject));
result.Add(new DAL.DataField("NewsContent", oNews.NewsContent));
result.Add(new DAL.DataField("Chger", oNews.Chger));
result.Add(new DAL.DataField("ChgTime", DateTime.Now));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateUpdateList區段執行失敗<參數錯誤>");
}
}
/// <!--取得所有News強型別物件清單-->
/**//// <summary>
/// 取得所有News強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <returns></returns>
public List<DOM.News> GetAll()
...{
//例外處理
try
...{
//field(Select欄位集合),*為取得所有欄位
string[] field = new string[] ...{ "*" };
//執行Select介面,傳入Select欄位集合、資料表名稱,傳回資料表
DataTable dt = objData.GetTable(field, DAL.DbName.News);
//將資料表轉換成強型別物件清單再回傳
return ToList(dt);
}
catch (Exception)
...{
//擲出例外
throw;
}
}
/// <!--取得News強型別物件清單-->
/**//// <summary>
/// 取得News強型別物件集合 - design By Phoenix 2008 -
/// </summary>
/// <param name="Condition">條件限制清單</param>
/// <returns>News強型別物件清單</returns>
public List<DOM.News> GetBy(List<DAL.DataField> Condition)
...{
//例外處理
try
...{
//field(Select欄位集合),*為取得所有欄位
string[] field = new string[] ...{ "*" };
//執行Select介面,傳入Select欄位集合、資料表名稱、條件限制清單,傳回資料表
DataTable dt = objData.GetTable(field, DAL.DbName.News, Condition);
//將資料表轉換成強型別物件清單再回傳
return ToList(dt);
}
catch (Exception)
...{
//擲出例外
throw;
}
}
/// <!--取得News強型別物件清單(ByNewsID)-->
/**//// <summary>
/// 取得News強型別物件清單(ByNewsID) - design By Phoenix 2008 -
/// </summary>
/// <param name="NID"></param>
/// <returns></returns>
public List<DOM.News> GetBy(int NID)
...{
//例外處理
try
...{
return GetBy(CreateCondition(NID));
}
catch (Exception)
...{
//擲出例外
throw;
}
}
private List<DAL.DataField> CreateCondition(int NID)
...{
return new List<DAL.DataField>() ...{ new DAL.DataField("NewsID", NID) };
}
/// <!--新增一筆News資料行-->
/**//// <summary>
/// 新增一筆News資料行 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>受影響的行數</returns>
public int Insert(DOM.News oNews)
...{
//例外處理
try
...{
//判斷Chger(異動者)欄位內容是否為null
if (oNews.Chger == null)
//若為null
//擲回例外。
throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。");
else
//若不為null
//執行CreateInsertList(傳入News清單)(傳回Insert清單)
//執行Insert介面(傳入資料表名稱、Insert清單)(傳回影響的行數)
//傳回受影響的行數
return objData.AddNew(DAL.DbName.News, CreateInsertList(oNews));
}
catch(Exception)
...{
//擲出例外
throw;
}
}
/// <!--執行刪除的動作,刪除Del標記為true的項目-->
/**//// <summary>
/// 執行刪除的動作,刪除Del標記為true的項目 - design By Phoenix 2008 -
/// </summary>
/// <returns>受影響的行數</returns>
public int Delete()
...{
//例外處理
try
...{
//自訂SqlQuery查詢指令
string SqlQuery = "DELETE FROM News WHERE (Del = 1)";
//執行Query,傳回受影響的行數
return objData.RunQuery(SqlQuery);
}
catch (Exception)
...{
//擲出例外
throw;
}
}
/// <!--設定News刪除標記-->
/**//// <summary>
/// 設定News刪除標記 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>受影響的行數</returns>
public int Delete(DOM.News oNews)
...{
//例外處理
try
...{
//呼叫CreateDeleteList(傳入News物件)(傳回Delete清單)
//執行Update介面(傳入資料表名稱、Delete清單)(傳回受影響的行數)
//傳回受影響的行數
return objData.Update(DAL.DbName.News, CreateDeleteList(oNews));
}
catch
...{
//擲出例外
throw;
}
}
/// <!--修改News資料行-->
/**//// <summary>
/// 修改News資料行 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews"></param>
/// <returns></returns>
public int Update(ref DOM.News oNews)
...{
//例外處理
try
...{
//判斷Chger(異動者)欄位內容是否為null
if (oNews.Chger == null)
//若為null
//擲回例外
throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。");
else
//若不為null
//呼叫CreateUpdateList(傳入News物件)(傳回Update清單)
//執行Update介面(傳入資料表名稱、Update清單)(傳回影響的行數)
//傳回受影響的行數
return objData.Update(DAL.DbName.News, CreateUpdateList(oNews));
}
catch (Exception)
...{
//擲出例外
throw;
}
}
///<!--初始化 BLL.News 類別的新執行個體-->
/**//// <summary>
/// 初始化 BLL.News 類別的新執行個體 - design By Phoenix 2008 -
/// </summary>
private News()
...{
//初始化 objData
objData = new MySqlServer();
}
}
}
using ...System;
using System.Collections.Generic;
using System.Data;
using DAL.DataObject;
namespace BLL
...{
///<!--News資料表,商業邏輯元件-->
/**//// <summary>
/// News資料表,商業邏輯元件 - design By Phoenix 2008 -
/// </summary>
public class News
...{
//宣告資料存取元件
private MySqlServer objData;
/// <!--建立News物件-->
/**//// <summary>
/// 建立News物件 - design By Phoenix 2008 -
/// </summary>
/// <param name="row">資料行</param>
/// <returns>News物件</returns>
private DOM.News CreateObject(DataRow row)
...{
//例外處理
try
...{
//Object and Collection Initializers
return new DOM.News()
...{
NewsID = (int)row[0],
NewsDate = (DateTime)row[1],
NewsSubject = row[2].ToString(),
NewsContent = row[3].ToString(),
Del = (bool)row[4],
Chger = row[5].ToString(),
ChgTime = (DateTime)row[6]
};
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateObject區段執行失敗<參數錯誤>");
}
}
///<!--由DataTable(資料表)轉換成List(強型別物件清單)-->
/**//// <summary>
/// 由DataTable(資料表)轉換成List(強型別物件清單) - design By Phoenix 2008 -
/// </summary>
/// <param name="dt">要轉換的DataTable(資料表)</param>
/// <returns>News強型別物件清單</returns>
private List<DOM.News> ToList(DataTable dt)
...{
//例外處理
try
...{
//建立回傳暫存(News強型別物件清單)
List<DOM.News> result = new List<DOM.News>();
result.Clear();
//將DataTable一一取出
//呼叫CreateObject(傳入資料行)(傳回News物件)
//將News物件加入到回傳暫存
foreach (DataRow row in dt.Rows)
result.Add(CreateObject(row));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("ToList區段執行失敗<參數錯誤>");
}
}
///<!--建立Insert強型別物件清單-->
/**//// <summary>
/// 建立Insert強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>DataField強型別物件清單</returns>
private List<DAL.DataField> CreateInsertList(DOM.News oNews)
...{
//例外處理
try
...{
//建立回傳暫存(DataField強型別物件清單)
List<DAL.DataField> result = new List<DAL.DataField>();
result.Clear();
//將要寫入的欄位、值 加入清單
result.Add(new DAL.DataField("NewsSubject", oNews.NewsSubject));
result.Add(new DAL.DataField("NewsContent", oNews.NewsContent));
result.Add(new DAL.DataField("Chger", oNews.Chger));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateInsertList區段執行失敗<參數錯誤>");
}
}
///<!--建立Delete強型別物件清單-->
/**//// <summary>
/// 建立Delete強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>DataField強型別物件清單</returns>
private List<DAL.DataField> CreateDeleteList(DOM.News oNews)
...{
//例外處理
try
...{
//建立回傳暫存(DataField強型別物件清單)
List<DAL.DataField> result = new List<DAL.DataField>();
result.Clear();
//加入條件式
result.Add(new DAL.DataField("Original_NewsID", oNews.NewsID));
//加入刪除標記
result.Add(new DAL.DataField("Del", oNews.Del));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateDeleteList區段執行失敗<參數錯誤>");
}
}
///<!--建立Update強型別物件清單-->
/**//// <summary>
/// 建立Update強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>DataField強型別物件清單</returns>
private List<DAL.DataField> CreateUpdateList(DOM.News oNews)
...{
//例外處理
try
...{
//建立回傳暫存(DataField強型別物件清單)
List<DAL.DataField> result = new List<DAL.DataField>();
result.Clear();
//加入(Original)條件限制
result.Add(new DAL.DataField("Original_NewsID", oNews.NewsID));
//加入新的值
result.Add(new DAL.DataField("NewsSubject", oNews.NewsSubject));
result.Add(new DAL.DataField("NewsContent", oNews.NewsContent));
result.Add(new DAL.DataField("Chger", oNews.Chger));
result.Add(new DAL.DataField("ChgTime", DateTime.Now));
//回傳結果
return result;
}
catch
...{
//擲回例外
throw new InvalidOperationException("CreateUpdateList區段執行失敗<參數錯誤>");
}
}
/// <!--取得所有News強型別物件清單-->
/**//// <summary>
/// 取得所有News強型別物件清單 - design By Phoenix 2008 -
/// </summary>
/// <returns></returns>
public List<DOM.News> GetAll()
...{
//例外處理
try
...{
//field(Select欄位集合),*為取得所有欄位
string[] field = new string[] ...{ "*" };
//執行Select介面,傳入Select欄位集合、資料表名稱,傳回資料表
DataTable dt = objData.GetTable(field, DAL.DbName.News);
//將資料表轉換成強型別物件清單再回傳
return ToList(dt);
}
catch (Exception)
...{
//擲出例外
throw;
}
}
/// <!--取得News強型別物件清單-->
/**//// <summary>
/// 取得News強型別物件集合 - design By Phoenix 2008 -
/// </summary>
/// <param name="Condition">條件限制清單</param>
/// <returns>News強型別物件清單</returns>
public List<DOM.News> GetBy(List<DAL.DataField> Condition)
...{
//例外處理
try
...{
//field(Select欄位集合),*為取得所有欄位
string[] field = new string[] ...{ "*" };
//執行Select介面,傳入Select欄位集合、資料表名稱、條件限制清單,傳回資料表
DataTable dt = objData.GetTable(field, DAL.DbName.News, Condition);
//將資料表轉換成強型別物件清單再回傳
return ToList(dt);
}
catch (Exception)
...{
//擲出例外
throw;
}
}
/// <!--取得News強型別物件清單(ByNewsID)-->
/**//// <summary>
/// 取得News強型別物件清單(ByNewsID) - design By Phoenix 2008 -
/// </summary>
/// <param name="NID"></param>
/// <returns></returns>
public List<DOM.News> GetBy(int NID)
...{
//例外處理
try
...{
return GetBy(CreateCondition(NID));
}
catch (Exception)
...{
//擲出例外
throw;
}
}
private List<DAL.DataField> CreateCondition(int NID)
...{
return new List<DAL.DataField>() ...{ new DAL.DataField("NewsID", NID) };
}
/// <!--新增一筆News資料行-->
/**//// <summary>
/// 新增一筆News資料行 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>受影響的行數</returns>
public int Insert(DOM.News oNews)
...{
//例外處理
try
...{
//判斷Chger(異動者)欄位內容是否為null
if (oNews.Chger == null)
//若為null
//擲回例外。
throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。");
else
//若不為null
//執行CreateInsertList(傳入News清單)(傳回Insert清單)
//執行Insert介面(傳入資料表名稱、Insert清單)(傳回影響的行數)
//傳回受影響的行數
return objData.AddNew(DAL.DbName.News, CreateInsertList(oNews));
}
catch(Exception)
...{
//擲出例外
throw;
}
}
/// <!--執行刪除的動作,刪除Del標記為true的項目-->
/**//// <summary>
/// 執行刪除的動作,刪除Del標記為true的項目 - design By Phoenix 2008 -
/// </summary>
/// <returns>受影響的行數</returns>
public int Delete()
...{
//例外處理
try
...{
//自訂SqlQuery查詢指令
string SqlQuery = "DELETE FROM News WHERE (Del = 1)";
//執行Query,傳回受影響的行數
return objData.RunQuery(SqlQuery);
}
catch (Exception)
...{
//擲出例外
throw;
}
}
/// <!--設定News刪除標記-->
/**//// <summary>
/// 設定News刪除標記 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews">News物件</param>
/// <returns>受影響的行數</returns>
public int Delete(DOM.News oNews)
...{
//例外處理
try
...{
//呼叫CreateDeleteList(傳入News物件)(傳回Delete清單)
//執行Update介面(傳入資料表名稱、Delete清單)(傳回受影響的行數)
//傳回受影響的行數
return objData.Update(DAL.DbName.News, CreateDeleteList(oNews));
}
catch
...{
//擲出例外
throw;
}
}
/// <!--修改News資料行-->
/**//// <summary>
/// 修改News資料行 - design By Phoenix 2008 -
/// </summary>
/// <param name="oNews"></param>
/// <returns></returns>
public int Update(ref DOM.News oNews)
...{
//例外處理
try
...{
//判斷Chger(異動者)欄位內容是否為null
if (oNews.Chger == null)
//若為null
//擲回例外
throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。");
else
//若不為null
//呼叫CreateUpdateList(傳入News物件)(傳回Update清單)
//執行Update介面(傳入資料表名稱、Update清單)(傳回影響的行數)
//傳回受影響的行數
return objData.Update(DAL.DbName.News, CreateUpdateList(oNews));
}
catch (Exception)
...{
//擲出例外
throw;
}
}
///<!--初始化 BLL.News 類別的新執行個體-->
/**//// <summary>
/// 初始化 BLL.News 類別的新執行個體 - design By Phoenix 2008 -
/// </summary>
private News()
...{
//初始化 objData
objData = new MySqlServer();
}
}
}
以上
Phoenix in 08/26