c# – ADO.Net:从SQL服务器表中获取表定义

我正在使用C#编写一个方法,该方法返回有关表的以下信息:
列名,列类型,列大小,外键.

有人能指出我如何实现这个目标的正确方向吗?

解决方法

要获得FK和Schema,您应该能够使用:
DA.FillSchema() 

DS.Table("Name").PrimaryKey

或者使用下面演示的方法调用sp_fkey

代码fromAnother Link

private void LoanSchema()
    {

         private List<String> tablesList = new List<String>();
         private Dictionary<String,String> columnsDictionary = new Dictionary<String,String>();

          string connectionString = "Integrated Security=sspI;" +
          "Persist Security Info = False;Initial Catalog=northwind;" +
          "Data Source = localhost";
          sqlConnection connection = new sqlConnection();
          connection.ConnectionString = connectionString;
          connection.open();

          sqlCommand command = new sqlCommand();
          command.Connection = connection;
          command.CommandText = "exec sp_tables";
          command.CommandType = CommandType.Text;

          sqlDataReader reader = command.ExecuteReader();

           if (reader.HasRows)
           {
               while (reader.Read())
                  tablesList.Add(reader["TABLE_NAME"].ToString());
           }
           reader.Close();

           command.CommandText = "exec sp_columns @table_name = '" +
           tablesList[0] + "'";
           command.CommandType = CommandType.Text;
           reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                          columnsDictionary.Add(reader["COLUMN_NAME"].ToString(),reader["TYPE_NAME"].ToString());
             }
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...