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

c#知道一个表名,怎么判断这个表里边那个字段是主键,请高手解答,谢谢
需要的是代码,o(∩_∩)o...
表不是自己创建的

------解决方案--------------------
C# code
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("select  * from table","Data Source=127.0.0.1;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=12345;");
                da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

                DataTable dt = new DataTable();
                da.Fill(dt);

                foreach (DataColumn col in dt.PrimaryKey)
                {
                    MessageBox.Show(col.ColumnName);
                }
            }
            catch
            {
                MessageBox.Show("error");
            }
        }