vb.net 创建数据表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5239469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:19:54 来源:igfitidea点击:
vb.net creating data table?
提问by Alex
I'm using vb.net / winforms. How can I convert 10 lines with three columns into a DataSet/DataTable?
我正在使用 vb.net/winforms。如何将三列的 10 行转换为数据集/数据表?
Lines are something like this:
线是这样的:
Item-1, 0, 44
Item-2, , 3
etc
回答by Bala R
Dim Table1 As DataTable
Table1 = New DataTable("TableName")
Dim column1 As DataColumn = New DataColumn("Column1")
column1.DataType = System.Type.GetType("System.String")
Dim column2 As DataColumn = New DataColumn("Column2")
column2.DataType = System.Type.GetType("System.Int32")
Dim column3 As DataColumn = New DataColumn("Column2")
column3.DataType = System.Type.GetType("System.Int32")
Table1.Columns.Add(column1)
Table1.Columns.Add(column2)
Table1.Columns.Add(column3)
Dim Row1 As DataRow
Row1 = Table1.NewRow()
Row1.Items("Column1") = "Item1"
Row1.Items("Column2") = 44
Row1.Items("Column3") = 99
Table1.Rows.Add(Row1)
' Repeat for other rows
回答by Phil Nicholas
I know this post is old, but I believe this can be solved in fewer lines of code.
我知道这篇文章很旧,但我相信这可以用更少的代码行来解决。
' Declare DataTable
Dim Table1 As DataTable
' Define columns
Table1.Columns.Add("Column1", GetType(System.String))
Table1.Columns.Add("Column2", GetType(System.Int32))
Table1.Columns.Add("Column3", GetType(System.Int32))
' Add a row of data
Table1.Rows.Add("Item1", 44, 99)
Table1.Rows.Add("Item2", 42, 3)