c# - sql server 2008获取表主键的方法

标签 c# sql sql-server database sql-server-2008

我有下面的代码,应该能够获取表中的主键

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/

相关文章:

sql - 为什么 concat a null with text is null?

具有相同ID的多行之间的SQL差异

c# - 带正交相机剪裁的 Viewport3D

sql - 使用 postgres 和 chart.io 构建客户生命周期值(value)折线图

c# - 使用WebAPI HttpResponseMessage时下载文件提示

mysql - 如何使用 Arel 选择固定值?

c# - 如何在 Linq to Sql 中添加未映射到表的字段

sql-server - 如果出现错误,如何退出SSIS包

c# - 像在 html 上一样测量字符串?

c# - 在 AAD 中按用户 ID 进行用户查找失败