通过C#中的List列出数据库值

时间:2015-10-12 04:01:04

标签: c# mysql

我试图在C#中显示分配给List的数据库值,但在尝试显示时遇到了问题。而不是我想要它显示,它显示了这个:System.Collections.Generic.List`1 [System.String]

这是我的代码:

public List<string>[] selectQuery(string query)
    {
        // create a list to hold the firt name, last name, and password
        List<string>[] list = new List<string>[4];

        list[0] = new List<string>(); // will hold the first name
        list[1] = new List<string>(); // will hold the last name
        list[2] = new List<string>(); // will hold the username
        list[3] = new List<string>(); // will hold the password

        if (this.openDbConnection() == true)
        {
            MySqlCommand cmd = new MySqlCommand(query, this.connection);

            MySqlDataReader dr = cmd.ExecuteReader();

            // read the data and store it in the list
            while (dr.Read())
            {
                list[0].Add(dr["first_name"] + "");
                list[1].Add(dr["last_name"] + "");
                list[2].Add(dr["username"] + "");
                list[3].Add(dr["password"] + "");
            }

            // close reading the data operator
            dr.Close();

            // close connection
            this.connection.Close();

            // return the list of results to be displayed
            return list;
        }
        else
        {
            return list;
        }
    }


  List<string>[] data = db.selectQuery("SELECT first_name, last_name, username, password FROM member WHERE username = '" + name + "'" + " AND password = PASSWORD('" + pass + "')");

        if (data != null)
        {
            // assign the username, password, first and last name to the  session variables
            Session["first_name"] = data[0].ToString();
            Session["last_name"]  = data[1].ToString();
            Session["username"]   = data[2].ToString();
            Session["password"]   = data[3].ToString();


            // redirect to member page
            Response.Redirect("members.aspx", false);
        }

和html是:

    <tr>
        <td id="user"><%=Session["username"].ToString()%></td>
        <td id="greeting"><%=Session["first_name"].ToString()%></td>
    </tr>

任何帮助将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:1)

这比它需要的要复杂得多。创建一个对象:

public class User
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public User()
    {
    }
}

然后在你的代码中:

public User selectQuery(string query)
{
    User myuser = new User();

    //use this if you want to create a List of users and change the method type to "List<User>" and the return to userList.
    List<User> userList = new List<User> ();

    if (this.openDbConnection() == true)
    {
        MySqlCommand cmd = new MySqlCommand(query, this.connection);

        MySqlDataReader dr = cmd.ExecuteReader();

        // read the data and store it in the list
        while (dr.Read())
        {
            myuser.first_name = dr["first_name"] + "";
            myuser.last_name= dr["last_name"] + "");
            myuser.username = dr["username"] + "");
            myuser.password = dr["password"] + "");

            //use the below line only if you are expecting to return multiple users
            userList.Add(myuser);
        }

        // close reading the data operator
        dr.Close();

        // close connection
        this.connection.Close();

        // return the list of results to be displayed
        return myuser;
    }
    else
    {
        return myuser;
    }
}

最后,您只需调用该方法,然后访问返回值的属性

User myuser = db.selectQuery("blah");

        if (data != null)
    {
        // assign the username, password, first and last name to the  session variables
        Session["first_name"] = myuser.first_name.ToString();
        Session["last_name"]  = myuser.last_name.ToString();
        Session["username"]   = myuser.username.ToString();
        Session["password"]   = myuser.password.ToString();
    }

等或如果您返回的列表执行以下操作

List<User> myusers = db.selectQuery("blah")

if(!myusers.Count == 1)  //this will let you check if it returns multiple users for some reason
{
        Session["first_name"] = myusers[0].first_name.ToString();
        Session["last_name"]  = myusers[0].last_name.ToString();
        Session["username"]   = myusers[0].username.ToString();
        Session["password"]   = myusers[0].password.ToString();
}