代码之家  ›  专栏  ›  技术社区  ›  Wernight

在SQL Server中检索表的数据表信息

  •  2
  • Wernight  · 技术社区  · 14 年前

    我需要获取列名、主键、外键和其他模式信息。这个 DataTable

    下面是我目前得到的代码。有了它,除了 外键 . 我希望他们能在 DataTable.Constraints

        private static DataTable LoadSchemaInfo(string tableName, SqlConnection connection)
        {
            string cmdText = "SELECT * FROM [" + tableName + "] WHERE 1 = 0";
    
            // Create a SqlDataAdapter to get the results as DataTable
            var sqlDataAdapter = new SqlDataAdapter(cmdText, connection);
    
            // Create a new DataTable
            var dataTable = new DataTable(tableName);
    
            // Fill the DataTable with the result of the SQL statement
            sqlDataAdapter.FillSchema(dataTable, SchemaType.Source);
    
            return dataTable;
        }
    

    知道如何检索所有信息或如何获取FK吗(最好不要使用纯SQL语法,因为我将缺乏一些编译时检查)?

    5 回复  |  直到 14 年前
        1
  •  5
  •   Christopher Klein    14 年前

    使用SMO你可以做到这一点。。。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Smo.Agent;
    
    
    // Add references: (in c:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\)
    // Microsoft SqlServer.ConnectionInfo
    // Microsoft SqlServer.Management.Sdk.Sfc
    // Microsoft SqlServer.Smo
    
    namespace SMO
    {
        class Program
        {
            static Database db;
    
            static void Main(string[] args)
            {
                Microsoft.SqlServer.Management.Smo.Server server;
    
                SqlConnection sqlConnection = new SqlConnection(@"Integrated Security=SSPI; Data Source=LOCAL");
                //build a "serverConnection" with the information of the "sqlConnection"
                Microsoft.SqlServer.Management.Common.ServerConnection serverConnection =
                  new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection);
    
                //The "serverConnection is used in the ctor of the Server.
                server = new Server(serverConnection);
    
                db = server.Databases["TestDB"];
    
                Table tbl;
                tbl = db.Tables["Sales"];
                foreach (ForeignKey fk in tbl.ForeignKeys)
                {
                    Console.WriteLine("Foreign key {0} references table {1} and key {2}", fk.Name, fk.ReferencedTable, fk.ReferencedKey);
                } 
            }
        }
    }
    
        2
  •  1
  •   marc_s    14 年前

    你可以随时检查 sys 数据库中的目录视图,使用简单的ADO.NET查询-视图如下:

    • sys.columns 关于您的列的信息
    • sys.foreign_keys
    • sys.tables 对于桌子

    等等。就做一个 SELECT (list of fields) FROM sys.foreign_keys 看看你得到了什么!

    请参阅:联机丛书 Querying the SQL Server System Catalog

        3
  •  0
  •   Ralph Shillington    14 年前

    如果您正在寻找数据库架构的约束,那么ADO.net DataTable不是您的最佳选择。你可能会发现 SMO

        4
  •  0
  •   Tom Brothers    14 年前

    您可以使用SqlConnection获取信息。

    string connectionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();
    
        DataTable dtAllForeignKeys = conn.GetSchema("ForeignKeys");
    
        string[] restrictionValues = { "Northwind", "dbo", "Orders" };
        DataTable dtForeignKeysForJustTheOrderTable = conn.GetSchema("ForeignKeys", restrictionValues);
        conn.Close();
    }
    
        5
  •  0
  •   VladV    14 年前

    要获取数据库中有关外键的信息,需要读取表的元数据(调用SELECT时,只获取查询的元数据,这就是为什么没有关于键的信息)。
    Information Schema Views 或者让SMO课程为你做这些。在后一种情况下,首先 Server 对象,然后获取数据库、表等。