C# 从数据库向字典添加值

C# 从数据库向字典添加值,c#,sql-server,dictionary,C#,Sql Server,Dictionary,我有一个projest,它有数据库和存储变量。我想在数据库中存储所有值和列,然后再次访问,但当我尝试访问时,我只给出数据库中的最后一个值。我如何解决这个问题 public class Variables { public static readonly List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>(); public stat

我有一个projest,它有数据库和存储变量。我想在数据库中存储所有值和列,然后再次访问,但当我尝试访问时,我只给出数据库中的最后一个值。我如何解决这个问题

public class Variables
{
    public static readonly List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();
    public static Dictionary<string, string> column;
    public static String Name = null;
    public static String Surname = null;
    public static String Gsm = null;
    public static String QRCodeID = null;
    public static String Email = null;
    public static String indirimOrani = null;

}

public class SelectSave()
{
    var cmdSelectSave = new SqlCommand
    {
        CommandText =
            "SELECT Name,Surname,Gsm,QRCodeID,Email,indirimOrani FROM elmacustomers WHERE OnayID=1",
        CommandType = CommandType.Text,
        Connection = sqlConElmaCafePlus
    };

    SqlDataReader reader = null;
    reader = cmdSelectSave.ExecuteReader();
    while (reader.Read())
    {
        Variables.column = new Dictionary<string, string>
        {
            ["Name"] = reader["Name"].ToString(),
            ["Surname"] = reader["Surname"].ToString(),
            ["Gsm"] = reader["Gsm"].ToString(),
            ["QRCodeID"] = reader["QRCodeID"].ToString(),
            ["Email"] = reader["Email"].ToString(),
            ["indirimOrani"] = reader["indirimOrani"].ToString()
        };
        Variables.rows.Add(Variables.column);
    }
}

public void DataReceive()
{
    foreach (Dictionary<string, string> column in Variables.rows)
    {
        MessageBox.Show(Variables.column["Name"]);//That's giving only last Name value in database. I want to scan all names in database.
    }
}
公共类变量
{
公共静态只读列表行=新列表();
公共静态字典列;
公共静态字符串名称=null;
公共静态字符串姓氏=null;
公共静态字符串Gsm=null;
公共静态字符串QRCodeID=null;
公共静态字符串Email=null;
公共静态字符串indirimOrani=null;
}
公共类SelectSave()
{
var cmdSelectSave=new SqlCommand
{
命令文本=
“从elmacustomers选择姓名、姓氏、Gsm、QRCodeID、电子邮件、indirimOrani,其中OnayID=1”,
CommandType=CommandType.Text,
连接=sqlConElmaCafePlus
};
SqlDataReader=null;
reader=cmdSelectSave.ExecuteReader();
while(reader.Read())
{
Variables.column=新字典
{
[“名称”]=读取器[“名称”].ToString(),
[“姓氏”]=读者[“姓氏]。ToString(),
[“Gsm”]=读卡器[“Gsm”].ToString(),
[“QRCodeID”]=读卡器[“QRCodeID”].ToString(),
[“Email”]=阅读器[“Email”].ToString(),
[“indirimOrani”]=阅读器[“indirimOrani”].ToString()
};
Variables.rows.Add(Variables.column);
}
}
public void DataReceive()
{
foreach(Variables.rows中的字典列)
{
MessageBox.Show(Variables.column[“Name”]);//这只提供数据库中的姓氏值。我想扫描数据库中的所有名称。
}
}
DataReceive()
函数中,在对行进行迭代时,对迭代器使用一些不同的名称,如
datarow
,然后使用以下名称:

foreach (Dictionary<string, string> column in Variables.rows.ToList())
{
    MessageBox.Show(column["Name"]);
}
foreach(Variables.rows.ToList()中的字典列)
{
MessageBox.Show(列[“名称]);
}

能否确认从数据库返回了多行?我对这种情况持怀疑态度:
WHERE OnayID=1
当你调试这个,但在选择Save的末尾有一个断点时,Variables.rows是否有不止一个列?是的,当我插入断点时,变量中存储的每个值都有。rows但是我如何访问这个离散值?此处照片:错误:集合已修改;枚举操作可能无法执行。何时出现此错误?通过做这个改变?不是所有的时间。我像你一样改变了什么都没有改变,但是你说你得到的是最后一个as值,你也得到了错误?是的,在点击消息框ok按钮后,我给出了错误