简体   繁体   English

使用 OleDbDataReader 从 Access 数据库中获取 ID 列表

[英]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.

相关问题 C#使用OleDbDataReader从Access数据库获取值 - c# using OleDbDataReader to get value from Access database 使用OleDBDataReader从Access数据库中选择备注字段时,它只返回字符串的一部分。我怎样才能获得整个字符串? - When using OleDBDataReader to select a memo field from an Access database it only returns part of the string. How can I get the whole string? 在C#中使用OleDbDataReader时无法从Access数据库读取字段信息 - can't read field info from access database when using OleDbDataReader in C# 从读取Access数据库的OleDbDataReader获取值 - Getting values back from OleDbDataReader reading from Access database 从MS Access数据库中获取Long后,使用OleDbDataReader.GetInt64()时获取System.InvalidCastException - Getting System.InvalidCastException when using OleDbDataReader.GetInt64() after fetching Long from MS Access database 将结果集从OleDbDataReader转换为列表 - converting resultset from OleDbDataReader into list 显示来自“ System.Data.OleDb.OleDbDataReader”的“字符串”。 访问数据库到C#表单 - Display a 'string' from 'System.Data.OleDb.OleDbDataReader'. Access Database to C# Form Ms Access 2013中的数据与OleDbDataReader绑定DropDownList - data bind DropDownList with OleDbDataReader from Ms access 2013 从OleDbDataReader中的MS ACCESS读取具有特殊名称的属性 - reading attributes with special names from MS ACCESS in OleDbDataReader 通过另一个列表的ID从列表中获取项目 - Get items from a list by ids of another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM