一步一步建立我的MIS系统(2).DataObject,所有数据对象的基类

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Text;

namespace EwEAI.Data
{

 /// <summary>
 /// 数据存在类型
 /// </summary>
 public enum RecordExistsType
 {
  FullFill,//具有完全相同的记录

  PartialFill,//具有部分相同的记录

  NotExists //没有相关的记录
 }


 /// <summary>
 /// RemoteObject:所有数据对象的基类。
 /// </summary>
    public class DataObject
 {
        public DataObject(string tableName,string userName)
  {  
   _TableName=tableName;
            UserName = userName;
  }

        public DataTable GetFieldAuthority()
        {
            string SQL = "select FieldName from FieldPrivileges"
            + " where TableName='" + TableName + "'"
            + "   and UserName in (select UserName from UserTree('" + UserName + "',1))"
            +"    and FieldName not in(select FieldName from FieldPrivileges "
            + "       where UpdatePrivilege=" + ((int)IT.PrivilegeType.DenyUpdate).ToString() + ")";

            return DBWR.Instance.ReadData(SQL);
           
        }

        public DataTable GetTableAuthority()
        {
            string SQL = "select InsertPrivilege,UpdatePrivilege,DeletePrivilege from TablePrivileges "
            + " where TableName='" + TableName + "'"
            + "  and UserName in(select UserName from UserTree('" + UserName + "',1))";

            return DBWR.Instance.ReadData(SQL);             
        }


        public string TableName
        {
            get
            {
                return _TableName;
            }           
        }
        private string _TableName = "";

        protected string UserName = null;

        private string[] Conditions = new string[3] {
            "SelectCondition",
            "UpdateCondition",
            "DeleteCondition"
        };

        public DataTable GetStruct()
        {
            string SQL = DBWR.Instance.BuildSelectSQL(UserName, TableName,"", "", null);
            if (SQL == null)
                return null;

            DataTable Result=DBWR.Instance.GetStruct(SQL);


            return Result;
        }

        public static DataTable GetCaption(string theTable)
        {
            DataTable Fields = DBWR.Instance.ReadData(
                "select FieldName,Caption from Fields where Tablename='" + theTable + "'");

            return Fields;
        }
       

  public DataTable GetData(string where,ref string ReturnSQL)
  {
            string SQL = DBWR.Instance.BuildSelectSQL(UserName, TableName, GetSelectCondition(where), "", null);

   if(SQL==null)
    return null ;

            DataTable Result = new DataTable(TableName);
   
   DBWR.Instance.ReadData(SQL,ref Result);
            SetCalcColumns(ref Result);

            ReturnSQL = SQL;

            return Result;  
  }

        protected virtual void SetCalcColumns(ref DataTable table)
        {
        }

  private string GetSelectCondition(string where)
  {
            string SQL = "select top 1 SelectCondition from ( "
                           + " select P.SelectCondition,U.Level from TablePrivileges P "
                           + "                join UserTree('" + UserName + "',1) U "
                           + "        on P.UserName=U.UserName "
                           + " where P.TableName='" + TableName + "'"
                           + " ) a "
                           + " order by Level ";
         

            string Define=(string)DBWR.Instance.GetSingleValue(SQL);
            StringBuilder Result = new StringBuilder(Define);

            if (Result.Length != 0)
            {
                ParseParameter(Result);             

                Result.Insert(0, "where ");
            }
          

            if (where != null && where != "")
            {
                if (Result.Length != 0)
                {
                    Result.Append(" and ");
                    Result.Append(where);
                }
                else
                {
                    Result.Append("where ");
                    Result.Append(where);
                }
            }

            return Result.ToString();
  }

        private void ParseParameter(StringBuilder Result)
        {
            Result.Replace("@User", "'"+UserName+"'");
            Result.Replace("@ToDay","'"+ DateTime.Now.ToShortDateString()+"'");
        }  
 }
}

阅读更多
个人分类: .Net分析与设计
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭