【发布时间】:2017-01-30 07:55:07
【问题描述】:
我创建了一个表示 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);";
当我创建 createQuery2 时,它会返回(在 VALUES 内)以下内容:
System.Collections.Generic.Dictionary
2[System.Int32,System.String] System.Collections.Generic.Dictionary2[System.Int32,System.String] System.Collections.Generic.Dictionary2[System.Int32,System.String] System.Collections.Generic.Dictionary2[System.Int32,System.String]
而不是字典中的值。 有人可以帮我更好地想象这个问题吗?为什么我看不到字典元素?
【问题讨论】:
-
fieldValues[i].dictionary 是一个字典,你只是它的 ToString,如果你使用 fieldValues[i].dictionary[0] 你会得到价值。但是:您的代码对 sql 注入是开放的,使用参数而不是仅仅连接值!
-
你能解释一下字典的两个键分别代表什么吗?它们是字段的名称和值吗?
标签: c# mysql .net dictionary generics