Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中结束循环时存储数据集#_C#_List_Dataset - Fatal编程技术网

C# 在C中结束循环时存储数据集#

C# 在C中结束循环时存储数据集#,c#,list,dataset,C#,List,Dataset,我正在使用此代码填充数据集 var ds = new DataSet(); List<string> list = new List<string>(); foreach (DataGridViewRow row in grdInterPOList.Rows) { DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell; if (Convert.ToBoolean(ch

我正在使用此代码填充数据集

var ds = new DataSet();
List<string> list = new List<string>();
foreach (DataGridViewRow row in grdInterPOList.Rows)
{
    DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
    if (Convert.ToBoolean(chk.Value) == true)
        if (row.Cells.Count >= 2 && row.Cells[1].Value != null) 
        {
            list.Add(row.Cells[1].Value.ToString());
        }
}
foreach (var _PO_No in list)
{

        ds= ShippingData_Export(_PO_No);

}


private DataSet ShippingData_Export(string X_PO_NO)
{
    var ds = new DataSet();
    var sqlConn = new SqlConnection(strConStr);
    try
    {
        sqlConn.Open();
        var cmd = new SqlCommand("proc_TBL_PO_M_ShippingExcel", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@X_PO_NO", X_PO_NO);
        var da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        sqlConn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    return ds;
}
var ds=new DataSet();
列表=新列表();
foreach(grdInterPOList.Rows中的DataGridViewRow行)
{
DataGridViewCheckBoxCell chk=行。单元格[0]作为DataGridViewCheckBoxCell;
if(Convert.ToBoolean(chk.Value)==true)
if(row.Cells.Count>=2&&row.Cells[1]。值!=null)
{
list.Add(row.Cells[1].Value.ToString());
}
}
foreach(列表中的变量编号)
{
ds=发货数据导出(采购订单号);
}
私有数据集ShippingData_导出(字符串X_PO_编号)
{
var ds=新数据集();
var sqlConn=新的SqlConnection(strConStr);
尝试
{
sqlConn.Open();
var cmd=new SqlCommand(“proc_TBL_PO_M_ShippingExcel”,sqlConn);
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.AddWithValue(@X_PO_NO”,X_PO_NO);
var da=新的SqlDataAdapter(cmd);
da.填充(ds);
sqlConn.Close();
}
捕获(例外情况除外)
{
Show(例如ToString());
}
返回ds;
}
如果我们选择了超过1 _PO__No,那么我们只会在循环后得到最后一个值。 所以我的问题是如何在结束循环后存储数据集值。
例如:如果我们选择2_PO_No:GV01和GV02,则在结束循环之后。我们将同时获得2个PO No,而不仅仅是最后一个(GV02)

代码中的问题是:

foreach (var _PO_No in list)
{
    ds = ShippingData_Export(_PO_No);
}
您在列表中循环,并对列表中的每个项目调用
ShippingData\u Export
,但每次调用此方法时,您都会覆盖
ds
。这就是为什么只存储最后一个值

如果希望单个
数据集
包含多个结果,则需要将
数据集
传递到
ShippingData\u Export
方法,并将结果添加到其中,而不是每次创建一个新结果

结果会是这样的:

foreach (var _PO_No in list)
{
    ShippingData_Export(_PO_No, ds);
}

private void ShippingData_Export(string X_PO_NO, DataSet ds)
{
    var sqlConn = new SqlConnection(strConStr);
    try
    {
        sqlConn.Open();
        var cmd = new SqlCommand("proc_TBL_PO_M_ShippingExcel", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@X_PO_NO", X_PO_NO);
        var da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        sqlConn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    return ds;
}
代码中还有几个其他小问题:

  • 将{}添加到行
    if(Convert.ToBoolean(chk.Value)==true)
    后的块中。习惯显式地使用{}使代码更具可读性
  • 您正在手动打开SQL连接,但在发生异常时不关闭它。查看网络,确保即使出了问题,也始终关闭连接

很抱歉,在我的案例中,我仍然不知道如何让一个数据集包含多个结果。请多解释!我需要这个数据集将数据导出到Excel!谢谢@user3035133我添加了一些代码来演示最终结果。这有用吗?