[英]Get list of IDs from Access Database using OleDbDataReader
Using a Microsoft Access database for a Web App Quiz Manager, I have table with a ID column that has a list of IDs which looks something like this:将 Microsoft Access 数据库用于 Web 应用测验管理器,我有一个带有 ID 列的表,其中包含一个如下所示的 ID 列表:
ID Answer QuesDescription QuesAnswer QuestionNum
1 1 Example Example 1
3 3 Example Example 2
4 4 Example Example 3
6 1 Example Example 4
Using the query SELECT ID FROM (QuizName)
with OleDbCommand
I managed to get the ID values from the database and stored into OleDbDataReader reader
.使用查询SELECT ID FROM (QuizName)
和OleDbCommand
我设法从数据库中获取 ID 值并存储到OleDbDataReader reader
中。 But i don't know how to get the ID values from the reader and store them as a String List.但我不知道如何从阅读器获取 ID 值并将它们存储为字符串列表。 Does anyone know how to do this?有谁知道如何做到这一点?
I've tried using stuff like我试过使用类似的东西
public List<string> GetIDValueFromQuestionNumber(string quizNumber)
{
try
{
string strSQL = string.Concat("SELECT count(ID) as RowCount FROM ", quizNumber);
List<string> resourceNames = new List<string>();
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(strSQL, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
reader.Read();
int rowCount = (int)reader["RowCount"];
strSQL = string.Concat("SELECT ID FROM ", quizNumber);
command = new OleDbCommand(strSQL, connection);
using (reader = command.ExecuteReader())
{
while (reader.Read())
{
resourceNames.Add(" " + reader.GetString(0));
}
}
connection.Close();
for (int count = 0; count < rowCount; count++)
{
int value = (int)reader.GetValue(count);
resourceNames.Add(value.ToString());
}
}
return resourceNames;
}
catch (Exception e)
{
return null;
}
}
But to no luck.但没有运气。
I should note that these tables can vary in depth.我应该注意,这些表格的深度可能会有所不同。
I suggest this approach.我建议这种方法。
Say a form - DataGridView to display our data.说一个表单 - DataGridView 来显示我们的数据。
And say a listbox to display the list of id that you build up into that List并说一个列表框来显示您在该列表中建立的 id 列表
So, this form:所以,这个表格:
And the button click code:和按钮点击代码:
private void button1_Click(object sender, EventArgs e)
{
// load up our data list with Hotels
string strSQL =
@"SELECT ID, FirstName, LastName, City, HotelName
FROM tblHotelsA ORDER BY HotelName";
DataTable rstData = MyRst(strSQL);
dataGridView1.DataSource = rstData;
// now build up a list of id in to string colleciton
List<string> MyIDList = new List<string>();
foreach (DataRow MyOneRow in rstData.Rows)
{
MyIDList.Add(MyOneRow["ID"].ToString());
}
// Lets set the id list to a listbox
listBox1.DataSource = MyIDList;
}
DataTable MyRst(string strSQL)
{
DataTable rstData = new DataTable();
using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
using (OleDbCommand cmdSQL = new OleDbCommand(strSQL, conn))
{
conn.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
And now we get/see this:现在我们得到/看到这个:
So, pull the table.所以,拉桌子。 Display it, do whatever.显示它,做任何事情。
Then use the SAME table, and simple loop each row, grab the ID and add to your list.然后使用 SAME 表,简单地循环每一行,获取 ID 并添加到您的列表中。
And of course, one would probably hide the "id" in the above list (just add the columns using edit columns - only add the ones you want).当然,人们可能会在上面的列表中隐藏“id”(只需使用编辑列添加列 - 只添加您想要的列)。 You can still get/grab/use ANY column from the data source - it not a requirement to display such columns.您仍然可以从数据源获取/抓取/使用任何列 - 不需要显示这些列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.