views:

96

answers:

3

My statement:

string sqlCommandText = "SELECT * FROM ForumThread WHERE";

How can I read * from it?

I'm trying like this but its doesnt work:

if (reader["*"] != DBNull.Value)
{
    result.TextContent = Convert.ToString(reader["*"]);
}
+4  A: 

* is not a column, it's a instruction to SELECT to just grab every column it can find.

The columns will appear individually though, so you need to iterate through the columns one by one and grab their contents.

To get the column contents, you can do this:

StringBuilder sb = new StringBuilder();
for (int index = 0; index < reader.FieldCount; index++)
{
    object value = reader[index];
    if (value != DBNull.Value)
        sb.Append(value.ToString());
}
result.TextContent = sb.ToString();

However, this will result in everything being mashed together.

For instance, if your result set look like this:

A    B    C    D
10   20   30   Some name here

Then your resulting string will look like this:

102030Some name here

Is this what you want?

Perhaps you told us what you want to accomplish, we can think of a better answer?

Lasse V. Karlsen
+1  A: 

Additionally, you can use this method to retrieve the associated field name:

reader.GetName( /* index of the column */);
Johann Blais
A: 

Hi, Suppose your ForumThread table contains the ThreadResponse column, and you actually want to read the ThreadResponse column, you can use the next code:

//calculates the column ordinal based on column name
int ThreadResponseColumnIdx = reader.GetOrdinal("ThreadResponse");

//If there is a line in the result
if (reader.Read())
{
  //read from the current line the ThreadResponse column
  result.TextContent = reader.GetString(ThreadResponseColumnIdx);
}
cipx