123 lines
5.9 KiB
C#
123 lines
5.9 KiB
C#
using System;
|
|
using System.Data.Common;
|
|
using System.Transactions;
|
|
using WebLib;
|
|
|
|
namespace 報到系統
|
|
{
|
|
/// <summary>
|
|
/// MMSPlatform 資料存取類別
|
|
/// </summary>
|
|
public class DAC_MMSPlatform : DAC
|
|
{
|
|
private const string _tableName = "[MMSPlatform]";
|
|
private const string _allFields = "[ID], [PlatformCode], [PlatformName], [Account], [Password], [Remark], [DB_APPNO], [DB_CRDAT], [DB_CRUSR], [DB_TRDAT], [DB_TRUSR]";
|
|
|
|
public DAC_MMSPlatform() : base() { }
|
|
|
|
public DAC_MMSPlatform(DbConnection conn) : base(conn) { }
|
|
|
|
/// <summary>查詢所有平台</summary>
|
|
public MMSPlatformList SelectAll()
|
|
{
|
|
MMSPlatformList result = new MMSPlatformList();
|
|
IDataList list = result as IDataList;
|
|
DbCommand cmd = NewCommand();
|
|
cmd.CommandText = "select " + _allFields + " from " + _tableName + " order by [ID]";
|
|
Select(cmd, ref list);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>依平台代碼查一筆</summary>
|
|
public MMSPlatformItem SelectByCode(string platformCode)
|
|
{
|
|
MMSPlatformList result = new MMSPlatformList();
|
|
IDataList list = result as IDataList;
|
|
DbCommand cmd = NewCommand();
|
|
cmd.CommandText = "select " + _allFields + " from " + _tableName + " where [PlatformCode] = @PlatformCode";
|
|
AddParam(cmd, "PlatformCode", platformCode);
|
|
Select(cmd, ref list);
|
|
return result.Count > 0 ? result[0] : null;
|
|
}
|
|
|
|
/// <summary>新增一筆平台設定</summary>
|
|
public MMSPlatformItem InsertOne(MMSPlatformItem item)
|
|
{
|
|
using (TransactionScope ts = DAC.NewTransactionScope())
|
|
{
|
|
DbCommand cmd = NewCommand();
|
|
cmd.CommandText =
|
|
"insert into " + _tableName + " ([PlatformCode],[PlatformName],[Account],[Password],[Remark],[DB_APPNO],[DB_CRDAT],[DB_CRUSR],[DB_TRDAT],[DB_TRUSR]) " +
|
|
"values (@PlatformCode,@PlatformName,@Account,@Password,@Remark,1,@DB_CRDAT,@DB_CRUSR,@DB_TRDAT,@DB_TRUSR); select @@identity";
|
|
AddParam(cmd, "PlatformCode", item.PlatformCode);
|
|
AddParam(cmd, "PlatformName", item.PlatformName);
|
|
AddParam(cmd, "Account", (object)item.Account ?? DBNull.Value);
|
|
AddParam(cmd, "Password", (object)item.Password ?? DBNull.Value);
|
|
AddParam(cmd, "Remark", (object)item.Remark ?? DBNull.Value);
|
|
AddParam(cmd, "DB_CRDAT", DateTime.Now);
|
|
AddParam(cmd, "DB_CRUSR", CurrentUserId);
|
|
AddParam(cmd, "DB_TRDAT", DateTime.Now);
|
|
AddParam(cmd, "DB_TRUSR", CurrentUserId);
|
|
int newId;
|
|
ExecuteScalar(cmd, out newId);
|
|
item.ID = newId;
|
|
ts.Complete();
|
|
return item;
|
|
}
|
|
}
|
|
|
|
/// <summary>修改一筆平台設定(含 DB_APPNO 樂觀鎖定)</summary>
|
|
public MMSPlatformItem UpdateOne(MMSPlatformItem item)
|
|
{
|
|
using (TransactionScope ts = DAC.NewTransactionScope())
|
|
{
|
|
// 備份舊值
|
|
MMSPlatformItem old = SelectByCode(item.PlatformCode);
|
|
if (old == null || old.DB_APPNO != item.DB_APPNO)
|
|
throw new System.Data.DBConcurrencyException("在更新 \"MMSPlatform\" 資料表時發生並行例外:找不到要更新的資料列");
|
|
|
|
DoBackup(old, "UPDATE");
|
|
|
|
DbCommand cmd = NewCommand();
|
|
cmd.CommandText =
|
|
"update " + _tableName + " set " +
|
|
" [PlatformName]=@PlatformName, [Account]=@Account, [Password]=@Password, [Remark]=@Remark, " +
|
|
" [DB_APPNO]=DB_APPNO+1, [DB_TRDAT]=@DB_TRDAT, [DB_TRUSR]=@DB_TRUSR " +
|
|
"where [ID]=@ID and [DB_APPNO]=@original_DB_APPNO";
|
|
AddParam(cmd, "PlatformName", item.PlatformName);
|
|
AddParam(cmd, "Account", (object)item.Account ?? DBNull.Value);
|
|
AddParam(cmd, "Password", (object)item.Password ?? DBNull.Value);
|
|
AddParam(cmd, "Remark", (object)item.Remark ?? DBNull.Value);
|
|
AddParam(cmd, "DB_TRDAT", DateTime.Now);
|
|
AddParam(cmd, "DB_TRUSR", CurrentUserId);
|
|
AddParam(cmd, "ID", item.ID);
|
|
AddParam(cmd, "original_DB_APPNO", item.DB_APPNO);
|
|
ExecuteNonQuery(cmd);
|
|
ts.Complete();
|
|
return item;
|
|
}
|
|
}
|
|
|
|
private void DoBackup(MMSPlatformItem item, string updateMode)
|
|
{
|
|
DbCommand cmd = NewCommand();
|
|
cmd.CommandText =
|
|
"insert into [MMSPlatform_BAK] ([ID],[PlatformCode],[PlatformName],[Account],[Password],[Remark],[DB_APPNO],[DB_CRDAT],[DB_CRUSR],[DB_TRDAT],[DB_TRUSR],[DB_TRMOD]) " +
|
|
"values (@ID,@PlatformCode,@PlatformName,@Account,@Password,@Remark,@DB_APPNO,@DB_CRDAT,@DB_CRUSR,@DB_TRDAT,@DB_TRUSR,@DB_TRMOD)";
|
|
AddParam(cmd, "ID", item.ID);
|
|
AddParam(cmd, "PlatformCode", item.PlatformCode);
|
|
AddParam(cmd, "PlatformName", item.PlatformName);
|
|
AddParam(cmd, "Account", (object)item.Account ?? DBNull.Value);
|
|
AddParam(cmd, "Password", (object)item.Password ?? DBNull.Value);
|
|
AddParam(cmd, "Remark", (object)item.Remark ?? DBNull.Value);
|
|
AddParam(cmd, "DB_APPNO", item.DB_APPNO);
|
|
AddParam(cmd, "DB_CRDAT", item.DB_CRDAT);
|
|
AddParam(cmd, "DB_CRUSR", item.DB_CRUSR);
|
|
AddParam(cmd, "DB_TRDAT", DateTime.Now);
|
|
AddParam(cmd, "DB_TRUSR", CurrentUserId);
|
|
AddParam(cmd, "DB_TRMOD", updateMode);
|
|
ExecuteNonQuery(cmd);
|
|
}
|
|
}
|
|
}
|