简体   繁体   English

无法使用数据阅读器显示列条目

[英]Not able to display column entries using Data Reader

SqlConnection con = new SqlConnection("My path");
SqlCommand cmd;
SqlDataReader dr;

Label1.Text = Session["name"].ToString();

con.Open();
cmd = new SqlCommand("Select '"+Session["name"]+"' from table1",con);
dr = cmd.ExecuteReader();

while(dr.read()){
   Dropdownlist1.Items.Add();   ------->Stuck here
}

I am selecting a column from a table which will have 3 or more entries. 我正在从具有3个或更多条目的表中选择一列。 And I want to put them in dropdownlist. 我想将它们放在下拉列表中。 I tried different approaches but nothing is giving me output I require. 我尝试了不同的方法,但没有提供所需的输出。 It shows error or display first entry only. 它显示错误或仅显示第一个条目。

Please feel free to edit my code and provide a suitable idea. 请随时编辑我的代码并提供合适的想法。

THANKS 谢谢

This is my table: 这是我的桌子:

SKY  |  SEA  | LAND
-----------------------
EAGLE| SHARK | LION
CROW | FISH  | TIGER
DUCK | WHALE | DEER   

Where Session["name"] can be SKY/SEA/LAND and depending on that I want my drop down to show the entries below it IF SKY is Session["name"] then dropdwonlist should display EAGLE CROW DUCK Session["name"]可以是SKY / SEA / LAND的情况下,并且如果希望SKY是Session [“ name”],则我希望下拉菜单显示下面的条目,然后dropdwonlist应该显示EAGLE CROW DUCK

Have you tried? 你有没有尝试过?

 while (dr.read()){
      Dropdownlist1.Items.Add(dr["Name"].ToString());
 }

If you want to go get only once and without also having to specify the column name then you could use GetString(0) like: 如果您只想获取一次而不必指定列名,则可以使用GetString(0),例如:

 while (dr.read()){
      Dropdownlist1.Items.Add(dr.GetString(0));
      break;
 }

Base on your question you are passing Session["name"] which is your table Column Name to your select query. 根据您的问题,您正在将Session [“ name”](这是您的表列名)传递给select查询。

There is also need to change on your code: 还需要更改您的代码:

Lets say your Session["name"] value is SKY 假设您的Session [“ name”]值为SKY

"Select '"+Session["name"]+"' from tableA" --session["name"] is treated here as a string

this will be rendered as Select 'Session[Name]' from TableA which is incorrect. 这将呈现为Select 'Session[Name]' from TableA错误。 so the result will be : 因此结果将是:

Session[Name]
Session[Name]
Session[Name]

Remove the single quote on the query and let's say : Session["name"] value is SKY 删除查询上的单引号,然后说:Session [“ name”]的值为SKY

cmd = new SqlCommand("Select "+Session["name"].ToString()+" from table1",con);

the query will be -> Select SKY from table1 查询将是-> Select SKY from table1

and the select result 和选择结果

EAGLE 
CROW 
DUCK

If so try this : 如果是这样,请尝试以下操作:

SqlConnection con = new SqlConnection("My path");
SqlCommand cmd;
SqlDataReader dr;

Label1.Text = Session["name"].ToString();

con.Open();
cmd = new SqlCommand("Select "+Session["name"].ToString()+" from table1",con);
dr = cmd.ExecuteReader();

       while (dr.Read())
        {
                   // get the results of each column
                   Dropdownlist1.Items.Add(dr[0].ToString()); //this will get your first column even your Session["name"] will contain any column values

        }

Check also this tutorial: Lesson 04: Reading Data with the SqlDataReader 还要检查本教程: 第04课:使用SqlDataReader读取数据

Best Regards 最好的祝福

you should use like below 你应该像下面这样使用

while (dr.read()){
  ListItem l = new ListItem(dr["name"].ToString(), "value", true);
  Dropdownlist.Items.Add(l);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据读取器在命令中使用语句 - Data reader in command using statement 使用数据阅读器选择和显示数据的问题 - Issues with Selecting and Displaying Data using Data Reader 数据读取器与指定的不兼容。 数据读取器中没有相应的列具有相同的名称 - data reader is incompatible with the specified. does not have a corresponding column in the data reader with the same name SQL 数据读取器插入标签 - 值不显示 - SQL Data Reader into Label - Value doesn't display 如何使用会话将特定列数据显示到数据库中的标签? - How to display specific column data to a label from a database using a session? 如何在 Visual Basic 中使用 excel 数据读取器获取特定单元格的内容? 使用 Excel 数据阅读器 - How to get the contents a particular cell using excel data reader in Visual Basic? Using ExcelData Reader 是否可以使用数据读取器查找表的行数? - is it possible to find row count of a table using data reader? 使用sql数据读取器遍历sql表中的所有值 - loop through all values in sql table using sql data reader 类型的成员“Column1”在数据读取器中没有同名的对应列 - A member of the type, 'Column1', does not have a corresponding column in the data reader with the same name 错误SQL数据读取器 - Error SQL data reader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM