예제 #1
0
        //创建文件数据库,并添加50条数据。
        void CreateTable()
        {
            Response.Write("文章见:http://www.cnblogs.com/cyq1162/p/3443244.html <hr />");
            if (DBTool.ExistsTable(tableName))
            {
                using (MAction action = new MAction(tableName))
                {
                    if (action.Fill("order by id desc"))
                    {
                        action.Delete("id<=" + action.Get <int>(0));
                    }
                }
                //DBTool.DropTable(tableName);
            }
            else
            {
                MDataColumn mdc = new MDataColumn();
                mdc.Add("ID", SqlDbType.Int, true);
                mdc.Add("Name");
                mdc.Add("CreateTime", SqlDbType.DateTime);
                DBTool.CreateTable(tableName, mdc);
            }
            MDataTable dt = new MDataTable(tableName);

            dt.Columns = DBTool.GetColumns(tableName);
            for (int i = 0; i < 60; i++)
            {
                dt.NewRow(true).Set(1, "Name_" + i).Set(2, DateTime.Now.AddSeconds(i));
            }
            dt.AcceptChanges(AcceptOp.Insert);
        }
예제 #2
0
        private void Init(MDataTable mTable, string conn)
        {
            if (mTable.Columns == null || mTable.Columns.Count == 0)
            {
                Error.Throw("MDataTable's columns can't be null or columns'length can't be zero");
            }
            if (string.IsNullOrEmpty(mTable.TableName))
            {
                Error.Throw("MDataTable's tablename can't  null or empty");
            }
            mdt = sourceTable = mTable;

            if (mdt.TableName.IndexOfAny(new char[] { '(', ')' }) > -1)
            {
                mdt.TableName = mdt.TableName.Substring(mdt.TableName.LastIndexOf(')') + 1).Trim();
            }

            _Conn = !string.IsNullOrEmpty(conn) ? conn : mTable.Conn;
            if (!DBTool.ExistsTable(mdt.TableName, _Conn, out dalTypeTo, out database))
            {
                if (!DBTool.CreateTable(mdt.TableName, mdt.Columns, _Conn))
                {
                    Error.Throw("Create Table Error:" + mdt.TableName);
                }
            }
            MDataColumn column = DBTool.GetColumns(mdt.TableName, _Conn);

            FixTable(column);//
            if (mdt.Columns.Count == 0)
            {
                Error.Throw("After fix table columns, length can't be zero");
            }
            SetDbBaseForTransaction();
        }
예제 #3
0
        private void Init(MDataTable mTable, string conn)
        {
            if (mTable.Columns == null || mTable.Columns.Count == 0)
            {
                Error.Throw("MDataTable's columns can't be null or columns'length can't be zero");
            }
            if (string.IsNullOrEmpty(mTable.TableName))
            {
                Error.Throw("MDataTable's tablename can't  null or empty");
            }
            mdt = sourceTable = mTable;

            if (mdt.TableName.IndexOfAny(new char[] { '(', ')' }) > -1)
            {
                mdt.TableName = mdt.TableName.Substring(mdt.TableName.LastIndexOf(')') + 1).Trim();
            }
            if (!string.IsNullOrEmpty(conn))
            {
                _Conn = conn;
            }
            else
            {
                if (mTable.DynamicData != null && mTable.DynamicData is MAction)//尝试多动态中获取链接
                {
                    _Conn = ((MAction)mTable.DynamicData).ConnectionString;
                }
                else if (mTable.DynamicData != null && mTable.DynamicData is MProc)
                {
                    _Conn = ((MProc)mTable.DynamicData).ConnectionString;
                }
                else
                {
                    _Conn = mTable.Conn;
                }
            }

            if (!DBTool.ExistsTable(mdt.TableName, _Conn, out dalTypeTo, out database))
            {
                DBTool.ErrorMsg = null;
                if (!DBTool.CreateTable(mdt.TableName, mdt.Columns, _Conn))
                {
                    Error.Throw("Create Table Error:" + mdt.TableName + DBTool.ErrorMsg);
                }
            }
            MDataColumn column = DBTool.GetColumns(mdt.TableName, _Conn);

            FixTable(column);//
            if (mdt.Columns.Count == 0)
            {
                Error.Throw("After fix table columns, length can't be zero");
            }
            SetDbBaseForTransaction();
        }
예제 #4
0
 private void InitData()
 {
     if (!DBTool.ExistsTable("UserType", "xml path={0}App_Data"))
     {
         using (UserType ut = new UserType())
         {
             ut.Delete("1=1");//Clear All Data
             for (int i = 1; i < 5; i++)
             {
                 ut.TypeName = "Type" + i;
                 ut.Insert(InsertOp.None);
             }
         }
     }
 }
예제 #5
0
 public static bool CheckSysAutoCacheTable()
 {
     if (!HasAutoCacheTable && !string.IsNullOrEmpty(AppConfig.Cache.AutoCacheConn))
     {
         string AutoCacheConn = AppConfig.Cache.AutoCacheConn;
         if (DBTool.TestConn(AutoCacheConn))
         {
             HasAutoCacheTable = DBTool.ExistsTable(KeyTableName, AutoCacheConn);
             //检测数据是否存在表
             if (!HasAutoCacheTable)
             {
                 MDataColumn mdc = new MDataColumn();
                 mdc.Add("CacheKey", System.Data.SqlDbType.NVarChar, false, false, 200, true, null);
                 mdc.Add("CacheTime", System.Data.SqlDbType.BigInt, false, false, -1);
                 HasAutoCacheTable = DBTool.CreateTable(KeyTableName, mdc, AutoCacheConn);
                 if (!HasAutoCacheTable)                                                  //若创建失败,可能并发下其它进程创建了。
                 {
                     HasAutoCacheTable = DBTool.ExistsTable(KeyTableName, AutoCacheConn); //重新检测表是否存在。
                 }
             }
         }
     }
     return(HasAutoCacheTable);
 }
예제 #6
0
        static void Start()
        {
            bool result = DBTool.TestConn(AppConfig.DB.DefaultConn);//检测数据库链接是否正常

            OutMsg("数据库链接:" + result);
            OutMsg("-----------------------------------------");
            string databaseName;
            Dictionary <string, string> tables = DBTool.GetTables(AppConfig.DB.DefaultConn, out databaseName);//读取所有表

            if (tables != null)
            {
                OutMsg("数据库:" + databaseName);
                foreach (KeyValuePair <string, string> item in tables)
                {
                    OutMsg("表:" + item.Key + " 说明:" + item.Value);
                    MDataColumn mdc = DBTool.GetColumns(item.Key);//读取所有列
                    foreach (MCellStruct ms in mdc)
                    {
                        OutMsg("  列:" + ms.ColumnName + " SqlType:" + ms.SqlType);
                    }
                }
            }
            OutMsg("-----------------------------------------");

            string newTableName = "A18";// +DateTime.Now.Second;

            DalType dalType;

            result = DBTool.ExistsTable(newTableName, AppConfig.DB.DefaultConn, out dalType);//检测表是否存在
            OutMsg("表 " + newTableName + (result ? "存在" : "不存在") + " 数据库类型:" + dalType);

            OutMsg("-----------------------------------------");
            if (result)
            {
                result = DBTool.DropTable(newTableName);
                OutMsg("表 " + newTableName + " 删除?" + result);
                OutMsg("-----------------------------------------");
            }

            MDataColumn newMdc = new MDataColumn();

            newMdc.Add("ID", System.Data.SqlDbType.Int);
            newMdc.Add("Name", System.Data.SqlDbType.NVarChar);

            result = DBTool.CreateTable(newTableName, newMdc);
            OutMsg("表 " + newTableName + " 创建?" + result);
            OutMsg("-----------------------------------------");

            newMdc[1].ColumnName = "UserName";
            newMdc[1].AlterOp    = AlterOp.Rename;   //将新创建的表name => username
            newMdc.Add("Password");
            newMdc[2].AlterOp = AlterOp.AddOrModify; // 新增列 Password

            result = DBTool.AlterTable(newTableName, newMdc);
            OutMsg("表 " + newTableName + " 修改结构?" + result);
            OutMsg("-----------------------------------------");

            OutMsg("------------------其它操作-------------------");
            dalType = DBTool.GetDalType("txt path={0}");
            OutMsg("数据库类型为: " + dalType);
            OutMsg("-----------------------------------------");

            OutMsg(DBTool.Keyword("表关键字", DalType.MsSql));//DBTool.NotKeyword 则取消
            OutMsg(DBTool.Keyword("表关键字", DalType.Oracle));
            OutMsg(DBTool.Keyword("表关键字", DalType.MySql));
            OutMsg(DBTool.Keyword("表关键字", DalType.SQLite));

            string changeDataType = DBTool.GetDataType(newMdc[0], DalType.Access, string.Empty);

            OutMsg("数据类型为: " + changeDataType);
            OutMsg("-----------------------------------------");

            string formatValue = DBTool.FormatDefaultValue(DalType.Access, "[#GETDATE]", 1, System.Data.SqlDbType.DateTime);

            OutMsg("Access的日期数据类型为: " + formatValue);
            OutMsg("-----------------------------------------");
        }
예제 #7
0
        /// <summary>
        /// 初始化状态[继承此基类的实体在构造函数中需调用此方法]
        /// </summary>
        /// <param name="entityInstance">实体对象,一般写:this</param>
        /// <param name="tableName">表名,如:Users</param>
        /// <param name="conn">数据链接,单数据库时可写Null,或写默认链接配置项:"Conn",或直接数据库链接字符串</param>
        protected void SetInit(Object entityInstance, string tableName, string conn)
        {
            conn     = string.IsNullOrEmpty(conn) ? AppConfig.DB.DefaultConn : conn;
            entity   = entityInstance;
            typeInfo = entity.GetType();
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = typeInfo.Name;
                    if (tableName.EndsWith(AppConfig.EntitySuffix))
                    {
                        tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length);
                    }
                }

                string key = tableName + MD5.Get(conn);
                if (!CacheManage.LocalInstance.Contains(key))
                {
                    DalType dal      = DBTool.GetDalType(conn);
                    bool    isTxtDal = dal == DalType.Txt || dal == DalType.Xml;
                    string  errMsg   = string.Empty;
                    Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。
                    if (Columns == null || Columns.Count == 0)
                    {
                        if (errMsg != string.Empty)
                        {
                            Error.Throw(errMsg);
                        }
                        Columns = TableSchema.GetColumns(typeInfo);
                        if (!DBTool.ExistsTable(tableName, conn))
                        {
                            if (!DBTool.CreateTable(tableName, Columns, conn))
                            {
                                Error.Throw("Create Table Error:" + tableName);
                            }
                        }
                    }
                    else if (isTxtDal)//文本数据库
                    {
                        if (FieldSource != FieldSource.Data)
                        {
                            MDataColumn c2 = TableSchema.GetColumns(typeInfo);
                            if (FieldSource == FieldSource.BothOfAll)
                            {
                                Columns.AddRange(c2);
                            }
                            else
                            {
                                Columns = c2;
                            }
                        }
                    }

                    if (Columns != null && Columns.Count > 0)
                    {
                        CacheManage.LocalInstance.Add(key, Columns, null, 1440);
                    }
                }
                else
                {
                    Columns = CacheManage.LocalInstance.Get(key) as MDataColumn;
                }

                action = new MAction(Columns.ToRow(tableName), conn);
                if (typeInfo.Name == "SysLogs")
                {
                    action.SetAopOff();
                }
                action.EndTransation();
            }
            catch (Exception err)
            {
                if (typeInfo.Name != "SysLogs")
                {
                    Log.WriteLogToTxt(err);
                }
                throw;
            }
        }
예제 #8
0
        /// <summary>
        /// 将原有的初始化改造成延时加载。
        /// </summary>
        private void SetDelayInit(Object entityInstance, string tableName, string conn, AopOp op)
        {
            if (string.IsNullOrEmpty(conn))
            {
                //不设置链接,则忽略(当成普通的实体类)
                return;
            }
            entity   = entityInstance;
            typeInfo = entity.GetType();
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = typeInfo.Name;
                    if (tableName.EndsWith(AppConfig.EntitySuffix))
                    {
                        tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length);
                    }
                }

                string key = tableName + StaticTool.GetHashKey(conn);
                if (!CacheManage.LocalInstance.Contains(key))
                {
                    DalType dal      = DBTool.GetDalType(conn);
                    bool    isTxtDal = dal == DalType.Txt || dal == DalType.Xml;
                    string  errMsg   = string.Empty;
                    Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。
                    if (Columns == null || Columns.Count == 0)
                    {
                        if (errMsg != string.Empty)
                        {
                            Error.Throw(errMsg);
                        }
                        Columns = ColumnSchema.GetColumns(typeInfo);
                        ConnBean connBean = ConnBean.Create(conn);//下面指定链接,才不会在主从备时被切换到其它库。
                        if (!DBTool.ExistsTable(tableName, connBean.ConnString))
                        {
                            DBTool.ErrorMsg = null;
                            if (!DBTool.CreateTable(tableName, Columns, connBean.ConnString))
                            {
                                Error.Throw("SimpleOrmBase :Create Table " + tableName + " Error:" + DBTool.ErrorMsg);
                            }
                        }
                    }
                    else if (isTxtDal)//文本数据库
                    {
                        if (FieldSource != FieldSource.Data)
                        {
                            MDataColumn c2 = ColumnSchema.GetColumns(typeInfo);
                            if (FieldSource == FieldSource.BothOfAll)
                            {
                                Columns.AddRange(c2);
                            }
                            else
                            {
                                Columns = c2;
                            }
                        }
                    }

                    if (Columns != null && Columns.Count > 0)
                    {
                        CacheManage.LocalInstance.Set(key, Columns, 1440, null);
                    }
                }
                else
                {
                    Columns = CacheManage.LocalInstance.Get(key) as MDataColumn;
                }

                _Action = new MAction(Columns.ToRow(tableName), conn);
                if (typeInfo.Name == "SysLogs")
                {
                    _Action.SetAopState(Aop.AopOp.CloseAll);
                }
                else
                {
                    _Action.SetAopState(op);
                }
                _Action.EndTransation();
            }
            catch (Exception err)
            {
                if (typeInfo.Name != "SysLogs")
                {
                    Log.Write(err, LogType.DataBase);
                }
                throw;
            }
        }