Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perhaps you should remove the semicolon at the end of Read</p> <pre><code>while (reader_sql.Read()) { for (int rr = 0; rr &lt; 20; rr++) colName[rr] = reader_sql["COLUMN_NAME"].ToString(); } </code></pre> <p>However, if your intention is to retrieve the columns belonging to the primary key, your code is wrong because you add 20 times the same primary key column, then repeat the same for the remaining columns ending with an array of 20 strings all equals to the last column in the primary key set. I think you should change your code to use a List(Of String) instead of a fixed length array and let the reader loop correctly on the primary key columns retrieved</p> <pre><code>List&lt;string&gt; pks = new List&lt;string&gt;(); while (reader_sql.Read()) { pks.Add(reader_sql["COLUMN_NAME"].ToString()); } </code></pre> <p><strong>EDIT:</strong> I have just noticed that your query contains a space before the table name. The string concatenation then produces an invalid table name, the query is syntactically right but doesn't return any data</p> <pre><code>string sql = "select distinct COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE " + "where TABLE_NAME='" + table + "' and CONSTRAINT_NAME like 'PK_%'"; ^ space removed here </code></pre> <p>And while you are at it, remove the string concatenation and use a parameterized query.....</p> <pre><code>string sql = "select distinct COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE " + "where TABLE_NAME=@tName and CONSTRAINT_NAME like 'PK_%'"; SqlCommand cmd_server2 = new SqlCommand(sql, connection); connection.Open(); cmd_server2.Parameters.AddWithValue("@tName", table); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload