c#简单应用之维护页面

SQL Server的增删改查

一次关于报警维护界面的书写

采用Fineui+C#+SQLServer完成一个简单的报警维护页面

开发工具 Visual Studio

开发成果

报警维护页面

如图所有功能均可实现

  • 输入类别(必须)和备注后点击新增将数据插入数据库并呈现在下表中


    若该类别已存在,则提示不要增加
  • 输入关键字可进行查询,查询结果在下表中展示


    查询页面支持单个字体
  • 点击删除,提示是否删除,确定后删除成功


    提示是否删除

开发流程

  • 首先添加一个新建项


    私命名为MachineWarningProgramName
  • 构造前端页面(基于FineUI)

前端构造也就都那么回事,看着长主要是在做格式。

  • 功能实现
    通过前端各个数据的绑定,进行后端数据的书写。
protected void Page_Load(object sender, EventArgs e)
{
  Grid1Bind();
}

private void Grid1Bind()
{
  string strDept = " select ProgramName,ErrorMessage,C_user,C_DATE,U_user,U_DATE from  ProgramError_ProgramName where ProgramName like '%" + tbProgramName.Text.Trim() + "%' and ErrorMessage like '%" + tbErrorMessage.Text.Trim() + "%'";
  DataTable dt = DbHelperSQL.Query(strDept).Tables[0];
  Grid1.DataSource = dt;
  Grid1.DataBind();
  // 查詢programerror_programname表內所有programname數據
}

protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
{
  DataTable dt = Grid1.DataSource as DataTable;
  string programname = Grid1.DataKeys[e.RowIndex][0].ToString();
  // 選取programname所在行
  if (e.CommandName == "Delete")
  {
    string sql = "delete from ProgramError_ProgramName where programName ='" + dt.Rows[e.RowIndex]["programName"].ToString() + "'";
    DbHelperSQL.ExecuteSql(sql);
    Grid1Bind();
  };
  // command事件觸發delete,執行刪除數據
}

protected void btnSearch_Click(object sender, EventArgs e)
{
  Grid1Bind();
}

protected void btnSave_Click(object sender, EventArgs e)
{
  if (tbProgramName.Text.Trim() == "")
  {
    Alert.Show("報警類別不能為空!");
    return;
  }
  StringBuilder sb = new StringBuilder();
  if (hidLx.Text.Trim() == "")
  {
    string strSql = "select * from [ProgramError_ProgramName] where   ProgramName ='" + tbProgramName.Text.Trim() + "' and [ErrorMessage]='" + tbErrorMessage.Text.Trim().Trim() + " ' ";
    DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
    if (dt.Rows.Count > 0)
    {
      Alert.Show("要保存的報警類別已存在,請不要重新增加!");
      return;
    }
    sb.AppendLine(" insert into ProgramError_ProgramName (ProgramName,ErrorMessage,C_user,C_DATE) ");
    sb.AppendLine(" values (N'" + tbProgramName.Text.Trim() + "','" + tbErrorMessage.Text.Trim().Trim() + "' ");
    sb.AppendLine("  ,'" + GetIdentityName() + "',getdate() )");
    }
  else
  {
    sb.AppendLine(" update ProgramError_ProgramName  set ProgramName ='" + tbProgramName.Text.Trim() + "',ErrorMessage='" + tbErrorMessage.Text.Trim().Trim() + "' ");
    sb.AppendLine(" ,U_USER='" + GetIdentityName() + "',U_DATE=getdate() ");
    sb.AppendLine(" where ProgramName ='" + HidCb.Text.Trim() + "'  ");
  }
  try
  {
    int rows = DbHelperSQL.ExecuteSql(sb.ToString());
    if (rows > 0)
    {
      tbProgramName.Text = "";
      tbProgramName.Enabled = true;
      tbErrorMessage.Text = "";
      hidLx.Text = "";
      Grid1Bind();
      Alert.Show("添加成功!");
    }
  }
  catch (Exception ex)
  {
    Alert.Show("添加失敗:" + ex);
    return;
  }
}

protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
{
  hidLx.Text = "1";
  HidCb.Text = Grid1.DataKeys[e.RowIndex][0].ToString();
  tbProgramName.Enabled = false;
  tbProgramName.Text = Grid1.DataKeys[e.RowIndex][0].ToString();
  tbErrorMessage.Text = Grid1.DataKeys[e.RowIndex][1].ToString();
}

代码真多--
用了将近一星期吸收理解sql server

have a nice day!

你可能感兴趣的:(c#简单应用之维护页面)