VB.NET创建数据表?

时间:2011-07-11 21:52:17

标签: vb.net datatable

我正在尝试在VB.NET中创建一个数据表,但是当我从该表输出一行时,它显示:“System.Data.DataRow”。

我将我的代码与几个网站进行了比较,看起来是正确的。但是,如果我代替Response.Write这些值,它们会正确显示。

所以这是我的代码:

        Sub Page_Load(Sender as Object, E as EventArgs)
            If Not IsPostback Then 
...
            End If

            Main
        End Sub

        Sub Main()
            Dim FirstMonthDate As Date = CDate("1/1/" & dYear.SelectedValue)
            Dim LastMonthDate As Date = GlobalFunctions.GlobalF.MonthLastDate(dYear.SelectedValue & "-" & dEndMonth.SelectedValue & "-1")
            Dim TheGroup As String
            Dim LastWeek As String
            Dim FirstWeek As String
            Dim dLastWeek As Date
            Dim dFirstWeek As Date
            Dim iWeek As String

            Dim commandstring As String = "SELECT cast(DateDiff(wk, '1/1/2011', '" & LastMonthDate & "') as varchar(30)) AS SELECTED_WEEK "
            Dim DSDateData As New System.Data.DataSet
            DSDateData = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)
            Dim dRow As DataRow

            For Each dRow In DSDateData.Tables(0).Rows
                iWeek = dRow("SELECTED_WEEK")
            Next

            Dim FirstYearDate As Date = CDate("1/1/" & dYear.SelectedValue)

            commandstring = "SELECT DateAdd(ww, " & iWeek & ", '" & FirstYearDate & "')  AS LAST_WEEK "
            Dim DSDateData2 As New System.Data.DataSet
            DSDateData2 = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)

            For Each dRow In DSDateData2.Tables(0).Rows
                LastWeek = dRow("LAST_WEEK")
            Next

            dLastWeek = CDate(LastWeek)

            commandstring = "SELECT DATEADD(dd, -1, DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,6-datepart(day,'" & LastMonthDate & "'),'" & LastMonthDate & "') ), 0)) AS FIRST_WEEK "
            Dim DSDateData3 As New System.Data.DataSet
            DSDateData3 = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)

            For Each dRow In DSDateData3.Tables(0).Rows
                FirstWeek = dRow("FIRST_WEEK")
            Next

            dFirstWeek = CDate(FirstWeek)
            dFirstWeek = DateAdd(DateInterval.WeekOfYear, -1, dFirstWeek)

            Dim arrDayYear As New ArrayList()
            Dim dFirstDay As New Date

            Dim arrList As ArrayList = New ArrayList()

            Dim dt2 As New DataTable
            Dim sWeek As DataColumn = New DataColumn("sWeek")
            sWeek.DataType = System.Type.GetType("System.String")
            Dim sDay As DataColumn = New DataColumn("sDay")
            sDay.DataType = System.Type.GetType("System.String")
            dt2.Columns.Add(sWeek)
            dt2.Columns.Add(sDay)

Response.Write("NW1")

            Dim dr As DataRow
            dr = dt2.NewRow()
            dr("sWeek") = "New week"
            dr("sDay") = "New day"
            dt2.Rows.Add(dr)

            Response.Write(dr)
            Response.Write("<br>")

            dFirstDay = dFirstDay.AddDays(1)
...
End Sub
...

这是导致“System.Data.DataRow”的最后一个Response.Write。但是之前的Response.Write(NW1)会打印出来。那么我做错了什么?

3 个答案:

答案 0 :(得分:2)

您的代码是正确的。

致电Response.Write(dr)打印dr.ToString() 由于DataRow未覆盖ToString(),因此jsut会返回typename。

您可能想要打印列的值。

答案 1 :(得分:1)

您必须迭代数据行或选择特定数据行。迭代和输出值将是这样的:

//c#
     foreach (DataRow row in dt.Rows) 
     {
        foreach (DataColumn col in dt.Columns)
           Console.WriteLine(row[col]);
     }  

//VB.Net
     For Each (row As DataRow In dt.Rows) 

        For Each (col As DataColumn In dt.Columns)
           Console.WriteLine(row[col])
        Next
     Next

仅打印第一行:

DataRow row = dt.Rows[0];

foreach (DataColumn col in row.Columns)
   Console.WriteLine(row[col]);

答案 2 :(得分:0)

Response.Write()需要一个字符串,因此它将你的变量(dr)转换为字符串。默认的“toString”方法只使用类的名称。在这种情况下“System.Data.DataRow”

似乎你可能想要遍历行并单独输出每个值,例如......

For Each element In dr.ItemArray
  Response.Write(element)
Next
Response.Write("<br>")