CUBRID学习笔记 16 元数据支持

简化了很多 ,在sqlserver需要用语句实现的功能

接口如下

public DataTable GetDatabases(string[] filters)
public DataTable GetTables(string[] filters)
public DataTable GetViews(string[] filters)
public DataTable GetColumns(string[] filters)
public DataTable GetIndexes(string[] filters)
public DataTable GetIndexColumns(string[] filters)
public DataTable GetExportedKeys(string[] filters)
public DataTable GetCrossReferenceKeys(string[] filters)
public DataTable GetForeignKeys(string[] filters)
public DataTable GetUsers(string[] filters)
public DataTable GetProcedures(string[] filters)
public static DataTable GetDataTypes()
public static DataTable GetReservedWords()
public static String[] GetNumericFunctions()
public static String[] GetStringFunctions()
public DataTable GetSchema(string collection, string[] filters)

获取数据库
CUBRIDSchemaProvider schema = new CUBRIDSchemaProvider(conn);
DataTable dt = schema.GetTables(new string[] { "%" });
Debug.Assert(dt.Columns.Count == 3);
Debug.Assert(dt.Rows.Count == 10);
Debug.Assert(dt.Rows[0][0].ToString() == "demodb");
Debug.Assert(dt.Rows[0][1].ToString() == "demodb");
Debug.Assert(dt.Rows[0][2].ToString() == "stadium");

获取表的外键
CUBRIDSchemaProvider schema = new CUBRIDSchemaProvider(conn);
DataTable dt = schema.GetForeignKeys(new string[] { "game" });
Debug.Assert(dt.Columns.Count == 9);
Debug.Assert(dt.Rows.Count == 2);
Debug.Assert(dt.Rows[0][0].ToString() == "athlete");
Debug.Assert(dt.Rows[0][1].ToString() == "code");
Debug.Assert(dt.Rows[0][2].ToString() == "game");
Debug.Assert(dt.Rows[0][3].ToString() == "athlete_code");
Debug.Assert(dt.Rows[0][4].ToString() == "1");
Debug.Assert(dt.Rows[0][5].ToString() == "1");
Debug.Assert(dt.Rows[0][6].ToString() == "1");
Debug.Assert(dt.Rows[0][7].ToString() == "fk_game_athlete_code");
Debug.Assert(dt.Rows[0][8].ToString() == "pk_athlete_code");


获取索引

CUBRIDSchemaProvider schema = new CUBRIDSchemaProvider(conn);
DataTable dt = schema.GetIndexes(new string[] { "game" });

Debug.Assert(dt.Columns.Count == 9);
Debug.Assert(dt.Rows.Count == 5);

Debug.Assert(dt.Rows[3][2].ToString() == "pk_game_host_year_event_code_athlete_code"); //verify index name
Debug.Assert(dt.Rows[3][4].ToString() == "True"); //Is it a primary key?

完整例子
using CUBRID.Data.CUBRIDClient;
using System.Diagnostics;
using System.Data;

namespace MetadataExample
{
    class Program
    {
        static void Main(string[] args)
        {
            CUBRIDConnectionStringBuilder sb = new CUBRIDConnectionStringBuilder("localhost", "demodb", "public", "", "33000");
            using (CUBRIDConnection conn = new CUBRIDConnection(sb.GetConnectionString()))
            {
                conn.Open();

                CUBRIDSchemaProvider schema = new CUBRIDSchemaProvider(conn);

                //get tables example
                using (DataTable dt = schema.GetTables(new string[] { "%" }))
                {
                    Debug.Assert(dt.Columns.Count == 3);
                    Debug.Assert(dt.Rows.Count == 10);

                    Debug.Assert(dt.Rows[0][0].ToString() == "demodb");
                    Debug.Assert(dt.Rows[0][1].ToString() == "demodb");
                    Debug.Assert(dt.Rows[0][2].ToString() == "stadium");
                }

                //get columns example
                using (DataTable dt = schema.GetColumns(new string[] { "game" }))
                {
                    Debug.Assert(dt.Columns.Count == 11);
                    Debug.Assert(dt.Rows.Count == 7);

                    Debug.Assert(dt.Rows[0][3].ToString() == "host_year");
                    Debug.Assert(dt.Rows[1][3].ToString() == "event_code");
                }

                //get users example
                using (DataTable dt = schema.GetUsers(null))
                {
                    Debug.Assert(dt.Columns.Count == 1);
                    Debug.Assert(dt.Rows.Count >= 2);

                    Debug.Assert(dt.Rows[0][0].ToString().ToUpper() == "DBA");
                    Debug.Assert(dt.Rows[1][0].ToString().ToUpper() == "PUBLIC");
                }

                //get views example
                using (DataTable dt = schema.GetViews(null))
                {
                    Debug.Assert(dt.Columns.Count == 3);
                    Debug.Assert(dt.Rows.Count == 0);
                }

                //get foreign keys example
                using (DataTable dt = schema.GetForeignKeys(new string[] { "game" }))
                {
                    Debug.Assert(dt.Columns.Count == 9);
                    Debug.Assert(dt.Rows.Count == 2);

                    Debug.Assert(dt.Rows[0][0].ToString() == "athlete");
                    Debug.Assert(dt.Rows[0][1].ToString() == "code");
                    Debug.Assert(dt.Rows[0][2].ToString() == "game");
                    Debug.Assert(dt.Rows[0][3].ToString() == "athlete_code");
                    Debug.Assert(dt.Rows[0][4].ToString() == "1");
                    Debug.Assert(dt.Rows[0][5].ToString() == "1");
                    Debug.Assert(dt.Rows[0][6].ToString() == "1");
                    Debug.Assert(dt.Rows[0][7].ToString() == "fk_game_athlete_code");
                    Debug.Assert(dt.Rows[0][8].ToString() == "pk_athlete_code");

                    Debug.Assert(dt.Rows[1][0].ToString() == "event");
                    Debug.Assert(dt.Rows[1][1].ToString() == "code");
                    Debug.Assert(dt.Rows[1][2].ToString() == "game");
                    Debug.Assert(dt.Rows[1][3].ToString() == "event_code");
                    Debug.Assert(dt.Rows[1][4].ToString() == "1");
                    Debug.Assert(dt.Rows[1][5].ToString() == "1");
                    Debug.Assert(dt.Rows[1][6].ToString() == "1");
                    Debug.Assert(dt.Rows[1][7].ToString() == "fk_game_event_code");
                    Debug.Assert(dt.Rows[1][8].ToString() == "pk_event_code");
                }

                //get indexes example
                using (DataTable dt = schema.GetIndexes(new string[] { "game" }))
                {
                    Debug.Assert(dt.Columns.Count == 9);
                    Debug.Assert(dt.Rows.Count == 5);

                    Debug.Assert(dt.Rows[3][2].ToString() == "pk_game_host_year_event_code_athlete_code"); //Index name
                    Debug.Assert(dt.Rows[3][4].ToString() == "True"); //Is PK?
                }

                conn.Close();
            }
        }
    }
}

欢迎转载 ,转载时请保留作者信息。本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com 。 过错

 

adonet的api http://www.cubrid.org/manual/api/ado.net/8.4.1/Index.html













posted @ 2016-03-16 16:14  过错  阅读(277)  评论(0编辑  收藏  举报