简体   繁体   English

在C#中使用OleDbDataReader时无法从Access数据库读取字段信息

[英]can't read field info from access database when using OleDbDataReader in C#

i wrote a form based C# program to read/write to a access data base. 我编写了一个基于表单的C#程序来读取/写入访问数据库。 in it i have 2 tables: User and Item the following code should have read a spesific field from the User table and return it to me based on the field number but, i can't get it to work for some reason... help would be good at this point (4AM) 在其中我有2个表:User和Item下面的代码应该已经从User表中读取了一个特殊的字段,并根据字段编号将其返回给我,但是由于某种原因我无法使其正常工作...帮助此时会比较好(凌晨4点)

 public string UserGetField(int user_id,int field)
        {
            string found="";
            string command="";
            switch (field)
            {
                case 1://first_name
                    command+= "first_name";
                    break;
                case 2://last_name
                      command+= "last_name";
                    break;
                case 3://grade
                      command+= "grade";
                    break;
                case 4://phone
                      command+= "phone";
                    break;
                case 5://address
                      command+= "address";
                    break;
                case 6://item
                    command += "item";
                    break;
            }
            cmd.CommandText = "select '" + command + "' from User where user_id = '" + user_id + "'";
            con.Open(); // open the connection
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                found += dr["first_name"].ToString();
            }
            con.Close();
            return found;
        }

try this: 尝试这个:

    cmd.CommandText = "select [" + command + "] from User where user_id = '" + user_id + "'";

Also, you're dynamically choosing the select field but always looking for the first_name field in the result. 另外,您正在动态选择选择字段,但始终在结果中寻找first_name字段。 Change to 改成

        while (dr.Read())
        {
            found += dr[0].ToString();  // first field value
        }

Try this ? 尝试这个 ?

    public string UserGetField(int user_id, int field)
    {
        var columns = new string[] { "first_name", "last_name", "grade", "phone", "address", "item" };

        var list = new List<string>();
        string command = columns[field - 1];
        var sql = string.Format("select '{0}' from User where user_id = '{1}'", command, user_id);
        var conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + @"\test.mdb"; //

        using (var conn = new OleDbConnection(conStr))
        {
            conn.Open();
            var cmd = new OleDbCommand(sql, conn);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                list.Add(dr[command].ToString());
            }
        }
        return string.Join(",", list.ToArray());
    }

this is the final version that works... swithced from table named User to Kids and made someother changes 这是有效的最终版本...从名为User的表切换到Kids并进行了其他更改

 public string UserGetField(int user_id,int field)
        {
            string found="";
            string command="";
            switch (field)
            {
                case 1://first_name
                    command+= "first_name";
                    break;
                case 2://last_name
                      command+= "last_name";
                    break;
                case 3://grade
                      command+= "grade";
                    break;
                case 4://phone
                      command+= "phone";
                    break;
                case 5://address
                      command+= "address";
                    break;
                case 6://item
                    command += "item";
                    break;
            }
            cmd.CommandText = "SELECT " + command + " FROM Kids WHERE user_id = " + user_id + "";
            con.Open(); // open the connection
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                found = dr[command].ToString();      
            }
            con.Close();
            return found;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循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? 显示来自“ System.Data.OleDb.OleDbDataReader”的“字符串”。 访问数据库到C#表单 - Display a 'string' from 'System.Data.OleDb.OleDbDataReader'. Access Database to C# Form C#.NET:OleDbDataReader类的读取方法无法运行 - C# .NET: read method of OleDbDataReader class won't run OleDbDataReader,C#-OleDbDataReader可以从excel文件中检索的最大行数是多少? - OleDbDataReader, C# - What is the maximum limit of the number of rows OleDbDataReader can retrieve from an excel file? 使用 OleDbDataReader 从 Access 数据库中获取 ID 列表 - Get list of IDs from Access Database using OleDbDataReader 如何使用C#方法从OleDbDataReader获取结果 - How can get result from OleDbDataReader with C# method 如何使用c#从数据库中读取数组字段? - How to read array field from database using c#? 从MS Access数据库中获取Long后,使用OleDbDataReader.GetInt64()时获取System.InvalidCastException - Getting System.InvalidCastException when using OleDbDataReader.GetInt64() after fetching Long from MS Access database 使用oledbdatareader C#移至下一条记录 - Move to next record using oledbdatareader C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM