如何在VB.NET中返回IEnumerable数据表

时间:2017-01-04 06:15:02

标签: c# .net vb.net

我正在尝试将C#代码转换为VB.Net但无法在Vb.NET中找到等效于C#的解决方案产量

我正在迭代数据表。我发现的一种方法是使用list迭代Datatable行。但同样需要将列表转换为数据表。因此,在VB.NET中返回数据表需要什么方法?

这是VB.NET我的代码:

Public Function GetFileData(ByVal sourceFileFullName As String, ByVal dt1 As System.Data.DataTable, ByVal RowCount As Integer) As IEnumerable(Of System.Data.DataTable)

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand


    con.ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings("con1").ConnectionString)

    Dim chunkRowCount As Integer = 0
    Dim Row As String

    Using sr As StreamReader = New StreamReader(sourceFileFullName)

        While Not (Row = sr.ReadLine()) = ""

            If Not RowCount = 0 Then
                chunkRowCount = chunkRowCount + 1
                    //var chunkDataTable = ; //Code for filling datatable or whatever  
                dt1.Rows.Add()

                Dim i As Integer = 0
                Dim Cell As String
                    For Each Cell  in Row.Split(',')

                    If (String.IsNullOrEmpty(Cell)) Then

                        dt1.Rows(dt1.Rows.Count - 1)(i) = DBNull.Value
                        i = i + 1

                    ElseIf Cell = "00.00.0000" Then

                        dt1.Rows(dt1.Rows.Count - 1)(i) = DBNull.Value
                        i = i + 1

                    Else

                        dt1.Rows(dt1.Rows.Count - 1)(i) = Cell
                        i = i + 1
                    End If
                Next
            End If

            RowCount = RowCount + 1

            If chunkRowCount = 10000 Then

                chunkRowCount = 0
                Yield return dt1
                dt1.Clear()
            End If

        End While

    End Using

    If dt1.Rows.Count > 0 Then
        yield return dt1

    End If
End Function

C#代码:

  public static IEnumerable<System.Data.DataTable> GetFileData(string sourceFileFullName, System.Data.DataTable dt1, int RowCount)
    {
        var con = ConfigurationManager.ConnectionStrings["con1"].ConnectionString.ToString();
        var connection = new SqlConnection(con);

        int chunkRowCount = 0;

        string Row;

        using (var sr = new StreamReader(sourceFileFullName))
        {

            //Read and display lines from the file until the end of the file is reached.                
            while ((Row = sr.ReadLine()) != null)
            {
                if (RowCount != 0)
                {
                    chunkRowCount++;
                    //var chunkDataTable = ; //Code for filling datatable or whatever  
                    dt1.Rows.Add();

                    int i = 0;

                    foreach (string Cell in Row.Split(','))
                    {
                        if (String.IsNullOrEmpty(Cell))
                        {
                            dt1.Rows[dt1.Rows.Count - 1][i] = DBNull.Value;
                            i = i + 1;
                        }
                        else if (Cell == "00.00.0000")
                        {
                            dt1.Rows[dt1.Rows.Count - 1][i] = DBNull.Value;
                            i = i + 1;
                        }
                        else
                        {
                            dt1.Rows[dt1.Rows.Count - 1][i] = Cell;
                            i = i + 1;
                        }
                    }

                }
                RowCount = RowCount + 1;

                if (chunkRowCount == 10000)
                {
                    chunkRowCount = 0;
                    yield return dt1;
                    dt1.Clear(); // = null;
                }

            } //end while


        }

        //return last set of data which less then chunk size
        if (dt1.Rows.Count > 0)
            yield return dt1;
    }

1 个答案:

答案 0 :(得分:1)

您可以在函数声明中使用Iterator修饰符,以便能够在VB.NET中使用Yield