Exemple #1
0
        private void viewDataShace(string filename)
        {
            Stream fs = null;

            try
            {
                string dacfile = filename;
                fs = File.Open(dacfile, FileMode.OpenOrCreate) as Stream;

                Microsoft.SqlServer.Dac.Model.ModelLoadOptions mlo = new Microsoft.SqlServer.Dac.Model.ModelLoadOptions();
                mlo.ModelStorageType = DacSchemaModelStorageType.Memory;
                var model = Microsoft.SqlServer.Dac.Model.TSqlModel.LoadFromDacpac(fs, mlo);
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
                this.localtree.Nodes.Clear();
                lmst.Clear();
                List <TabInfoClass> lstemp = null;
                foreach (var s in new Microsoft.SqlServer.Dac.Model.ModelTypeClass[] { Microsoft.SqlServer.Dac.Model.ModelSchema.Table, Microsoft.SqlServer.Dac.Model.ModelSchema.View, Microsoft.SqlServer.Dac.Model.ModelSchema.Procedure })
                {
                    Application.DoEvents();//var allTables = model.GetObjects(DacQueryScopes.All, ModelSchema.Table);
                    var allTables    = model.GetObjects(Microsoft.SqlServer.Dac.Model.DacQueryScopes.UserDefined, s);
                    var tableScripts = from t in allTables

                                       select t;
                    lstemp = new List <TabInfoClass>();
                    //  Microsoft.SqlServer.Dac.Model.TSqlObject tob = new Microsoft.SqlServer.Dac.Model.TSqlObject();

                    // Microsoft.SqlServer.TransactSql.ScriptDom.TSqlScript
                    // Microsoft.SqlServer.Dac.Model.TSqlObjectOptions
                    // Microsoft.SqlServer.Dac.TSqlModelUtils

                    // tableScripts.OrderBy<Microsoft.SqlServer.Dac.Model.TSqlObject, Microsoft.SqlServer.Dac.Model.ObjectIdentifier>(t=>t.Name);
                    foreach (var x in tableScripts)
                    {
                        string on = x.ObjectType.Name; //类型
                        string nm = x.Name.Parts[1];   //名字
                        string jb = x.GetScript();

                        mymodel mym = new mymodel();
                        mym.tab = nm;
                        mym.sql = jb;
                        lmst.Add(mym);
                        TabInfoClass tabc = new TabInfoClass();
                        tabc.TableName = nm;
                        tabc.FiledName = new List <fileds>();
                        //  List<TreeNode> lst = new List<TreeNode>();
                        foreach (var c in x.GetChildren())
                        {
                            Application.DoEvents();
                            try {
                                string field = c.Name.Parts[2].ToString();

                                string valtype = c.GetReferenced(Microsoft.SqlServer.Dac.Model.Column.DataType).First().Name.Parts[0];


                                fileds fds = new fileds();
                                fds.Fname = field;
                                fds.Ftype = valtype;
                                tabc.FiledName.Add(fds);
                            }catch (Exception error)
                            {
                                continue;
                            }
                        }
                        lstemp.Add(tabc);
                        stab.Add(tabc);
                    }
                    var sortedList =
                        (from a in lstemp
                         orderby a.TableName
                         select a).ToList();

                    for (int i = 0; i <= sortedList.Count - 1; i++)
                    {
                        TreeNode[] treearray = new TreeNode[sortedList[i].FiledName.Count];

                        var fList =
                            (from a in sortedList[i].FiledName
                             orderby a.Fname
                             select a).ToList();
                        for (int j = 0; j < fList.Count; j++)
                        {
                            Application.DoEvents();
                            treearray[j]          = new TreeNode();
                            treearray[j].Text     = fList[j].Fname;
                            treearray[j].ImageKey = "add";
                        }

                        TreeNode tn = new TreeNode(sortedList[i].TableName, treearray);
                        this.localtree.Nodes.Add(tn);
                    }
                }
            }
            catch (Exception error)
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
                MessageBox.Show(error.Message);
            }
        }
Exemple #2
0
        private void viewDataShace2(string filename)
        {
            Stream fs = null;

            try
            {
                string dacfile = filename;
                fs = File.Open(dacfile, FileMode.OpenOrCreate) as Stream;

                Microsoft.SqlServer.Dac.Model.ModelLoadOptions mlo = new Microsoft.SqlServer.Dac.Model.ModelLoadOptions();
                mlo.ModelStorageType = DacSchemaModelStorageType.Memory;
                var model = Microsoft.SqlServer.Dac.Model.TSqlModel.LoadFromDacpac(fs, mlo);
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
                this.remotree.Nodes.Clear();
                lmst2.Clear();
                List <TabInfoClass> lstemp = null;
                foreach (var s in new Microsoft.SqlServer.Dac.Model.ModelTypeClass[] { Microsoft.SqlServer.Dac.Model.ModelSchema.Table, Microsoft.SqlServer.Dac.Model.ModelSchema.View, Microsoft.SqlServer.Dac.Model.ModelSchema.Procedure })
                {
                    Application.DoEvents();//var allTables = model.GetObjects(DacQueryScopes.All, ModelSchema.Table);
                    var allTables    = model.GetObjects(Microsoft.SqlServer.Dac.Model.DacQueryScopes.UserDefined, s);
                    var tableScripts = from t in allTables

                                       select t;
                    lstemp = new List <TabInfoClass>();


                    foreach (var x in tableScripts)
                    {
                        Application.DoEvents();
                        string  on  = x.ObjectType.Name; //类型
                        string  nm  = x.Name.Parts[1];   //名字
                        string  jb  = x.GetScript();
                        mymodel mym = new mymodel();
                        mym.tab = nm;
                        mym.sql = jb;
                        lmst2.Add(mym);
                        TabInfoClass tabc = new TabInfoClass();
                        tabc.TableName = nm;
                        tabc.FiledName = new List <fileds>();
                        foreach (var c in x.GetChildren())
                        {
                            Application.DoEvents();
                            try
                            {
                                string field   = c.Name.Parts[2].ToString();
                                string valtype = c.GetReferenced(Microsoft.SqlServer.Dac.Model.Column.DataType).First().Name.Parts[0];
                                fileds fds     = new fileds();
                                fds.Fname = field;
                                fds.Ftype = valtype;
                                tabc.FiledName.Add(fds);
                            }
                            catch (Exception error)
                            {
                                continue;
                            }
                        }
                        lstemp.Add(tabc);
                        dtab.Add(tabc);
                    }
                    CreateRemotree(lstemp);
                }
            }
            catch (Exception error)
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
                MessageBox.Show(error.Message);
            }
        }