开发者

select statements how to read all from "*"

开发者 https://www.devze.com 2023-01-16 03:28 出处:网络
My statement: string sqlCommandText = \"SELECT * FROM ForumThread WHERE\"; How can I read * from it开发者_Go百科?

My statement:

string sqlCommandText = "SELECT * FROM ForumThread WHERE";

How can I read * from it开发者_Go百科?

I'm trying like this but its doesnt work:

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


* 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?


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

reader.GetName( /* index of the column */);


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消