我有下面的代码,应该能够获取表中的主键
public List<string> GetPrimaryKeysForTable(string tableName)
{
List<String> retVal = new List<string>();
SqlCommand command = connector.GetCommand("sp_pkeys");
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("@table_name", typeof(SqlChars)).Value = tableName;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
retVal.Add(reader[3].ToString());
}
return retVal;
}
我的数据库中有一个名为 Users
的表。当我将 Users
作为我的参数传递时,阅读器不返回任何结果。知道为什么这可能无法返回我的主键吗?
最佳答案
试试这个:
command.Parameters.Add("@table_name", SqlDbType.NVarChar).Value = tableName;
关于c# - sql server 2008获取表主键的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8578734/