/// <summary>
/// 获取指定表的主键列名
/// </summary>
/// <param name="tableName"></param>
/// <returns></returns>
public static string SQLGetpKey(string tableName)
{
return SQLHelper.ExecSQL_GetValue("Declare @objectid int"+
" Set @objectid = object_id('"+ tableName + "')"+
" Select col_name(@objectid, colid) '主键字段' From sysobjects as o"+
" Inner Join sysindexes as i On i.name = o.name"+
" Inner Join sysindexkeys as k On k.indid = i.indid"+
" Where o.xtype = 'PK' and parent_obj = @objectid and k.id = @objectid");
}
Declare@objectidint--若主键字段是整型Set@objectid= object_id('tableName')Select col_name(@objectid, colid)'主键字段'From sysobjects as o InnerJoin sysindexes as i On i.name = o.name InnerJoin sysindexkeys as k On k.indid = i.indid Where o.xtype ='PK'and parent_obj =@objectidand k.id =@objectid