Esempio n. 1
0
        /// <summary>
        /// 生成数据库文档
        /// </summary>
        /// <param name="tablesName"></param>
        public void ToWord(List <string> tablesName)
        {
            ProcTable pt = new ProcTable();
            DataSet   dsColumnsDescription = pt.GetTablesColumnDecription(tablesName);

            string   fullPath = path + "/数据库文档.doc";
            ProcWord word     = new ProcWord();

            word.FileName = fullPath;

            foreach (string tableName in tablesName)
            {
                TableInfo tableInfo            = new TableInfo(strNameSpace, strPrefix, tableName);
                DataSet   dsTableInfo          = tableInfo.dsTableInfo;                  //0表名  1列类型  2标识列
                DataTable dtColumnType         = tableInfo.ColumnType;                   //列名0 类型1 长度3 可空6
                DataTable dtIdentity           = tableInfo.dsTableInfo.Tables[2];        //列名0 seed 1   increment 2
                string[]  keyColumns           = tableInfo.KeyColumns;                   //主键列
                DataTable dtColumnsDescription = pt.GetTableColumnDecription(tableName); //列名0 说明1

                //默认值
                DataTable dtConstraint = null;//constraint_type(DEFAULT)   constraint_keys默认值
                //表关系
                DataTable dtRelation = null;
                if (tableInfo.dsTableInfo.Tables.Count > 6)
                {
                    dtConstraint = tableInfo.dsTableInfo.Tables[6]; //constraint_type(DEFAULT)   constraint_keys默认值
                    dtRelation   = tableInfo.dsTableInfo.Tables[6]; //column 0
                }

                word.WriteText("表名:" + tableName + "  [" + tableInfo.tableDescription + "]");
                int rows = dtColumnType.Rows.Count;
                word.AddParagraph();
                word.AddTable(rows + 1, 7);
                #region 添加列头
                word.AddTableContent(1, 1, "列名", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                word.AddTableContent(1, 2, "类型(长度)", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                word.AddTableContent(1, 3, "可空", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                word.AddTableContent(1, 4, "默认值", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                word.AddTableContent(1, 5, "标识", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                word.AddTableContent(1, 6, "主键", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                word.AddTableContent(1, 7, "列说明", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                #endregion
                ProcString ps                = new ProcString();
                string     strColName        = "";
                string     strIdentity       = "";
                string     strPrimary        = "";
                string     strDefault        = "";
                string     strColDescription = "";
                string     tem               = "";
                int        j = 1;
                for (int i = 0; i < rows; i++)
                {
                    j++;
                    //列名
                    strColName = dtColumnType.Rows[i][0].ToString();
                    word.AddTableContent(j, 1, strColName);
                    //列类型(长度)
                    word.AddTableContent(j, 2, dtColumnType.Rows[i][1].ToString() + "(" + dtColumnType.Rows[i][3].ToString() + ")");
                    //可空
                    word.AddTableContent(j, 3, dtColumnType.Rows[i][6].ToString());
                    //默认值
                    #region 默认值
                    if (null != dtConstraint && dtConstraint.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtConstraint.Rows)
                        {
                            tem = dr[0].ToString();
                            if (tem.IndexOf("DEFAULT") >= 0)
                            {
                                tem = tem.Substring(18);
                                if (tem == strColName)
                                {
                                    strDefault = dr["constraint_keys"].ToString();
                                    break;
                                }
                            }
                            strDefault = "";
                        }
                    }
                    #endregion
                    word.AddTableContent(j, 4, strDefault);
                    //标识
                    #region 标识
                    if (null != dtIdentity && dtIdentity.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtIdentity.Rows)
                        {
                            if (strColName == dr[0].ToString())
                            {
                                strIdentity = "yes(" + dr[1].ToString() + "," + dr[2].ToString() + ")";
                                break;
                            }
                            strIdentity = "";
                        }
                    }
                    #endregion
                    word.AddTableContent(j, 5, strIdentity);
                    //主键
                    strPrimary = ps.IsContains(strColName, tableInfo.KeyColumns) ? "yes" : "";
                    word.AddTableContent(j, 6, strPrimary);
                    //列说明
                    #region 列说明
                    if (null != dtColumnsDescription && dtColumnsDescription.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtColumnsDescription.Rows)
                        {
                            if (strColName == dr[0].ToString())
                            {
                                strColDescription = dr[1].ToString();
                                break;
                            }
                            strColDescription = "";
                        }
                    }
                    #endregion
                    word.AddTableContent(j, 7, strColDescription);
                }

                #region 表关系
                if (dtRelation.Rows.Count > 0)
                {
                    tem = "";
                    foreach (DataRow dr in dtRelation.Rows)
                    {
                        if (dr["status_enabled"].ToString() == "Enabled")
                        {
                            tem += dr["constraint_name"].ToString() + "[" + dr["constraint_keys"].ToString() + "]" + " , ";
                        }
                    }
                    if (tem.EndsWith(" , "))
                    {
                        tem = tem.Substring(0, tem.Length - 3);

                        word.WriteText("表关系:" + tem);
                    }
                }
                #endregion

                word.AddParagraph();
                word.AddParagraph();
            }//end foreach
            word.SaveAs();
        }
Esempio n. 2
0
        /// <summary>
        /// 单表时,生成实例类文件
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public string ToModel(bool confirmWrite, string tableName)
        {
            StringBuilder ret = new StringBuilder();
            ProcTable     pt  = new ProcTable();
            ProcString    ps  = new ProcString();

            ps.SetTabs(strNameSpace);
            DataTable dtColumnDescription = pt.GetTableColumnDecription(tableName);

            string strTableDescription = pt.GetTableDesciption(tableName);

            DataTable dtColumnType = pt.GetTableInfo(tableName).Tables[1];

            string strClassName = ps.GetClassNameByTableName(tableName, strPrefix);

            StringBuilder sbBody    = new StringBuilder();
            TableInfo     tableInfo = new TableInfo(strNameSpace, strPrefix, tableName);

            foreach (DataRow drType in dtColumnDescription.Rows)
            {
                string columnName           = drType["ColumnName"].ToString();        //列名
                string columnType           = drType["ColumnDescription"].ToString(); //列数据类型
                string firstLowerColumnName = "";                                     //首字母小写,用于字段
                string firstUpperColumnName = "";                                     //首字母大写,用于属性

                #region 以下为字段
                foreach (DataRow drDescription in dtColumnDescription.Rows)
                {
                    if (drDescription["ColumnName"].ToString() == columnName)
                    {
                        string strColumnDescription = drDescription["ColumnDescription"].ToString();
                        sbBody.AppendLine(ps.tabMember + "/// <summary>");
                        sbBody.AppendLine(ps.tabMember + "/// " + strColumnDescription);
                        sbBody.AppendLine(ps.tabMember + "/// <summary>");
                    }
                }
                firstLowerColumnName = ps.ConvertStringToUpperOrLower(columnName, false);
                //if (!string.IsNullOrEmpty(columnDefault))
                //{
                //    if (columnName.ToUpper() != "HOSPITAL_NO")
                //    {
                //        sbBody.AppendLine(ps.tabMember + "private " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName + "=\"" + columnDefault + "\";");
                //    }
                //}
                //else
                //{
                sbBody.AppendLine(ps.tabMember + "private " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName + ";");
                //}
                #endregion

                #region 以下为属性
                foreach (DataRow drDescription in dtColumnDescription.Rows)
                {
                    if (drDescription["ColumnName"].ToString() == columnName)
                    {
                        string strColumnDescription = drDescription["ColumnDescription"].ToString();
                        sbBody.AppendLine(ps.tabMember + "/// <summary>");
                        sbBody.AppendLine(ps.tabMember + "/// " + strColumnDescription);
                        sbBody.AppendLine(ps.tabMember + "/// <summary>");
                    }
                }
                firstUpperColumnName = ps.ConvertStringToUpperOrLower(columnName, true);
                sbBody.AppendLine(ps.tabMember + "public " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstUpperColumnName);
                sbBody.AppendLine(ps.tabMember + "{");
                sbBody.AppendLine(ps.tabLocalVar + "get{ return " + firstLowerColumnName + "; }");
                sbBody.AppendLine(ps.tabLocalVar + "set{ " + firstLowerColumnName + " = value; }");
                sbBody.AppendLine(ps.tabMember + "}");
                #endregion
                sbBody.AppendLine();
            }
            string strClass = ps.GetCLassByModel(strNameSpace, strPrefix, tableName, sbBody.ToString());
            ret.Append(strClass);

            #region 写文件
            if (confirmWrite)
            {
                string tem = path + "/Model/";
                if (!Directory.Exists(tem))
                {
                    Directory.CreateDirectory(tem);
                }
                string fullpath = tem + strClassName + ".cs";

                Write(fullpath, ret.ToString());
            }
            #endregion

            return(ret.ToString());
        }