使用返回的值从DataSet [英] Using returned Values from a DataSet

查看:104
本文介绍了使用返回的值从DataSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集返回的方法。

I have a method which returns DataSet.

  protected DataSet GetProgramList()
    {
        DataSet ds1 = new DataSet();
        using (SqlConnection cn = new SqlConnection("server=Daffodils-PC\\sqlexpress;Database=Assignment1;Trusted_Connection=Yes;"))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM Program", cn))
                da.Fill(ds1, "Program");
        }
        return ds1;
    }

我想从其他方法的数据集,低于使用特定的列:

I want to use a specific column from the DataSet in other Method which is below:

protected DataSet GetStudentByProgramID(int programID)
{
    DataSet ds2 = new DataSet();
    using (SqlConnection cn = new SqlConnection("server=Daffodils-PC\\sqlexpress;Database=Assignment1;Trusted_Connection=Yes;"))
    {
        using (SqlDataAdapter da = new SqlDataAdapter(@"SELECT LastName, FirstName FROM Student JOIN Program on Program.ProgramID = Student.ProgramID WHERE ProgramID ="+programID, cn))
            da.Fill(ds2, "Student");
    }
    return ds2;
}

例如我想使用,从程序表格列的 ProgramID 第一个的方法。我知道我必须存储在一个变量返回的数据集,但如何

For example I want to use, the column ProgramID from Program Table in first method. I know I have to store the returned dataset in a variable but How?

推荐答案

既然你将有 DS1 GetStudentByProgramID

然后就可以使用这种方式。

Then you can use it this way

rotected DataSet GetStudentByProgramID(int programID)
{

 DataColumn programId = ds1.Tables[0].Columns["ProgramId"];
//to read row you can iterate from ds1.Table[0].Rows

DataSet ds2 = new DataSet();
using (SqlConnection cn = new SqlConnection("server=Daffodils-PC\\sqlexpress;Database=Assignment1;Trusted_Connection=Yes;"))
{
using (SqlDataAdapter da = new SqlDataAdapter(@"SELECT LastName, FirstName FROM Student WHERE ProgramID ="+programID, cn))
da.Fill(ds2, "Student");
}
return ds2;
}

这篇关于使用返回的值从DataSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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