예제 #1
0
        //private const string Catalog = "3plogic";
        //private const string Password = "******";
        //private const string SystemName = "W500DEV";
        //private const string Username = "******";
        /// <summary>
        /// gets attributes of all tables
        /// </summary>
        /// <param name="tableNameToFind">if empty, get all tables, otherwise, just tablename specified</param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static List<FullMeta> TableAttributesList(string tableNameToFind, string connectionString)
        {
            var results = new List<FullMeta>();

            // Pull out of connectionString the Catalog
            // Catalog=3plogic;
            const string catalogSearchString = "Catalog=";
            int pos1 = connectionString.IndexOf(catalogSearchString, StringComparison.Ordinal) + catalogSearchString.Length;
            int pos2 = connectionString.Substring(pos1).IndexOf(";", StringComparison.Ordinal) + pos1;
            string catalogName = connectionString.Substring(pos1, pos2 - pos1).ToLower();

            using (var sqlConnection = new SqlConnection(connectionString))
            {
                var serverConnection =
                    new ServerConnection(sqlConnection);

                var svr = new Server(serverConnection);

                StringCollection sc = svr.GetDefaultInitFields(typeof (Table));
                svr.SetDefaultInitFields(typeof (View), sc);

                DatabaseCollection databaseCollection = svr.Databases;

                if (databaseCollection.Count > 0)
                {
                    foreach (Database database in databaseCollection)
                    {
                        // only process tables in our catalog
                        if (!database.Name.ToLower().Equals(catalogName))
                        {
                            continue;
                        }

                        TableCollection tableCollection = database.Tables;
                        foreach (Table table in tableCollection)
                        {
                            if (table.IsSchemaOwned && !table.IsSystemObject)
                            {
                                try
                                {
                                    if (String.IsNullOrEmpty(tableNameToFind) ||
                                        table.Name.ToLower().Equals(tableNameToFind.ToLower()))
                                    {
                                        string xml = CreateXMLFromTableSqlServer(table.Name, sqlConnection);
                                        List<AttributeInfo> listAttributeInfo = ParseXMLForAttributes(xml);
                                        results.Add(new FullMeta(database.Name, table.Name, listAttributeInfo));
                                    }
                                }
                                catch (Exception)
                                {
                                    // todo:  figure out how to skip tables that are not ours
                                    Debug.WriteLine("problem: " + table.Name);
                                }
                            }
                        }
                    }
                }
            }
            return results;
        }