【问题标题】:Make SQL result have a class type使 SQL 结果具有类类型
【发布时间】:2011-09-11 00:26:33
【问题描述】:

这是我的第一个 C# 项目,使用 VS 2008 和 .NET 3.5,客户端需要。

我有一个 GradeVO 类型的列表

这是我的连接,我正在为 DataGridView 设置值

DataSet ds = new DataSet();
SqlConnection Conexao = new SqlConnection(@"Data Source=localhost; Initial Catalog=usr_mmidia; User ID=usr_mmidia; Password=123; MultipleActiveResultSets = true");
Conexao.Open();
SqlDataAdapter da = new SqlDataAdapter("MAG_SP_LISTA_SETORES_SQL", Conexao);
da.SelectCommand.Parameters.Add(new SqlParameter(("@p_codfil"), @"500"));
da.SelectCommand.Parameters.Add(new SqlParameter(("@p_categoria"), categoria));
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable t = new DataTable();
da.Fill(t);
dgNivel1.DataSource = t;

此代码有效,但我需要 DataSource 来接收 GradeVO 列表,这可能吗?

提前致谢。

【问题讨论】:

  • 您是否习惯于使用数据集?如果您使用 LINQ-to-SQL,它将自动为您处理 SQL 到对象的映射。现在它被认为不是很好,但它很容易上手并附带 .NET 框架。

标签: c# .net .net-3.5 datagridview


【解决方案1】:

你总是可以这样做:

var myDataSource = t.Select(row => new GradeVO 
                                       {
                                           //your initialization logic here to get 
                                           //stuff from the row into the object
                                       });
dgNivel1.DataSource = myDataSource;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多