datatable怎么判断一列是否为主键?

MicroDeviser 2008-12-10 06:23:33
如题
我查询出table后,
怎么判断一列是否为主键
我用table的PrimaryKey属性获取,但都是空的,这是怎么回事,我在sqlserver中设置了主键的啊
...全文
422 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhnzzy 2008-12-11
  • 打赏
  • 举报
回复
都说了
MicroDeviser 2008-12-11
  • 打赏
  • 举报
回复
哇,顶,搞定了,等下来结贴
flyjimi 2008-12-10
  • 打赏
  • 举报
回复
支持3楼的。
table的PrimaryKey 是需要你手工设置的,或者按照 在DataAdapter.Fill时,通过设置dataadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 获取。
gdstfox 2008-12-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
dataadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dataadapter.Fill(Table);
DataColumn[] cols;
cols = Table.PrimaryKey;
for(int i = 0; i < cols.Length; i++)
{
MessageBox.Show(cols[i].ColumnName);
}
[/Quote]

用这个方法好
特别 2008-12-10
  • 打赏
  • 举报
回复
看一下DataTable.Constraints 属性
fskjb01 2008-12-10
  • 打赏
  • 举报
回复
mark
wuyq11 2008-12-10
  • 打赏
  • 举报
回复
dataadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dataadapter.Fill(Table);
DataColumn[] cols;
cols = Table.PrimaryKey;
for(int i = 0; i < cols.Length; i++)
{
MessageBox.Show(cols[i].ColumnName);
}

wangping_li 2008-12-10
  • 打赏
  • 举报
回复

/// <summary>
/// 判断某列是否为主键
/// </summary>
/// <param name="columnName">列的物理名称</param>
/// <param name="tableName">表名</param>
/// <returns></returns>
public bool IsPrimaryKey(String columnName,String tableName)
{
SqlParameter[] parameters = new SqlParameter[3];
parameters[0] = new SqlParameter("@table_name", tableName);
parameters[1] = new SqlParameter("@table_owner", null);
parameters[2] = new SqlParameter("@table_qualifier", null);
DataRowCollection drc = SqlHelper.ExecuteDataset(Common.GetConnString, CommandType.StoredProcedure, "sp_pkeys", parameters).Tables[0].Rows;
if (drc.Count==0 || (drc[0]["COLUMN_NAME"].ToString() != columnName))
return false;
else
return true;


}
feiyun0112 2008-12-10
  • 打赏
  • 举报
回复
CommandBehavior.KeyInfo
http://msdn.microsoft.com/zh-cn/library/system.data.commandbehavior(VS.80).aspx

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

http://feiyun0112.cnblogs.com/

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧