コード例 #1
0
        private static bool FillSchemaFromDb(ref MDataRow row, ref DbBase dbBase, string tableName, string sourceTableName)
        {
            try
            {
                MDataColumn mdcs = null;
                //if (tableName.IndexOf('(') > -1 && tableName.IndexOf(')') > -1)//自定义视图table
                //{
                //    dbBase.tempSql = "view";//使用access方式加载列
                //}
                mdcs = GetColumns(tableName, ref dbBase);
                if (mdcs.Count == 0)
                {
                    return(false);
                }
                row           = mdcs.ToRow(sourceTableName);
                row.TableName = sourceTableName;
                string key = GetSchemaKey(tableName, dbBase.DataBase, dbBase.dalType);
                CacheManage.LocalInstance.Add(key, mdcs.Clone(), null, 1440);

                switch (dbBase.dalType)//文本数据库不保存。
                {
                case DalType.Access:
                case DalType.SQLite:
                case DalType.MsSql:
                case DalType.MySql:
                case DalType.Oracle:
                    if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath))
                    {
                        string folderPath = AppDomain.CurrentDomain.BaseDirectory + AppConfig.DB.SchemaMapPath;
                        if (System.IO.Directory.Exists(folderPath))
                        {
                            mdcs.WriteSchema(folderPath + key + ".ts");
                        }
                    }
                    break;
                }
                return(true);
            }
            catch (Exception err)
            {
                Log.WriteLogToTxt(err);
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// 为指定的表架构生成SQL(Create Table)语句
        /// </summary>
        public static bool CreateTable(string tableName, MDataColumn columns, string conn)
        {
            if (string.IsNullOrEmpty(tableName) || tableName.Contains("(") && tableName.Contains(")"))
            {
                return(false);
            }
            bool    result   = false;
            DalType dalType  = GetDalType(conn);
            string  dataBase = string.Empty;

            switch (dalType)
            {
            case DalType.Txt:
            case DalType.Xml:
                // string a, b, c;
                conn = AppConfig.GetConn(conn);       // CYQ.Data.DAL.DalCreate.GetConnString(conn, out a, out b, out c);
                if (conn.ToLower().Contains(";ts=0")) //不写入表架构。
                {
                    return(true);
                }
                else
                {
                    tableName = Path.GetFileNameWithoutExtension(tableName);
                    string fileName = NoSqlConnection.GetFilePath(conn) + tableName + ".ts";
                    result   = columns.WriteSchema(fileName);
                    dataBase = new NoSqlConnection(conn).Database;
                }
                break;

            default:
                using (MProc proc = new MProc(null, conn))
                {
                    dataBase = proc.DataBase;
                    try
                    {
                        proc.dalHelper.IsAllowRecordSql = false;
                        proc.SetAopState(Aop.AopOp.CloseAll);
                        proc.ResetProc(GetCreateTableSql(tableName, columns, proc.DalType, proc.DalVersion));    //.Replace("\n", string.Empty)
                        result = proc.ExeNonQuery() > -2;

                        //获取扩展说明
                        string descriptionSql = GetCreateTableDescriptionSql(tableName, columns, proc.DalType).Replace("\r\n", " ").Trim(' ', ';');
                        if (!string.IsNullOrEmpty(descriptionSql))
                        {
                            if (proc.DalType == DalType.Oracle)
                            {
                                foreach (string sql in descriptionSql.Split(';'))
                                {
                                    proc.ResetProc(sql);
                                    if (proc.ExeNonQuery() == -2)
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                proc.ResetProc(descriptionSql);
                                proc.ExeNonQuery();
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        Log.WriteLogToTxt(err);
                    }
                }
                break;
            }
            if (result)
            {
                //处理表缓存
                string key = TableSchema.GetTableCacheKey(dalType, dataBase, conn);
                if (TableSchema.tableCache.ContainsKey(key))
                {
                    Dictionary <string, string> tableDic = TableSchema.tableCache[key];
                    if (!tableDic.ContainsKey(tableName))
                    {
                        tableDic.Add(tableName, "");
                    }
                }
            }
            return(result);
        }
コード例 #3
0
ファイル: DBTool.cs プロジェクト: zjy040191/cyqdata
        /// <summary>
        /// 为指定的表架构生成SQL(Create Table)语句
        /// </summary>
        public static bool CreateTable(string tableName, MDataColumn columns, string conn)
        {
            if (string.IsNullOrEmpty(tableName) || tableName.Contains("(") && tableName.Contains(")"))
            {
                return(false);
            }
            bool         result   = false;
            DataBaseType dalType  = GetDataBaseType(conn);
            string       dataBase = string.Empty;

            switch (dalType)
            {
            case DataBaseType.Txt:
            case DataBaseType.Xml:
                // string a, b, c;
                conn = AppConfig.GetConn(conn);       // CYQ.Data.DAL.DalCreate.GetConnString(conn, out a, out b, out c);
                if (conn.ToLower().Contains(";ts=0")) //不写入表架构。
                {
                    //增加缓存

                    result = true;
                }
                else
                {
                    tableName = Path.GetFileNameWithoutExtension(tableName);
                    string fileName = NoSqlConnection.GetFilePath(conn) + tableName + ".ts";
                    result   = columns.WriteSchema(fileName);
                    dataBase = GetDBInfo(conn).DataBaseName;
                }
                break;

            default:
                #region MyRegion


                using (MProc proc = new MProc(null, conn))
                {
                    dataBase = proc.DataBaseName;
                    try
                    {
                        proc.dalHelper.IsRecordDebugInfo = false;
                        proc.SetAopState(Aop.AopOp.CloseAll);
                        proc.ResetProc(GetCreateTableSql(tableName, columns, proc.DataBaseType, proc.DataBaseVersion));    //.Replace("\n", string.Empty)
                        result = proc.ExeNonQuery() > -2;
                        if (result)
                        {
                            //获取扩展说明
                            string descriptionSql = GetCreateTableDescriptionSql(tableName, columns, proc.DataBaseType).Replace("\r\n", " ").Trim(' ', ';');
                            if (!string.IsNullOrEmpty(descriptionSql))
                            {
                                if (proc.DataBaseType == DataBaseType.Oracle)
                                {
                                    foreach (string sql in descriptionSql.Split(';'))
                                    {
                                        proc.ResetProc(sql);
                                        if (proc.ExeNonQuery() == -2)
                                        {
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    proc.ResetProc(descriptionSql);
                                    proc.ExeNonQuery();
                                }
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        Log.Write(err, LogType.DataBase);
                    }
                    finally
                    {
                        if (proc.RecordsAffected == -2)
                        {
                            _ErrorMsg.AppendLine("CreateTable:" + proc.DebugInfo);
                        }
                    }
                }
                #endregion
                break;
            }
            if (result)
            {
                CrossDB.Add(tableName, "U", conn);//修改缓存。
            }
            return(result);
        }
コード例 #4
0
ファイル: DBTool.cs プロジェクト: Feng2012/cyqdata
        /// <summary>
        /// 为指定的表架构生成SQL(Create Table)语句
        /// </summary>
        public static bool CreateTable(string tableName, MDataColumn columns, string conn)
        {
            if (string.IsNullOrEmpty(tableName) || tableName.Contains("(") && tableName.Contains(")"))
            {
                return false;
            }
            bool result = false;
            switch (GetDalType(conn))
            {
                case DalType.Txt:
                case DalType.Xml:
                    // string a, b, c;
                    conn = AppConfig.GetConn(conn);// CYQ.Data.DAL.DalCreate.GetConnString(conn, out a, out b, out c);
                    if (conn.ToLower().Contains(";ts=0"))//不写入表架构。
                    {
                        return true;
                    }
                    else
                    {
                        tableName = Path.GetFileNameWithoutExtension(tableName);
                        string fileName = NoSqlConnection.GetFilePath(conn) + tableName + ".ts";
                        result = columns.WriteSchema(fileName);
                    }
                    break;
                default:
                    using (MProc proc = new MProc(null, conn))
                    {
                        try
                        {
                            proc.dalHelper.IsAllowRecordSql = false;
                            proc.SetAopOff();
                            proc.ResetProc(GetCreateTableSql(tableName, columns, proc.DalType, proc.DalVersion));//.Replace("\n", string.Empty)
                            result = proc.ExeNonQuery() > -2;

                            //获取扩展说明
                            string descriptionSql = GetCreateTableDescriptionSql(tableName, columns, proc.DalType).Replace("\r\n", string.Empty).Trim(' ', ';');
                            if (!string.IsNullOrEmpty(descriptionSql))
                            {
                                if (proc.DalType == DalType.Oracle)
                                {
                                    foreach (string sql in descriptionSql.Split(';'))
                                    {
                                        proc.ResetProc(sql);
                                        if (proc.ExeNonQuery() == -2)
                                        {
                                            break;
                                        }

                                    }
                                }
                                else
                                {
                                    proc.ResetProc(descriptionSql);
                                    proc.ExeNonQuery();
                                }
                            }

                        }
                        catch (Exception err)
                        {
                            Log.WriteLogToTxt(err);
                        }
                    }
                    break;

            }
            return result;
        }