C# 将数据集的内容提取到对象中

C# 将数据集的内容提取到对象中,c#,sql,sql-server,C#,Sql,Sql Server,我有一个数据集,它调用我的存储过程,并返回一个整数列表。如何提取可以存储在变量中的整数列表,无论是大小不断增长的集合(如列表),还是原始数据类型(如整数数组) 下面是我的代码: private DataSet getSubGroupsBelongingToUser() { DataTable variable; DataSet DS; myConnectionString = ConfigurationMa

我有一个
数据集
,它调用我的
存储过程
,并返回一个整数列表。如何提取可以存储在变量中的整数列表,无论是大小不断增长的集合(如
列表
),还是原始数据类型(如
整数数组

下面是我的代码:

 private DataSet getSubGroupsBelongingToUser()
        {
            DataTable variable;
            DataSet DS;
            myConnectionString = ConfigurationManager.ConnectionStrings["FSK_ServiceMonitor_Users_Management.Properties.Settings.FSK_ServiceMonitorConnectionString"].ConnectionString;

            using (mySQLConnection = new SqlConnection(myConnectionString))
            {
                SqlParameter param = new SqlParameter("@UserId", getUserID(cbxSelectUser.Text));
                DS = GetData("Test", param);
                variable = DS.Tables[0];

            }


            return DS;
        }
当我将鼠标悬停在DS放大镜上时(参见图):

我想在某处检索并存储整数列表。我该怎么做呢?我在网上遇到的所有示例都使用linq,这在这里不适用,因为我从需要一个输入参数的存储过程中获取结果。下面是存储过程的定义:

create proc [dbo].[Test]
@UserId smallint
as
begin
    select DepartmentSubGroupId from DepartmentSubGroupUser
    where UserId= @UserId
end
GO

因此,本质上,当您传入一个UserId时,您应该得到这些值。我正在使用
SQL Server
作为我的DBMS。

正如@David指出的,最简单的选择是使用
SqlDataReader
并循环遍历所有记录

但是,如果您的心被设置为
DataTable
s,那么您所需要做的就是迭代结果表中的所有行,从列
departmentsubkid
中获取值并将其添加到列表中。使用Linq也可以这样做:

return DS.Tables[0].Rows
    .Cast<DataRow>() // Rows is an ICollection and you need to cast each item
    .Select(r => (int)r["DepartmentSubGroupId"]) // For each row get the value from column DepartmentSubGroupId 
    .ToList();
返回DS.Tables[0]行
.Cast()//行是一个ICollection,您需要强制转换每个项
.Select(r=>(int)r[“departmentsubkid”])//对于每一行,从列departmentsubkid获取值
.ToList();

正如@David指出的,最简单的选择是使用
SqlDataReader
并循环遍历所有记录

但是,如果您的心被设置为
DataTable
s,那么您所需要做的就是迭代结果表中的所有行,从列
departmentsubkid
中获取值并将其添加到列表中。使用Linq也可以这样做:

return DS.Tables[0].Rows
    .Cast<DataRow>() // Rows is an ICollection and you need to cast each item
    .Select(r => (int)r["DepartmentSubGroupId"]) // For each row get the value from column DepartmentSubGroupId 
    .ToList();
返回DS.Tables[0]行
.Cast()//行是一个ICollection,您需要强制转换每个项
.Select(r=>(int)r[“departmentsubkid”])//对于每一行,从列departmentsubkid获取值
.ToList();

最简单、最有效的方法是根本不使用
数据集
/
数据表

private List<int> GetSubGroupsBelongingToUser()
{
    List<int> list = new List<int>();
    using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["FSK_ServiceMonitor_Users_Management.Properties.Settings.FSK_ServiceMonitorConnectionString"].ConnectionString))
    using (var cmd = new SqlCommand("Test", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        var param = new SqlParameter("@UserId", SqlDbType.Int).Value = int.Parse(cbxSelectUser.Text);
        cmd.Parameters.Add(param);
        con.Open();
        using (var rd = cmd.ExecuteReader())
        {
            while (rd.Read()) list.Add(rd.GetInt32(0)); 
        }
    } // no need to close the connection with the using

    return list;
}

最简单和最有效的方法是根本不使用
数据集
/
数据表

private List<int> GetSubGroupsBelongingToUser()
{
    List<int> list = new List<int>();
    using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["FSK_ServiceMonitor_Users_Management.Properties.Settings.FSK_ServiceMonitorConnectionString"].ConnectionString))
    using (var cmd = new SqlCommand("Test", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        var param = new SqlParameter("@UserId", SqlDbType.Int).Value = int.Parse(cbxSelectUser.Text);
        cmd.Parameters.Add(param);
        con.Open();
        using (var rd = cmd.ExecuteReader())
        {
            while (rd.Read()) list.Add(rd.GetInt32(0)); 
        }
    } // no need to close the connection with the using

    return list;
}

在我看到@Tim的解决方案之前,我已经准备好了这个(它与上述大多数解决方案一样有效):

public List getsubsubjectsbelogingtouser()
{
List departmentsubkids=新列表();
myConnectionString=ConfigurationManager.ConnectionString[“FSK\U ServiceMonitor\U Users\U Management.Properties.Settings.FSK\U ServiceMonitorConnectionString”]。ConnectionString;
使用(mySQLConnection=newsqlconnection(myConnectionString))
{
SqlParameter=newsqlparameter(“@UserId”,getUserID(cbxSelectUser.Text));
mySQLCommand=newsqlcommand(“Test”,mySQLConnection);
mySQLCommand.CommandType=CommandType.StoredProcess;
mySQLCommand.Parameters.Add(参数);
mySQLConnection.ConnectionString=myConnectionString;
mySQLConnection.Open();
SqlDataReader SqlDataReader=mySQLCommand.ExecuteReader();
while(sqlDataReader.Read())
{
departmentsubkids.Add(Convert.ToInt32(sqlDataReader[“departmentsubkid”]);
}
}
返回部门子ID;
}

谢谢大家,非常感谢。

在我看到@Tim的解决方案之前,我已经准备好了这个(它与上述大多数解决方案一样有效):

public List getsubsubjectsbelogingtouser()
{
List departmentsubkids=新列表();
myConnectionString=ConfigurationManager.ConnectionString[“FSK\U ServiceMonitor\U Users\U Management.Properties.Settings.FSK\U ServiceMonitorConnectionString”]。ConnectionString;
使用(mySQLConnection=newsqlconnection(myConnectionString))
{
SqlParameter=newsqlparameter(“@UserId”,getUserID(cbxSelectUser.Text));
mySQLCommand=newsqlcommand(“Test”,mySQLConnection);
mySQLCommand.CommandType=CommandType.StoredProcess;
mySQLCommand.Parameters.Add(参数);
mySQLConnection.ConnectionString=myConnectionString;
mySQLConnection.Open();
SqlDataReader SqlDataReader=mySQLCommand.ExecuteReader();
while(sqlDataReader.Read())
{
departmentsubkids.Add(Convert.ToInt32(sqlDataReader[“departmentsubkid”]);
}
}
返回部门子ID;
}

谢谢大家,非常感谢。

如果您只想要一个
列表
,那么为什么要使用
数据集
?您可以使用
SqlDataReader
循环结果并将每条记录添加到列表中。如果您只需要
列表
,那么为什么要使用
数据集
?您可以使用
SqlDataReader
循环结果并将每条记录添加到列表中。