首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# Windows Forms 實作:編輯功能表單與資料更新機制

     編輯:關於C語言

前言

承接上一篇「新增功能」表單,本文解析對應的編輯表單 SM_EditFunction。主要差異在於:建構子接收現有實體 FunctionTable editFuncItem 進行初始化、確認時執行 UPDATE 而非 INSERT、並提供 IsEdit 屬性供呼叫端判斷使用者是否真的修改了資料。

表單初始化:載入既有資料

public SM_EditFunction(FunctionTable editFuncItem, ClientDictionary[] dic_funcType)
{
    InitializeComponent();
    oriFuncItem = editFuncItem; // 保存原始資料供更新時參考
    DIC_funcType = dic_funcType;

    // 綁定類別下拉選單
    cbo_Type.DataSource = DIC_funcType;
    cbo_Type.ValueMember = "valueMember";
    cbo_Type.DisplayMember = "displayMember";

    // 填入既有值
    txt_Name.Text = oriFuncItem.funcName;
    txt_Tag.Text = oriFuncItem.funcTag;
    ckb_IsInterface.Checked = (oriFuncItem.funcIsInterface == "1");
    cbo_Type.SelectedValue = oriFuncItem.funcType;
    txt_FunEntity.Text = oriFuncItem.funcEntity;
    txt_BelongTo.Text = oriFuncItem.funcBelongTo;
    txt_Detail.Text = oriFuncItem.funcDetail;

    funcTableConn = new DataAccessObject().Connection;
}

確認更新:驗證與組裝

private void btn_Confirm_Click(object sender, EventArgs e)
{
    // 必填驗證(同新增邏輯)
    if (string.IsNullOrEmpty(txt_Name.Text)) { MessageBox.Show("功能名稱不能為空"); return; }
    if (string.IsNullOrEmpty(txt_Tag.Text)) { MessageBox.Show("功能標識不能為空"); return; }
    if (string.IsNullOrEmpty(txt_FunEntity.Text)) { MessageBox.Show("功能體不能為空"); return; }

    newFuncItem = new FunctionTable
    {
        funcName = txt_Name.Text.Trim(),
        funcTag = txt_Tag.Text.Trim(),
        funcIsInterface = ckb_IsInterface.Checked ? "1" : "0",
        funcType = cbo_Type.SelectedValue.ToString().Trim(),
        funcEntity = txt_FunEntity.Text.Trim(),
        funcBelongTo = txt_BelongTo.Text.Trim(),
        funcDetail = txt_Detail.Text.Trim(),
        // 保留原始不可編輯欄位
        funcIsExpand = oriFuncItem.funcIsExpand,
        funcVer = oriFuncItem.funcVer,
        funcID = oriFuncItem.funcID,
        funcFatherID = oriFuncItem.funcFatherID
    };

    UpdateFuncItem(newFuncItem);
    isEdit = true; // 標記已修改
    this.Close();
}

資料庫更新與交易控制

private void UpdateFuncItem(FunctionTable newItem)
{
    funcTableConn.Open();
    using (var tran = funcTableConn.BeginTransaction())
    {
        try
        {
            var sql = @"UPDATE 功能 SET
                            功能標識=:tag, 功能名稱=:name, 功能簡述=:detail,
                            所屬窗體=:belong, 有無界面=:interface, 功能類別=:type,
                            功能體=:entity, 上級功能ID=:father, 可否展顯=:expand, 版本號=:ver
                        WHERE 功能ID = :id";
            using (var cmd = new OracleCommand(sql, funcTableConn, tran))
            {
                cmd.Parameters.Add("tag", newItem.funcTag);
                cmd.Parameters.Add("name", newItem.funcName);
                cmd.Parameters.Add("detail", newItem.funcDetail);
                cmd.Parameters.Add("belong", newItem.funcBelongTo);
                cmd.Parameters.Add("interface", newItem.funcIsInterface);
                cmd.Parameters.Add("type", newItem.funcType);
                cmd.Parameters.Add("entity", newItem.funcEntity);
                cmd.Parameters.Add("father", newItem.funcFatherID);
                cmd.Parameters.Add("expand", newItem.funcIsExpand);
                cmd.Parameters.Add("ver", newItem.funcVer);
                cmd.Parameters.Add("id", newItem.funcID);
                cmd.ExecuteNonQuery();
            }
            tran.Commit();
        }
        catch (Exception ex)
        {
            try { tran.Rollback(); } catch { }
            MessageBox.Show("修改失敗:" + ex.Message);
        }
        finally { funcTableConn.Close(); }
    }
}

取消與狀態回傳

private void btn_Cancel_Click(object sender, EventArgs e)
{
    isEdit = false; // 明確標記未修改
    this.Close();
}

public bool IsEdit { get { return isEdit; } } // 唯讀屬性供外部檢查
public FunctionTable NewFuncItem { get { return newFuncItem; } }

設計重點總結

  • 原始資料保留:oriFuncItem 保存未修改前的值,更新時只更新使用者可編輯的欄位,funcID、funcFatherID、funcVer 等系統欄位保持不變。
  • 狀態標記:isEdit 讓呼叫端(如主視窗的 TreeView 節點重新整理邏輯)知道是否需重新載入資料。
  • 參數化 SQL:同新增邏輯,防注入、效能佳。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved