コード例 #1
0
ファイル: DBTool.cs プロジェクト: cardinals/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;
            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.IsRecordDebugInfo = false;
                        proc.SetAopState(Aop.AopOp.CloseAll);
                        proc.ResetProc(GetCreateTableSql(tableName, columns, proc.DalType, proc.DalVersion));    //.Replace("\n", string.Empty)
                        result = proc.ExeNonQuery() > -2;
                        if (result)
                        {
                            //获取扩展说明
                            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.Write(err, LogType.DataBase);
                    }
                    finally
                    {
                        if (proc.RecordsAffected == -2)
                        {
                            _ErrorMsg.AppendLine("CreateTable:" + proc.DebugInfo);
                        }
                    }
                }
                break;
            }
            if (result)
            {
                TableSchema.Add(tableName, "U", conn);//修改缓存。
            }
            return(result);
        }