日期:2014-05-18  浏览次数:21286 次

c#如何获取数据表的主键???
C# code

 static void Main(string[] args)
        {
            string connStr = "Data Source=cc4152;Initial Catalog=db1;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
                string[] restri = new string[4];
                restri[2] = "T_Users";//获取数据中名为T_Users的数据表
                DataTable table = conn.GetSchema("Columns",restri);
                foreach (DataRow row in table.Rows)
                {
                    foreach (DataColumn col in table.Columns)
                    {
                        Console.WriteLine("{0} = {1}",col.ColumnName,row[col]);//此处可以获得字段的详细信息
                        //我应该如何获得主键信息呢?
                    }
                }
            }
            catch(SqlException ex)
            {
                Console.WriteLine(ex);
            }
            conn.Close();
            Console.ReadKey();
        }


请问根据以上的信息能否获得T_Users数据表的主键呢?如果不能有哪种方法可以获取T_users的所有结构信息(主键,字段,字段类型等等)

------解决方案--------------------
SQL code
 SELECT     TOP (100) PERCENT d.name AS TableName, CASE WHEN a.colorder = 1 THEN isnull(f.value, '') ELSE '' END AS TableDesc,
        a.colorder AS SequenceNumber, a.name AS ColumnName,  CASE WHEN EXISTS
        (SELECT     1
        FROM          dbo.sysindexes si INNER JOIN
        dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid and si.name like 'PK_%' INNER JOIN
        dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN
        dbo.sysobjects so ON so.name = so.name AND so.xtype = 'PK'
        WHERE      sc.id = a.id AND sc.colid = a.colid) THEN 1 ELSE 0 END AS PrimaryKey,CASE WHEN EXISTS
        (SELECT     1
        FROM          dbo.sysindexes si INNER JOIN
        dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid  INNER JOIN
        dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN
        dbo.sysobjects so ON so.name = so.name AND so.xtype = 'PK'
        WHERE      sc.id = a.id AND sc.colid = a.colid) THEN 1 ELSE 0 END AS Flag, b.name AS DbType, a.length AS Length,
        COLUMNPROPERTY(a.id, a.name, 'PRECISION') AS Precision, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS DecimalDigits,
        a.isnullable AS AllowNull, ISNULL(e.text, '') AS DefaultValue, ISNULL(g.value, '') AS Description, d.crdate AS CreateTime,
        CASE WHEN a.colorder = 1 THEN d .refdate ELSE NULL END AS UpdateTime
        FROM         syscolumns AS a LEFT OUTER JOIN
        systypes AS b ON a.xtype = b.xusertype INNER JOIN
        sysobjects AS d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0 and d.[Name] not like '_Del_%' and d.[name] not like 'Enum_%' LEFT OUTER JOIN
        syscomments AS e ON a.cdefault = e.id LEFT OUTER JOIN
        sys.extended_properties AS g ON a.id = g.major_id AND a.colid = g.minor_id LEFT OUTER JOIN
        sys.extended_properties AS f ON d.id = f.major_id AND f.minor_id = 0
        ORDER BY TableName, SequenceNumber

------解决方案--------------------
SQL code

//查询一张表的主键:
SELECT a.name  
  FROM   syscolumns a  
  inner  join sysobjects d on a.id=d.id        
  where  d.name='SPF_Users' and exists(SELECT 1 FROM sysobjects where xtype='PK' and  parent_obj=a.id and name in (   
  SELECT name  FROM sysindexes   WHERE indid in(   
  SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid   
  )))

//查询一张表的所有字段:
SELECT c.name,o.name FROM syscolumns AS c
INNER JOIN sysobjects AS o
ON c.id = o.id where o.name='SPF_Users'

------解决方案--------------------
判断一个表是否设置了主键,主键是哪个字段可以通过下面SQL语句实现
select * 
from syscolumns