首页 > 开发 > 综合 > 正文

从DACPAC文件中读取元数据

2024-07-21 02:48:46
字体:
来源:转载
供稿:网友
从DACPAC文件中读取元数据

SQL数据库项目生成时会生成dacpac文件,可从中读出所需的元数据,进行一些转换(如生成数据字典)

 

  1. var model = new TSqlModel(@"D:/kljob/CardLan/CardLanDB/bin/Debug/cardlandb.dacpac");
  2.  
  3. xmlWriterSettings settings = new XmlWriterSettings();
  4. settings.Indent = true;
  5. settings.Encoding = new UTF8Encoding(false);
  6. settings.NewLineChars = Environment.NewLine;
  7. MemoryStream ms = new MemoryStream();
  8. XmlWriter writer=XmlWriter.Create(ms,settings);
  9.  
  10. int startID=31;
  11. writer.WriteStartDocument();
  12. writer.WriteStartElement("root");
  13. foreach (var s innew ModelTypeClass[]{ModelSchema.PRocedure ,ModelSchema.View}){
  14.    var allTables = model.GetObjects(DacQueryScopes.UserDefined,s );
  15.    var tableScripts = from t in allTables
  16.                select t ;
  17.       foreach(var x in tableScripts){
  18.          writer.WriteStartElement("QueryObject");
  19.          writer.WriteElementString("ObjectID",startID++.ToString());
  20.          writer.WriteElementString("SourceType", x.ObjectType.Name);
  21.          writer.WriteElementString("ObjectName", x.Name.Parts[1]);
  22.          writer.WriteElementString("Remark","");
  23.  
  24.             int id=0;
  25.             foreach(var c in x.GetChildren())
  26.             {
  27.                writer.WriteStartElement("QueryObjectCols");
  28.                writer.WriteElementString("Title",c.Name.Parts[2]);
  29.                writer.WriteElementString("ObjColID",id++.ToString());
  30.                writer.WriteElementString("Field",c.Name.Parts[2]);
  31.                writer.WriteElementString("Width","80");
  32.                writer.WriteElementString("Sortable","1");
  33.                if(s==ModelSchema.Procedure)
  34.                   writer.WriteElementString("ValueType",c.GetReferenced(Parameter.DataType).First().Name.Parts[0]);
  35.                else
  36.                   writer.WriteElementString("ValueType",c.GetReferenced().First().GetReferenced(Column.DataType).First().Name.Parts[0]);
  37.                writer.WriteElementString("ShoWorder","1");
  38.                writer.WriteEndElement();
  39.  
  40.             }
  41.          writer.WriteEndElement();
  42.          break;
  43.       }
  44.  
  45.  }
  46.  writer.WriteEndElement();
  47.  writer.WriteEndDocument();
  48. writer.Close();
  49. string xml = Encoding.UTF8.GetString(ms.ToArray());
  50. Console.WriteLine(xml);
  51. //tableScripts.Dump();

 

 

 

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表