列出DataSet记录 [英] Listing DataSet Records
问题描述
我正在尝试获取要在表格中显示的数据集中的所有记录的列表。我已经尝试了通过研究找到的几个选项,并且没有在哪里找到。任何人都有更简单的想法?作为一般的编程新手,这给了我适合。这是我尝试过的最后一件事:
strSQL ="从Safety_Data中选择*,其中xref_id =" &安培; strTypePull& "和Shift_ID =" &安培; strShiftPull& " and Date =''" &安培; strSelDate& "''"
Dim objDataSet As New Data.DataSet
Dim objConn As New Data.SqlClient.SqlConnection(" Data Source = XXXXXX; Initial Catalog = XXXXXX ;用户ID = XXXXXX;密码= XXXXXX;")
Dim objCmd As New Data.SqlClient.SqlDataAdapter(strSQL,objConn)
objConn.Open()
objCmd.Fill(objDataSet)
Dim objDC As Data.DataColumn
Dim objDT As Data.DataRow
For each objDT in objDataSet.Tables(0).Rows
For each objDC in objDataSet.Tables(0).Columns
txtRecID = objDataSet。表(0)。行(0)(RID)。ToString()
txtDate = objDataSet.Tables(0).Rows(0)(" Date")。ToString()
txtShift = objDataSet.Tables(0).Rows(0)(" Shift_ID")。ToString()
txtType = objDataSet.Tables(0).Rows( 0)(" Xref_ID& ()。ToString()
TxtOccNotes = objDataSet.Tables(0).Rows(0)(" Notes")。ToString()
下一页>
下一步
I am trying to get a listing of all records in a dataset to display in a table. I have tried several options I have found through research and get no where. Anyone have any easier ideas? Being new to programming in general, this have given me fits. Here''s the last thing I tried:
strSQL = "Select * from Safety_Data where xref_id=" & strTypePull & "and Shift_ID=" & strShiftPull & "and Date=''" & strSelDate & "''"
Dim objDataSet As New Data.DataSet
Dim objConn As New Data.SqlClient.SqlConnection("Data Source=XXXXXX;Initial Catalog=XXXXXX;User ID=XXXXXX;password=XXXXXX;")
Dim objCmd As New Data.SqlClient.SqlDataAdapter(strSQL, objConn)
objConn.Open()
objCmd.Fill(objDataSet)
Dim objDC As Data.DataColumn
Dim objDT As Data.DataRow
For Each objDT In objDataSet.Tables(0).Rows
For Each objDC In objDataSet.Tables(0).Columns
txtRecID = objDataSet.Tables(0).Rows(0)("RID").ToString()
txtDate = objDataSet.Tables(0).Rows(0)("Date").ToString()
txtShift = objDataSet.Tables(0).Rows(0)("Shift_ID").ToString( )
txtType = objDataSet.Tables(0).Rows(0)("Xref_ID").ToString()
TxtOccNotes = objDataSet.Tables(0).Rows(0)("Notes").ToString()
Next
Next
推荐答案
您在寻找什么?所有的行?
根据你的查询,似乎objDataSet只有一个表所以
objDataSet.Tables(0)
What are you looking for? All the rows?
Based on your query it would appear that objDataSet will only have one table so
objDataSet.Tables(0)
您不需要循环遍历列的foreach循环。可以通过循环遍历行来引用列。
row(i)(列名)
删除foreach循环循环遍历列,代码应该适合你。
Nathan
You don''t need the foreach loop that loops through the columns. The columns can be referenced by looping through the rows as you are doing.
row(i)(columnname)
Remove the foreach loop that loops through the columns and the code should work properly for you.
Nathan
这是更改的代码。我在数据集中有3条记录,但它只显示一条。
Dim objDC As Data.DataColumn
Dim objDT As Data.DataRow
Dim i As Integer
for each objDT in objDataSet.Tables(0).Rows
txtRecID.Text = objDataSet.Tables(0).Rows (i)(RID)。ToString()
txtDate.Text = objDataSet.Tables(0).Rows(i)(" Date")。ToString()
txtShift.Text = objDataSet.Tables(0).Rows(i)(" Shift_ID")。ToString()
txtType.Text = objDataSet.Tables(0).Rows( i)(Xref_ID)。ToString()
TxtOccNotes.Text = objDataSet.Tables(0).Rows(i)(" Notes")。ToString()
下一步
This is the changed code. I have 3 records in dataset but it only displays one.
Dim objDC As Data.DataColumn
Dim objDT As Data.DataRow
Dim i As Integer
For Each objDT In objDataSet.Tables(0).Rows
txtRecID.Text = objDataSet.Tables(0).Rows(i)("RID").ToString()
txtDate.Text = objDataSet.Tables(0).Rows(i)("Date").ToString()
txtShift.Text = objDataSet.Tables(0).Rows(i)("Shift_ID").ToString( )
txtType.Text = objDataSet.Tables(0).Rows(i)("Xref_ID").ToString()
TxtOccNotes.Text = objDataSet.Tables(0).Rows(i)("Notes").ToString()
Next
这篇关于列出DataSet记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!