SQL数据库项目生成时会生成dacpac文件,可从中读出所需的元数据,进行一些转换(如生成数据字典)
- var model = new TSqlModel(@"D:kljobCardLanCardLanDBinDebugcardlandb.dacpac");
- XmlWriterSettings settings = new XmlWriterSettings();
- settings.Indent = true;
- settings.Encoding = new UTF8Encoding(false);
- settings.NewLineChars = Environment.NewLine;
- MemoryStream ms = new MemoryStream();
- XmlWriter writer=XmlWriter.Create(ms,settings);
- int startID=31;
- writer.WriteStartDocument();
- writer.WriteStartElement("root");
- foreach (var s in new ModelTypeClass[]{ModelSchema.Procedure ,ModelSchema.View}){
- var allTables = model.GetObjects(DacQueryScopes.UserDefined,s );
- var tableScripts = from t in allTables
- select t ;
- foreach(var x in tableScripts){
- writer.WriteStartElement("QueryObject");
- writer.WriteElementString("ObjectID",startID++.ToString());
- writer.WriteElementString("SourceType", x.ObjectType.Name);
- writer.WriteElementString("ObjectName", x.Name.Parts[1]);
- writer.WriteElementString("Remark","");
- int id=0;
- foreach(var c in x.GetChildren())
- {
- writer.WriteStartElement("QueryObjectCols");
- writer.WriteElementString("Title",c.Name.Parts[2]);
- writer.WriteElementString("ObjColID",id++.ToString());
- writer.WriteElementString("Field",c.Name.Parts[2]);
- writer.WriteElementString("Width","80");
- writer.WriteElementString("Sortable","1");
- if(s==ModelSchema.Procedure)
- writer.WriteElementString("ValueType",c.GetReferenced(Parameter.DataType).First().Name.Parts[0]);
- else
- writer.WriteElementString("ValueType",c.GetReferenced().First().GetReferenced(Column.DataType).First().Name.Parts[0]);
- writer.WriteElementString("ShowOrder","1");
- writer.WriteEndElement();
- }
- writer.WriteEndElement();
- break;
- }
- }
- writer.WriteEndElement();
- writer.WriteEndDocument();
- writer.Close();
- string xml = Encoding.UTF8.GetString(ms.ToArray());
- Console.WriteLine(xml);
- //tableScripts.Dump();