如何遍历列表并显示每个列表项的结果 [英] How Do I Loop Through A List And Display Result For Each List Items

查看:72
本文介绍了如何遍历列表并显示每个列表项的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人如何,我如何遍历列表并显示每个列表项的值,



i从sql数据库中检索列并存储在列表中: var listAttributeRegion = new List< long>(); listAttributeRegion包含以下格式的dataid值:50600,50601,50602,50603,50604等,


i希望将列表中的每个值作为参数值传递给select语句,并将结果结果集绑定到datagrid,并插入到数据库中的表中。这是我的代码:





how everyone, how can i iterate through a list and display values for each list items,

i am retrieving a column from sql database and store in a list :var listAttributeRegion = new List<long>( ); the listAttributeRegion contains dataid values in this format: 50600,50601,50602,50603,50604 etc,

i want to pass each of this values from the list as parameter values to a select statement, and bind the resulting result set to a datagrid and also insert to a table in the database. here is my code:


while ( reader.Read( ) )
                   {

                       listAttributeRegion.Add(Convert.ToInt64(reader["id"].ToString( )) );


                   }

                   foreach ( long k in listAttributeRegion )
                   {
                       var dataId = k;

                       try
                       {

                           using ( var strCon = new SqlConnection( ConfigurationManager.ConnectionStrings["edms"].ConnectionString ) )
                           {
                               using ( var com = new SqlCommand( "Select l.AttrID , l.ValStr from csowner.LLAttrData l join csowner.dtree d on  ID = d.DataID where d.DataID = @DataID order by l.AttrID asc", strCon ) )
                               {
                                   com.CommandType = CommandType.Text;
                                   com.Parameters.Add( "@DataID", SqlDbType.Int ).Value = dataId;
                                   using ( var adapter = new SqlDataAdapter( com ) )
                                   {

                                       using (var ds = new DataSet( ) )
                                       {
                                           adapter.Fill( ds );
                                           dataGridView1.DataSource = ds.Tables[0];
                                       }
                                   }
                               }
                           }
                       }
                       catch ( SqlException ex )
                       {

                       }
                          reader.Close( );
                   }







上面的代码用结果集填充ds,每个数据都通过,我希望能够追加/组合所有结果集并显示在数据网格中,也插入到表中。请帮助解决方法。




The above code populates ds with results set, when each dataid is passed, i want to able to append/combine all result sets and display in a datagrid, also insert to a table. Please any assistance on how to do this will be appreciated.

推荐答案

我不确定您的方案,但我认为您可以将查询更改为得到目标。



使用(var com = new SqlCommand(Select l.AttrID)而不是使用
I am not sure about your scenario but I think you can you can change your query to some extend to get the goal.

Instead of doing
using ( var com = new SqlCommand( "Select l.AttrID , l.ValStr from csowner.LLAttrData l join csowner.dtree d on  ID = d.DataID where d.DataID = @DataID order by l.AttrID asc", strCon ) )

<你可以这样写:





you can write like:

using ( var com = new SqlCommand( "Select l.AttrID , l.ValStr from csowner.LLAttrData l join csowner.dtree d on  ID = d.DataID where d.DataID in(select distinct id from yourtable) order by l.AttrID asc", strCon ) )



或者您也可以使用加入来获取数据。



这里 yourtable 是你的第一个查询的表名。

通过这种方式你可以保存每个项目的循环并点击数据ase每次和整体上你的数据组合问题也将得到解决。


Or you can use joining also to get data.

Here yourtable is your first query's table name.
by this way you you can save looping through each item and hit database each time and overall your problem of combining data will also be solved.


这篇关于如何遍历列表并显示每个列表项的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆