将数据库值保存为变量

时间:2015-07-30 18:51:36

标签: c# ms-access oledb

我无法弄清楚如何将Access数据库中的个人数据保存到变量中。

我知道你可以保存这样的变量:

int memberID;
cmd.CommandText = "SELECT * FROM MemberDetails WHERE [MemberID] =@MemberID";
cmd.Parameters.AddWithValue("@MemberID", memberID);

try
{
    con.Open();
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        lblName.Text = (memberID);
    }
  con.Close();
}
catch (Exception)
{
    throw;
}

但是我想将所有数据保存到单独的变量中,如下所示:

cmd.CommandText = "SELECT [MemberID] = @MemberID, [Name] = @Name, [Surname] = @Surname";
cmd.Parameters.AddWithValue("@MemberID", memberID);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Surname", surname);

这显然不起作用(否则我不会在这里),但这样的事情是可能的,我将如何做到这一点?

2 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我,但看起来您似乎正在尝试检索已知MemberID的行。如果是这样,那么你总是可以做这样的事情

int memberID = [some number];
DataTable results = new DataTable(); //Your SQL row will be "saved" in here
string command = string.Format("select * from MemberDetails where MemberID = {0}", memberID);

using (SqlDataAdapter sda = new SqlDataAdapter(command, con))
{
    sda.Fill(results); //Your DataTable now has your values
}

您只需要将memberID提供给条件变量即可。

答案 1 :(得分:0)

也许看一下Microsoft的这个很棒的教程,它解释了如何使用C#查询MS Access数据库以及如何处理结果:https://msdn.microsoft.com/en-us/library/ms971485.aspx

你可以做些什么来达到预期的效果呢(这是上面提到的文章中略有修改的例子):

OleDbConnection conn = null;
OleDbDataReader reader = null;
try
{
    conn = new OleDbConnection(
        "Provider=Microsoft.Jet.OLEDB.4.0; " + 
        "Data Source=" + Server.MapPath("MyDataFolder/MyAccessDb.mdb"));
    conn.Open();

    OleDbCommand cmd = 
        new OleDbCommand("Select MemberID, Name, Surname FROM MemberDetails WHERE MemberID = @MemberID", conn);
    cmd.Parameters.AddWithValue("@MemberID", memberID);

    reader = cmd.ExecuteReader();

    while(reader.Read())
    {
        memberID = reader.GetInt32(reader.GetOrdinal("MemberID"));
        name = reader["Name"].ToString();
        surname = reader["Surname"].ToString();
    }

}
finally
{
    if (reader != null)  reader.Close();
    if (conn != null)  conn.Close();
}

但要小心,只有在数据库查询中只返回一个结果时,这才能正常工作。当返回多个记录时,只有最后一个记录将存储在变量中。

当您需要捕获多个结果时,您可能希望创建一个Member类,为每个记录创建一个实例,并将该实例添加到成员列表中。像这样:

    public class Member
    {
        public int MemberId {get; set;}
        public string Name {get; set;}
        public string Surname {get; set;}
    }

public IList<Member> GetMembers()
{
    OleDbConnection conn = null;
    OleDbDataReader reader = null;
    try
    {
        conn = new OleDbConnection(
            "Provider=Microsoft.Jet.OLEDB.4.0; " + 
            "Data Source=" + Server.MapPath("MyDataFolder/MyAccessDb.mdb"));
        conn.Open();

        OleDbCommand cmd = 
            new OleDbCommand("Select MemberID, Name, Surname FROM MemberDetails", conn);

        reader = cmd.ExecuteReader();

        var members = new List<Member>();
        while(reader.Read())
        {
            var member = new Member();
            member.MemberID = reader.GetInt32(reader.GetOrdinal("MemberID"));
            member.Name = reader["Name"].ToString();
            member.Surname = reader["Surname"].ToString();

            members.Add(member);
        }

        return members;
    }
    finally
    {
        if (reader != null)  reader.Close();
        if (conn != null)  conn.Close();
    }

    return null;
}