第七章 业务数据库的管理(五)-- C#动态创建控件

7.4 关联业务数据表

    关联业务数据表实际上就是在数据库(所有用户创建的数据表)中选择业务过程需要处理的业务数据表,并将其数据表名称、创建者等记录到关联表RelatedTables中备用。这样,在定义业务过程时就可以在关联表中选择业务数据表,从而建立业务过程与业务数据的关联。关联业务数据表的页面如图7.5所示:

 

 

图7.5 关联业务数据表

 

7.4.1 关联业务数据表页面RelatedTables.aspx代码

    从图7.5可以看出,RelatedTables.aspx页面实际上由两个表格组成,上面的表格显示已经记录在关联表中的业务数据表;下面表格中显示所有尚未记录到关联表中的用户创建的数据表(不是数据库系统表)。

    RelatedTables.aspx代码比较简单,就是两个空的服务器端表格控件。表格的行和列由页面后台代码动态创建。RelatedTables.aspx代码如下:

    ......

    <form id="Form1" method="post" runat="server">
      <table height="60" cellSpacing="0" cellPadding="0" width="100%" border="0">
       <tr><td></td></tr>
      </table>
      <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
       <tr>
        <td style="FONT-SIZE: 16px" align="center">关联业务数据表</td>
       </tr>
       <tr height="50"><td align="right">&nbsp;</td>
       </tr>
       <tr>
         <td><asp:table id="Tbl" style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid;

                 BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; FONT-FAMILY: 宋体;

                 BORDER-COLLAPSE: collapse" runat="server" border="1"

                 cellPadding="3" cellSpacing="0" Width="100%"></asp:table>

         </td>
      </tr>
     </table>
     <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">

       ......
       <tr>
        <td bgColor="#666666" height="1"></td>
       </tr>
       <tr>
        <td><asp:table id="Table1" style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid;

            BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; FONT-FAMILY: 宋体;

             BORDER-COLLAPSE: collapse" runat="server" border="1" cellPadding="3"

             cellSpacing="0" Width="100%"></asp:table>

        </td>
       </tr>
     </table>

     ......
     </form>

     ...... 

7.4.2 RelatedTables.aspx.cs代码

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    using DataAccess;

    namespace workflow.admin
    {
       public class RelatedTables : System.Web.UI.Page
       {
          protected System.Web.UI.WebControls.Table Table1;
          protected System.Web.UI.WebControls.Table Tbl;
 
          private void Page_Load(object sender, System.EventArgs e)
          {
             if(!IsPostBack)
             {
               //验证用户是否登录
               if(Session["userid"] == null)
               Response.Redirect("./Message.aspx");
             }
             displaytbl();//控件的动态创建和显示不能放在IsPostBack判断内,否则出错。
             displaytable();
          }

          //显示RelatedTables表记录

          void displaytbl()
          {
             string sqlstr="SELECT * FROM relatedtables";
             Base basecode=new Base();
             SqlDataReader dr=basecode.SQLExeDataReader(sqlstr);

             //动态生成表行(表头)

             TableRow tr=new TableRow();
             tr.BackColor=ColorTranslator.FromHtml("#eeeeee");
             tr.HorizontalAlign=HorizontalAlign.Center;   
             TableCell tc1=new TableCell();//生成表列
             tc1.Text="数据表名称";
             tr.Cells.Add(tc1);

             tc1=new TableCell();
             tc1.Text="数据表中文名称";
             tr.Cells.Add(tc1);

             tc1=new TableCell();
             tc1.Text="数据表拥有者";
             tr.Cells.Add(tc1);

             tc1=new TableCell();
             tr.Cells.Add(tc1);

             TableCell tc2=new TableCell();
             tc2.Text="删除关联";
             tr.Cells.Add(tc2);

             Tbl.Rows.Add(tr);

             if(dr !=null)
             {
               int i=1;
               while(dr.Read())
               {
                  TableRow tr1=new TableRow();
                  TableCell tc=new TableCell();
                  Label lb=new Label();
                  lb.ID="label_tablename"+i;//通过不同的i值区分label控件
                  lb.Text=dr["tablename"].ToString();
                  tc.Controls.Add(lb);
                  tr1.Cells.Add(tc);

                  tc=new TableCell();
                  lb=new Label();
                  lb.ID="label_desc"+i;
                  lb.Text=dr["description"].ToString();
                  tc.Controls.Add(lb);
                  tr1.Cells.Add(tc);

                  tc=new TableCell();
                  lb=new Label();
                  lb.ID="label_tableower"+i;
                  lb.Text=dr["ower"].ToString();
                  tc.Controls.Add(lb);
                  tr1.Cells.Add(tc);

                  tc=new TableCell();
                  lb=new Label();
                  lb.ID="label_button"+i;//显示错误信息的label控件
                  lb.ForeColor=Color.Red;
                  lb.Visible=false;
                  tc.Controls.Add(lb);
                  tc.Width=60;
                  tc.HorizontalAlign=HorizontalAlign.Left;
                  tr1.Cells.Add(tc);

                  tc=new TableCell();
                  Button bn=new Button();//删除按钮
                  bn.ID=i.ToString();
                  bn.Text="删除";
                  bn.Click+=new EventHandler(bn_Click_del);//动态设置按钮点击事件
                  tc.Controls.Add(bn);
                  tr1.Cells.Add(tc);

                  tr1.HorizontalAlign=HorizontalAlign.Center;                   
                  Tbl.Rows.Add(tr1);
                  i=i+1;
               }

               basecode.AppCon.Close();
             }
          }

          //删除按钮点击事件处理函数

          void bn_Click_del(Object sender, EventArgs e)
          {
             Button   btn    (Button)sender;
             int i=int.Parse(btn.ID);
             //Response.Write("按钮的ID为:"+i);
             string tbname=((Label)this.FindControl("label_tablename"+i)).Text.Trim();
             string tbower=((Label)this.FindControl("label_tableower"+i)).Text.Trim();
             Label lb=(Label)this.FindControl("label_button"+i);

             //利用事务处理,在删除关联表时,同时删除RelatedTablesFields表中的对应内容.
             string strSql1="delete RelatedTablesFields where TableName='"+tbname+"' and

                  TableOwer='"+tbower+"';";
             string strSql2="delete relatedtables where tablename='"+tbname+"' and

                  ower='"+tbower+"'";

             Base bs=new Base();
             if(!bs.ExeSQLNoResultTransaction(strSql1+strSql2))
             {
                lb.Text=bs.BaseSqlErrDes;
                lb.Visible=true;
             }
             else
             {

                //重定向到本页面
                Response.Redirect( Request.Url.ToString( ) );
             }
          
          //显示除已经记录到RelatedTables表中的所有用户创建的数据表
          void displaytable()
          {

            //系统表sysobjects记录所有数据库对象;系统表sysusers记录所有创建数据库对象的用户;

            //xtype='U'表示数据库对象由用户创建而不是由系统创建 
            string sqlstr="select name,ower=(select name from sysusers where

                uid=sysobjects.uid) from sysobjects where  xtype='U' and name

                   not in(select tablename from relatedtables)";
            Base basecode=new Base();
            SqlDataReader dr=basecode.SQLExeDataReader(sqlstr);

            TableRow tr=new TableRow();
            tr.BackColor=ColorTranslator.FromHtml("#eeeeee");
            tr.HorizontalAlign=HorizontalAlign.Center;   
            TableCell tc1=new TableCell();
            tc1.Text="数据表名称";
            tr.Cells.Add(tc1);

            tc1=new TableCell();
            tc1.Text="数据表拥有者";
            tr.Cells.Add(tc1);

            tc1=new TableCell();
            tc1.Text="数据表中文名称";
            tr.Cells.Add(tc1);

            tc1=new TableCell();
            tr.Cells.Add(tc1);

            TableCell tc2=new TableCell();
            tc2.Text="将数据表关联到本项目";
            tr.Cells.Add(tc2);

            Table1.Rows.Add(tr);

            if(dr !=null)
            {
              int i=1;
              while(dr.Read())
              {
                 TableRow tr1=new TableRow();
                 TableCell tc=new TableCell();
                 Label lb=new Label();
                 lb.ID="label_name"+i;//与displaytbl()中定义的控件编号不同,以避免混淆。
                 lb.Text=dr["name"].ToString();
                 tc.Controls.Add(lb);
                 tr1.Cells.Add(tc);

                 tc=new TableCell();
                 lb=new Label();
                 lb.ID="label_ower"+i;
                 lb.Text=dr["ower"].ToString();
                 tc.Controls.Add(lb);
                 tr1.Cells.Add(tc);

                 tc=new TableCell();
                 TextBox tx=new TextBox();//输入业务表中文名的文本框控件
                 tx.Columns=30;
                 tx.ID="tx"+i;
                 tc.Controls.Add(tx);
                 tr1.Cells.Add(tc);

                 tc=new TableCell();
                 lb=new Label();
                 lb.ID="label_bn"+i;
                 lb.Text="文本框不能为空";
                 lb.ForeColor=Color.Red;
                 lb.Visible=false;
                 tc.Controls.Add(lb);
                 tc.Width=200;
                 tc.HorizontalAlign=HorizontalAlign.Left;
                 tr1.Cells.Add(tc);

                 tc=new TableCell();
                 Button bn=new Button();

                 //这里的"关联"按钮与前面的"删除"按钮都以数字为ID,容易重复,可以在这里再再加上

                 //几个0以区分开.

                 //bn.ID=i.ToString()+"000";

                 //使用下面两行代替上面一行,修改原来的按钮方法内容,可以实现相同的效果.
                 bn.ID="button"+i.ToString();//ID可以重复,由CommandName来区分.
                 bn.CommandName=i.ToString();//也可以利用该参数存储有用的的数字行号i.

                 bn.Text="关联";
                 bn.Click+=new EventHandler(bn_Click);
                 tc.Controls.Add(bn);
                 tr1.Cells.Add(tc);

                 tr1.HorizontalAlign=HorizontalAlign.Center;                   
                 Table1.Rows.Add(tr1);
                 i=i+1;
              }

              basecode.AppCon.Close();
           }
        }

        void bn_Click(Object sender, EventArgs e)
        {
           Button   btn    (Button)sender;

           //int i=(int.Parse(btn.ID))/1000;
           //Response.Write("按钮的ID为:"+i);

           //修改为对应CommandName参数
           int i=int.Parse(btn.CommandName);

           string tbname=((Label)this.FindControl("label_name"+i)).Text.Trim();
           string tbower=((Label)this.FindControl("label_ower"+i)).Text.Trim();

           //获取文本框的输入值
           string tbdescription=HttpContext.Current.Request["tx"+i].Trim();

           //以下两种方法同样可行

           //string tbdescription=((TextBox)this.FindControl("tx"+i)).Text.Trim();
           //string tbdescription=Request.Form["tx"+i].Trim();
           Label lb=(Label)this.FindControl("label_bn"+i);

           if(tbdescription == "")
           {
              lb.Visible=true;
              return;
           }
           lb.Visible=false;

           //判断业务表是否包含标识字段,如果没有则终止关联操作.
           Base basecode=new Base();
           string strSql="Select name from syscolumns Where ID=OBJECT_ID('"+tbname+"')  and

                autoval is not null";
           DataSet identityds=basecode.SQLExeDataSet(strSql);
           if(identityds.Tables[0].Rows.Count<1 )
           {
              lb.Text="数据表"+tbname+"没有定义标识字段,不能进行表关联.";
              lb.Visible=true;
              return;
             

           //将关联记录插入关联表的语句
           string TransactionSql="INSERT INTO relatedtables(tablename, description, ower)

                VALUES ('"+tbname+"', '"+tbdescription+"', '"+tbower+"');";

           //将关联表的字段名插入到关联字段表,用于后续定义任务的读写字段时使用.

           //systypes存储sql所有数据类型的名称等信息;systypes.xtype是数据类型编号

           strSql="SELECT syscolumns.name AS fieldname, systypes.name AS

                fieldtype,syscolumns.length as fieldlenth FROM syscolumns INNER JOIN

                sysobjects ON syscolumns.id = sysobjects.id INNER JOIN systypes ON

                 syscolumns.xtype = systypes.xtype WHERE syscolumns.ID

                  =OBJECT_ID('"+tbname+"') ORDER BY syscolumns.colid";
           DataSet ds=basecode.SQLExeDataSet(strSql);
           string fieldname;
           string datatype;
           string datalenth;
           for(int j=0;j<ds.Tables[0].Rows.Count;j++)
           {
              fieldname=ds.Tables[0].Rows[j]["fieldname"].ToString();
              datatype=ds.Tables[0].Rows[j]["fieldtype"].ToString();
              datalenth=ds.Tables[0].Rows[j]["fieldlenth"].ToString();
              //将前面插入关联表的语句与这里插入关联字段表的多个语句(通过循环获得)组合成多语句

              //操作,交给sql事务处理.
              TransactionSql=TransactionSql+"insert into RelatedTablesFields

                   (TableName,TableOwer,FieldName,DataType,DataLenth) values

                ('"+tbname+"','"+tbower+"','"+fieldname+"','"+datatype+"',"+datalenth+");";   
           }
           if(! basecode.ExeSQLNoResultTransaction(TransactionSql))//调用sql事务处理方法
           {
              lb.Text=basecode.BaseSqlErrDes;
              lb.Visible=true;
              return;
           }
           else
           {
              //添加处理IsIdentity字段的内容;ControlType=0表示无控件,提示系统不需要为该字段进

              //行控件处理.
              strSql="update RelatedTablesFields set ControlType=0,IsIdentity='Y' where

                   FieldName='"+identityds.Tables[0].Rows[0]["name"].ToString()+"' and

                    TableName='"+tbname+"' and TableOwer='"+tbower+"'";
              if(! basecode.SQLExeNonQuery(strSql))
              {
                 lb.Text=basecode.BaseSqlErrDes;
                 lb.Visible=true;
              }
              else
                 Response.Redirect( Request.Url.ToString( ) );
           }
         }

     

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值