2

In my code I am trying to CREATE INDEX in my code, and since CREATING duplicated INDEX is not allowed, I wanted to check if my INDEX existed in my SQL CE Database.

In C# code, I ran the query using IDbCommand with query "SELECT * FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_NAME = 'mytablename'" so if there are results, I have the INDEX already created for this Database. Nothing fancy. However, when I run this, I did not see the result even though I know I created the index.

So I tried just running the query using SQL Server Compact/SQLite Toolbox. I am seeing that I have the INDEX with the tool's querying. I thought my SQL syntax might be wrong so I ran query "SELECT * FROM INFORMATION_SCHEMA.INDEXES" without WHERE to compare how many results I receive. I am seeing 13 results vs 12 results. Obviously the missing one is mytablename.

Does anybody have clue why this weird issue is occuring? If this is does not resolve, I can try catch and catch if there is duplicate and ignore the result. But preferably, I would like to properly catch if the Row exists or not.

The C# code I used is following.

    DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
    using (DbConnection conn = factory.CreateConnection())
    {
        conn.ConnectionString = "xxxx";
        try
        {
            conn.Open();
            IDbCommand cmd = factory.CreateCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT * FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_NAME = 'mytablename'";
            DbDataReader ddr = (DbDataReader)cmd.ExecuteReader();
            ddr.Read();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            conn.Close();
        }
    }
ShinT
  • 971
  • 1
  • 13
  • 29
  • So SELECT Count(*) FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_NAME = 'mytablename' yields 12, but toolbox shows 13? – Shannon Holsinger Sep 15 '16 at 22:40
  • Sorry if it was unclear. I took out WHERE so SQL Query is `SELECT Count(*) FROM INFORMATION_SCHEMA.INDEXES` yields 12 but toolbox shows 13. And given I CREATE(ed) INDEX specifically right before this statement, I should see 13. (which toolbox is correct) – ShinT Sep 15 '16 at 23:43
  • That's truly odd... is it possible (I know this is strange) that you somehow have duplicate indices that the I_S.Indexes is only counting as one? – Shannon Holsinger Sep 15 '16 at 23:45
  • I just blasted a table with every different combination of index I could think of and checked schema each time - the number was correct going up (loop) and coming back down (remove loop). I cannot seem to replicate our condition. – Shannon Holsinger Sep 15 '16 at 23:46
  • Have you used any commercial comparison kits: http://stackoverflow.com/questions/685053/what-is-best-tool-to-compare-two-sql-server-databases-schema-and-data – Shannon Holsinger Sep 15 '16 at 23:48
  • Let me give it a shot – ShinT Sep 15 '16 at 23:55
  • Did you find out what was causing this? – Karl Gjertsen Jan 16 '17 at 15:35

0 Answers0