0

So, I'm trying to build a code generator that will extract indexes from a database and make a class that will filter based on an index. Below code works in SQL-server and yields 2 records. But my SqlDataReader yields zero records. Provided example for 1 table with index. Hoping someone can help me out here.

Code in SQL server:

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'

Code in 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;
        }

5
  • Don't know what you mean by this. I copied the command text of the sql command during execution and pasted it in MS SQL Server and executed it. VS yields zero results, MS SQL server gives me 2. Commented May 17, 2018 at 8:22
  • Atm, i'm feeding the function parameters from a console application, purely to test the code. In the end, a gui application will allow the user to select indexes from a list. At no point, will the user be able to type in any text. So I don't see the problem here. Commented May 17, 2018 at 8:25
  • I've reformatted your first code block, please do the same for the second one. Commented May 17, 2018 at 8:26
  • Read() or HasRows both yield me the result false, so doubt there will be a difference there. Commented May 17, 2018 at 8:27
  • I use the first read to read the columns that are the same for all records, then use the while to read the columns that are different for each record. Commented May 17, 2018 at 8:44

1 Answer 1

2

Please check the users rights, I believe with only the public rights the user will not get any data.

Make sure the user you are connecting to with SQL management studio and the user in the connection string are the same.

The user (in my tests at least) needs at least the db_datareader role.

Sign up to request clarification or add additional context in comments.

4 Comments

awesome glad that helped
to the person who down voted this please tell me why you down voted it? Very interested to see your response....
@mjwills its a security issue, the query did not return results because in management studio where the query was run the user running the query had the db_datareader role, while in the code the code connects using a different user, this user does not have db_datareader role.
providing the user with the right db_datareader role did the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.