Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从下面的语法中访问c#dictionary元素?_C#_Mysql_.net_Dictionary_Generics - Fatal编程技术网

如何从下面的语法中访问c#dictionary元素?

如何从下面的语法中访问c#dictionary元素?,c#,mysql,.net,dictionary,generics,C#,Mysql,.net,Dictionary,Generics,我创建了一个列表,表示xls工作表中的单元格值,它看起来像这样,后面是一个INSERT语句,我将使用该语句将值放入某个数据库中: List<FieldValues> fieldValues = new List<FieldValues>() { new FieldValues(tableFields[0]) { dictionary = new Dictionary<int, string>() {

我创建了一个列表,表示xls工作表中的单元格值,它看起来像这样,后面是一个INSERT语句,我将使用该语句将值放入某个数据库中:

List<FieldValues> fieldValues = new List<FieldValues>()
{
    new FieldValues(tableFields[0]) {
        dictionary = new Dictionary<int, string>()
        {
            {0, "" },
            {1, "" }
        }

    },
    new FieldValues(tableFields[1]) {
        dictionary = new Dictionary<int, string>()
        {
            {0, "x" },
            {1, "x" }
        }

    },
    new FieldValues(tableFields[2]) {
        dictionary = new Dictionary<int, string>()
        {
            {0, "x" },
            {1, "x" }
        }

    },
    new FieldValues(tableFields[3])  {
        dictionary = new Dictionary<int, string>()
        {
            {0, "Car" },
            {1, "Travel" }
        }

    },
};


string createQuery2 = createQuery + "\n INSERT INTO `poi_specs_mgu_ece_1.6` (";

for (var i = 0; i < tableFields.Count; i++)
{
    createQuery2 += "\n\t" + tableFields[i].GetFieldDeclaration() + (i == tableFields.Count - 1 ? string.Empty : ",");
}
createQuery2 += ")\n VALUES ( ";


for (var i = 0; i < fieldValues.Count; i++)
{

    createQuery2 += "\n\t" + fieldValues[i].dictionary; //+ (i == fieldValues.Count - 1 ? string.Empty : ",");
}
createQuery2 += " \n);";
List fieldValues=新列表()
{
新字段值(表字段[0]){
字典=新字典()
{
{0, "" },
{1, "" }
}
},
新字段值(tableFields[1]){
字典=新字典()
{
{0,“x”},
{1,“x”}
}
},
新字段值(tableFields[2]){
字典=新字典()
{
{0,“x”},
{1,“x”}
}
},
新字段值(tableFields[3]){
字典=新字典()
{
{0,“Car”},
{1,“旅行”}
}
},
};
字符串createQuery2=createQuery+“\n插入'poi_specs_mgu_ece_1.6'(”;
对于(var i=0;i
当我创建createQuery2时,它返回(内部值)以下内容:

System.Collections.Generic.Dictionary
2[System.Int32,System.String]
System.Collections.Generic.Dictionary
2[System.Int32,System.String] System.Collections.Generic.Dictionary
2[System.Int32,System.String]
System.Collections.Generic.Dictionary
2[System.Int32,System.String]

而不是字典中的值。 有人能帮我更好地想象这个问题吗?为什么我不能阅读字典元素?

在这部分代码中:

createQuery2 += "\n\t" + fieldValues[i].dictionary;
当字典是一个对象时,您正在将它添加到字符串中,因此它试图将您的对象变成一个字符串,该字符串只返回类型

您需要直接访问字典值

像这样:

createQuery2 += "\n\t" + fieldValues[i].dictionary[0] + fieldValues[i].dictionary[1];

fieldValues[i]。dictionary是一个字典,如果使用fieldValues[i]。dictionary[0],您将获得vaue。但是:您的代码对sql注入是开放的,请使用参数,而不仅仅是串联值!你能解释一下什么代表这本词典的两个键吗?它们是字段的名称和值吗?但是,假设我想创建一个方法来检查字典是否有特定的行?类似这样的新字段值(tableFields[3]){dictionary=newdictionary(){{{0,“Car”},{1,“Travel”}我如何访问字典行对不起,您可以忽略我的最后一条评论:应该是,但是,假设我想创建一个方法来检查字典是否有某一行?类似于以下公共字符串GetRowValue(int row){return string.Empty;}如何访问以验证我的字典是否有某一行?如果我理解正确,您应该能够调用新的
GetRowValue(int row)
内联方法,而不是
fieldValues[I]。字典[row]
。也就是说,似乎有更好的方法来完成您正在尝试的任务。如果您编辑问题以提供有关您想要完成的任务的更多详细信息,也许人们可以帮助您找到更好的解决方案。主要思想是,我想检查某一行的存在性,具体取决于我定义的类型: