获取表元数据

可以通过对连接使用 GetSchema() 方法并将元数据加载到 DataTable 中来获取表元数据:

DataTable table = _conn.GetSchema("Tables", new string[] { database_name, schema_name, table_name, table_type });

例如:

DataTable table = _conn.GetSchema("Tables", new string[] { null, null, null, "SYSTEM TABLE" });

database_nameschema_nametable_name 可以设置为 null,也可以设置为特定名称,或者也可以使用 LIKE 模式。

table_type 可以设置为以下值之一:

  • "SYSTEM TABLE"

  • "TABLE"

  • "GLOBAL TEMPORARY"

  • "LOCAL TEMPORARY"

  • "VIEW"

  • NULL

如果 table_type 设置为 null,则会返回所有元数据表的元数据。

示例用法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Vertica.Data.VerticaClient;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //configure connection properties
            VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();
            builder.Host = "192.168.1.10";
            builder.Database = "VMart";
            builder.User = "dbadmin";

            //open the connection
            VerticaConnection _conn = new VerticaConnection(builder.ToString());
            _conn.Open();

            //create a new data table containing the schema
            //the last argument can be "SYSTEM TABLE", "TABLE", "GLOBAL TEMPORARY",
            // "LOCAL TEMPORARY", "VIEW", or null for all types
            DataTable table = _conn.GetSchema("Tables", new string[] { null, null, null, "SYSTEM TABLE" });

            //print out the schema
            foreach (DataRow row in table.Rows) {
                foreach (DataColumn col in table.Columns)
                {
                    Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
                }
                Console.WriteLine("============================");
            }

            //close the connection
            _conn.Close();
        }
    }
}