我有一个数据表,其中有一个名为sID的列,其值为1到14,所以我想将每个sID的每一行放在一个新的数据表中。
e.g Datatable1 contains all rows with sID=1
Datatable2 contains all rows with sID=2
Datatable3 contains all rows with sID=3
现在我不确定如何使用Datatable.Select()来为每个Datatable填充其行。 感谢
答案 0 :(得分:1)
Datatable.Select()方法是选择行,以填充您需要填充的每个数据表,如下所示:
Dim dt as DataTable
dt.Columns.Add("sID")
dt.Columns.Add("Column2")
Dim dr as Datarow
Dim i as integer
for i = 0 to 3
dr = dt.NewRow()
dr(0) = sID
dr(1) = "Column2Value"
dt.Rows.Add(dr)
next i
或者来自这样的数据库:
Sub FillData()
' 1
' Open connection
Using c As New SqlConnection(Properties.Settings.[Default].DataConnectionString)
c.Open()
' 2
' Create new DataAdapter
Using a As New SqlDataAdapter("SELECT * FROM EmployeeIDs", c)
' 3
' Use DataAdapter to fill DataTable
Dim t As New DataTable()
' 4
' Render data onto the screen
' dataGridView1.DataSource = t; // <-- From your designer
a.Fill(t)
End Using
End Using
End Sub