Example #1
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);
        }
Example #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;
            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);
        }