-1

guys i have an SQL statement returning more than 1 value. I am trying to use the StreamReader to get the values into an array as below

 string sql = "select distinct COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME=' " + table + "' and CONSTRAINT_NAME like 'PK_%'";
            SqlConnection conn2 = new SqlConnection(cnstr.connectionString(cmbDatabase.Text));
            SqlCommand cmd_server2 = new SqlCommand(sql);
            cmd_server2.CommandType = CommandType.Text;
            cmd_server2.Connection = conn2;
            conn2.Open();

            //reader_sql = new StreamReader();
            SqlDataReader reader_sql = null;
            string[] colName = new string[200];
            reader_sql = cmd_server2.ExecuteReader();
            while (reader_sql.Read());

            for (int rr = 0; rr < 20; rr++)
            {
                colName[rr] = reader_sql["COLUMN_NAME"].ToString();
            }

It is not working, what am I doing wrong guys ?

Anoushka Seechurn
  • 2,166
  • 7
  • 35
  • 52

3 Answers3

3

You've got a stray ; turning your while into a tight loop, so instead try:

        while (reader_sql.Read())

        for (int rr = 0; rr < 20; rr++)
        {
            colName[rr] = reader_sql["COLUMN_NAME"].ToString();
        }
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
2

Perhaps you should remove the semicolon at the end of Read

while (reader_sql.Read())
{
    for (int rr = 0; rr < 20; rr++)
       colName[rr] = reader_sql["COLUMN_NAME"].ToString();
}    

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

List<string> pks = new List<string>();
while (reader_sql.Read())
{
    pks.Add(reader_sql["COLUMN_NAME"].ToString());
}    

EDIT: 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

string sql = "select distinct COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE " + 
             "where TABLE_NAME='" + table + "' and CONSTRAINT_NAME like 'PK_%'";
                                ^ space removed here

And while you are at it, remove the string concatenation and use a parameterized query.....

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);
Steve
  • 213,761
  • 22
  • 232
  • 286
2

You get the exception because

while (reader_sql.Read());

should be

while (reader_sql.Read())
{
    for (int rr = 0; rr < 20; rr++)
     {
         colName[rr] = reader_sql["COLUMN_NAME"].ToString();
     }
}
Ehsan
  • 31,833
  • 6
  • 56
  • 65