C#如何从返回SqlDataReader的数据集? [英] c# how do you return dataset from sqldatareader?

查看:406
本文介绍了C#如何从返回SqlDataReader的数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的一个公共类:

I have this in a public class:

SqlConnection myConnection = new SqlConnection("Data Source=hermes;database=qcvalues; Integrated Security=SSPI;");
myConnection.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(InitializeQuery(), myConnection);
myReader = myCommand.ExecuteReader();



我需要控制的数据源获得myReader数据集。

I need the datasource of a control to get the dataset from myReader.

很不幸,这是很难做到的,因为控制是一种形式(单独的类)上。我怎么会返回 myReader 数据集中到控制我的窗体上的数据源属性?

Unfortunately this is difficult to do because the control is on a form (a separate class). how would I return myReader dataset into the datasource property of the control on my form?

推荐答案

您没有。使用DataAdapter代替:

You don't. Use a DataAdapter instead:

var ds = new DataSet();

using(var conn = new SqlConnection(connString))
{
    conn.Open();
    var command = new SqlCommand(InitializeQuery(), conn);
    var adapter = new SqlDataAdapter(command);

    adapter.Fill(ds);
}

这篇关于C#如何从返回SqlDataReader的数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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