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

时间:2012-03-07 03:28:44

标签: c# sql ms-access oledb

我写了一个基于表单的C#程序来读/写访问数据库。 在它我有2个表:用户和项目 以下代码应该从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;
        }

3 个答案:

答案 0 :(得分:0)

试试这个:

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

此外,您正在动态选择选择字段,但始终在结果中查找first_name字段。改为

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

答案 1 :(得分:0)

试试这个?

    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());
    }

答案 2 :(得分:0)

这是有效的最终版本......从名为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;
        }