C# 选择“如何读取”中的所有语句*&引用;

C# 选择“如何读取”中的所有语句*&引用;,c#,sql,asp.net-mvc,C#,Sql,Asp.net Mvc,我的发言: string sqlCommandText = "SELECT * FROM ForumThread WHERE"; 如何从中读取* 我试着这样做,但它不起作用: if (reader["*"] != DBNull.Value) { result.TextContent = Convert.ToString(reader["*"]); } *不是一列,它是一条指令,用于选择以获取它能找到的每一列 不过,这些列将单独出现,因此您需要逐个遍历这些列并获取它们的内容 要获取列内

我的发言:

string sqlCommandText = "SELECT * FROM ForumThread WHERE";
如何从中读取
*

我试着这样做,但它不起作用:

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

*
不是一列,它是一条指令,用于
选择
以获取它能找到的每一列

不过,这些列将单独出现,因此您需要逐个遍历这些列并获取它们的内容

要获取列内容,可以执行以下操作:

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();
然后生成的字符串将如下所示:

A    B    C    D
10   20   30   Some name here
这里有什么名字

这是你想要的吗


也许你告诉了我们你想要完成什么,我们可以想出一个更好的答案吗?

*
不是一列,它是一个指令,可以选择它能找到的每一列

不过,这些列将单独出现,因此您需要逐个遍历这些列并获取它们的内容

要获取列内容,可以执行以下操作:

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();
然后生成的字符串将如下所示:

A    B    C    D
10   20   30   Some name here
这里有什么名字

这是你想要的吗


也许您告诉了我们您想要完成什么,我们可以想出更好的答案?

此外,您可以使用此方法检索相关字段名:

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

此外,您可以使用此方法检索关联的字段名:

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

假设您的ForumThread表包含ThreadResponse列,并且您确实想要读取ThreadResponse列,您可以使用下面的代码:

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

假设您的ForumThread表包含ThreadResponse列,并且您确实想要读取ThreadResponse列,您可以使用下面的代码:

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

尽量避免在select语句中使用*并显式指定列。这对性能更有利,并且您会留下一个提示,即您在代码中要查找哪些字段。dude,您需要先找到一篇关于SQL(T-SQL、PL/SQL、MySQL)的介绍性文章/书籍……尽量避免在select语句中使用*并显式指定列。这对性能更有利,您可以留下一个提示,您在代码中要查找哪些字段。杜德,您需要先找到一篇关于SQL(T-SQL、PL/SQL、MySQL)的介绍性文章/书籍。。。。