sql 命令产生在 sql 服务器中不为空的空结果

sql command yields empty results that aren't empty in sql server

所以,我正在尝试构建一个代码生成器,它将从数据库中提取索引并制作一个 class,它将根据索引进行过滤。 下面的代码在 SQL-server 中工作并产生 2 条记录。但是我的 SqlDataReader 产生了零条记录。 为 1 table 提供了带有索引的示例。 希望有人能帮我解决这个问题。

SQL 服务器中的代码:

create table Agent(
    ID bigint constraint PK_Agent primary key identity(1,1),
    LastName nvarchar(50) not null,
    FirstName nvarchar(50) not null,
    index IN_Agent_Name nonclustered (LastName, FirstName)
)

select t.object_id, 
        s.name as schemaname, 
        t.name as tablename,  
        i.index_id,  
        i.name as indexname,  
        index_column_id,  
        c.name as columnname  
from sys.tables t 
inner join sys.schemas s on t.schema_id = s.schema_id 
inner join sys.indexes i on i.object_id = t.object_id 
inner join sys.index_columns ic on ic.object_id = t.object_id and ic.index_id = i.index_id 
inner join sys.columns c on c.object_id = t.object_id and ic.column_id = c.column_id 
where i.index_id > 0 
and i.type in (1, 2)  
and i.is_primary_key = 0  
and i.is_unique_constraint = 0  
and i.is_disabled = 0  
and i.is_hypothetical = 0  
and ic.key_ordinal > 0  
and t.name like 'Agent'  
and i.name like 'IN_Agent_Name'

VS 中的代码:


        public static TableIndex GetIndex(string indexName, string tableName)
        {
            TableIndex index = null;
            using (var conn = new SqlConnection("Server=localhost;Database=VenturaERD;User Id=VenturaDBUser;Password = Ventura;"))
            {
                conn.Open();
                var cmd = new SqlCommand("select t.object_id, s.name as schemaname, t.name as tablename, i.index_id, i.name as indexname, index_column_id, c.name as columnname from sys.tables t inner join sys.schemas s on t.schema_id = s.schema_id inner join sys.indexes i on i.object_id = t.object_id inner join sys.index_columns ic on ic.object_id = t.object_id and ic.index_id = i.index_id inner join sys.columns c on c.object_id = t.object_id and ic.column_id = c.column_id where i.index_id > 0 and i.type in (1, 2) and i.is_primary_key = 0 and i.is_unique_constraint = 0 and i.is_disabled = 0 and i.is_hypothetical = 0 and ic.key_ordinal > 0 and t.name like '" + tableName + "' and i.name like '" + indexName + "'")
                {
                    Connection = conn
                };
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        index = new TableIndex()
                        {
                            TableId = reader.GetInt32(reader.GetOrdinal("object_id")),
                            TableName = reader.GetString(reader.GetOrdinal("tablename")),
                            IndexId = reader.GetInt32(reader.GetOrdinal("index_id")),
                            IndexName = reader.GetString(reader.GetOrdinal("indexname")),
                            Columns = new List()
                            {
                                new IndexColumn()
                                {
                                    ColumnName = reader.GetString(reader.GetOrdinal("columnname")),
                                    Order=reader.GetInt32(reader.GetOrdinal("index_column_id"))
                                }
                            }
                        };
                        while (reader.Read())
                        {
                            index.Columns.Add(new IndexColumn()
                            {
                                ColumnName = reader.GetString(reader.GetOrdinal("columnname")),
                                Order = reader.GetInt32(reader.GetOrdinal("index_column_id"))
                            });
                        }
                    }
                    reader.Close();
                }
            }
            return index;
        }

请检查用户权限,我相信只有public权限的用户不会得到任何数据。

确保您使用 SQL management studio 连接的用户与连接字符串中的用户相同。

用户(至少在我的测试中)至少需要 db_datareader 角色。