如何使用c#从asp.net中的数据库获取值到泛型列表 [英] how to get values from database to generic list in asp.net using c#

查看:125
本文介绍了如何使用c#从asp.net中的数据库获取值到泛型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的所有数据从数据库复制到我将绑定到gridview的通用列表。但我无法做到。我正在做的是:

i want to copy all my data from database to generic list to which i will bind to gridview. but i won't be able to do it . what i am doing is as :

public List<string>   fillgridviewDAL()
       {
           SqlConnection connection = new SqlConnection(constr);
           connection.Open();
                       command = new SqlCommand("spuserdetailsinfo", connection);  //******Don't fix the STORED PROCEDURE NAME in DAL. you have to pass it from BL.Remember that companies doesn't modify the DAL. They only play with .cs and BAL.
               command.CommandType = CommandType.StoredProcedure;
               dataset = new DataSet();
               dataadapter = new SqlDataAdapter();
               dataadapter.SelectCommand = command;
               dataadapter.Fill(dataset);
               connection.Close();
               connection.Dispose();


               List<string> NameList = (from r in dataset.Tables["MyTableName"].AsEnumerable()
                                        select r.Field<string>("name")+ r.Field<string>("age")+r.Field<string>("salary")+r.Field<string>("city")).ToList();
               return NameList;
       }



当我看到数据集中的内容时它显示完整的表但是当我使用上面的方法使用泛型列表时,它会连接列并在特定索引处给出一个结果行。简而言之,它以int形式提供输出。比如何将gridview绑定到我的通用列表...感谢你们所有人。


when i see contents in dataset it shows full table but when i use generic list using above method it concat columns and gives a resultant row at particular index. In short it gives me output in int form. than how can i bind the gridview to my generic list...Thanking u all.

推荐答案

它给你一个连接的输出,因为这就是你要求它做的。

而不是连接列,以新的匿名类型项目列。

更改您的代码如下:

It gives you a concatenated output because that's what you are asking it to do.
Instead of concatenating the columns, project columns in a new anonymous type.
Change your code as below:
var NameList = (from r in dataset.Tables["MyTableName"].AsEnumerable()
                            select new
                            {
                                Name = r.Field<string>("name"),
                                Age = r.Field<string>("age"),
                                Salary = r.Field<string>("salary"),
                                City = r.Field<string>("city")
                            });





这基本上创建了一个匿名类型,它是一个集合有名称,年龄,薪水和城市。您可以使用foreach循环遍历它们。还要记住,只有在迭代结果集合时才会执行查询。如果你想在那个时刻执行它,最后添加ToList()。



GridView 应该接受匿名类型为 DataSource 。只需要处理列名。



希望有所帮助!



This basically creates an anonymous type which is a collection having columns Name, Age, Salary and City. You can iterate through them using a foreach loop. Also remember that the query will execute only when the result collection will be iterated. Add ToList() in the end if you want to execute it at that very moment.

You GridView should accept anonymous type as DataSource. Just take care of the column names.

Hope that helps!


这篇关于如何使用c#从asp.net中的数据库获取值到泛型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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