Beispiel #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();
        }