我得到了现在没有错误显示但它也没有向db发送任何内容我认为它没有看到ComplexStruct我想读出ComplexStruct中的所有变量并将它们发送到db但是即使这不起作用你有什么想法吗?以及如何声明变量?
这是整个代码:
以下所有内容都必须发送到我的数据库我的数据库表结构如下:a,b,c,d,e,f .....,aa,ab,ac,ad,ae,.. ..,ba,bb,bc等等到cz
这一切都应该在这一部分完成:
提前谢谢我希望有人可以帮助我这就是我现在所拥有的:
}
private void Main2()
{
{
var structure = new { Datavalues = new short[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13, 14, 15, 16, 17, 18 ,19, 20, 21, 22, 23, 24 ,25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 82, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110 } };
var fields = new[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax", "ay", "az", "ba", "bb", "bc", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bk", "bl", "bm", "bn", "bo", "bp", "bq", "br", "bs", "bt", "bu", "bv", "bw", "bx", "by", "bz", "ca", "cb", "cc", "cd", "ce", "cf", "cg", "ch", "ci", "cj", "ck", "cl", "cm", "cn", "co", "cp", "cq", "cr", "cs", "ct", "cu", "cv", "cw", "cx", "cy", "cz",};
string cs = @"server=localhost;userid=root;
password=flex01;database=gen1";
for (int i = 0; i < 5; i++)
{
using (var conn = new MySqlConnection(cs))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "INSERT INTO actualvalues (" + string.Join(", ", fields) + ") VALUES (" + string.Join(", ", fields.Select(r => "@" + r)) + ")";
for (int i = 0; i < fields.Length; i++)
cmd.Parameters.Add("@" + fields[i], SqlDbType.SmallInt).Value = structure.Datavalues[i];
}
}
}
}
它给了我以下错误:
错误1'System.Array'不包含'Select'的定义,也没有扩展方法'Select'接受类型'System.Array'的第一个参数可以找到(你是否缺少using指令或程序集)参考):
错误2在此范围内无法声明名为“i”的局部变量,因为它会给“i”赋予不同的含义,“i”已在'父级或当前'范围内用于表示其他内容
答案 0 :(得分:0)
拳头你的问题不明确,不要将你的所有代码发布到pastebin,只需添加一些你认为问题所在的代码行。但是我看到你创建sql命令的代码并不完全正确。您的命令应如下所示:
cmd.CommandText = "INSERT INTO actualvalues (a, b) VALUES (@A, @b)";//add c,d and so on
cmd.Parameters.Add("@A", SqlDbType.SmallInt);
cmd.Parameters["@A"].Value = structure.datavalues[0];
cmd.Parameters.Add("@b", SqlDbType.SmallInt);
cmd.Parameters["@b"].Value = structure.datavalues[1];
//... add parameters for c,d and so on
就像我在评论中发表的SqlCommand.Parameters documentation一样,有一个例子说明了一切都已完成。
答案 1 :(得分:0)
正如@renuiz所说,你使用了参数错误。
这就是我在一段代码中看到解决方案的方式:
// your structure, that you have defined before:
var structure = new { Datavalues = new short[] { 1, 2, 3, 4, 5, 6, ... } };
// put all your fields here. !!must be the same length as structure.Datavalues:
var fields = new[] { "a", "b", "c", "d", "e", ..., "cz" };
cmd.CommandText = "INSERT INTO actualvalues (" + string.Join(", ", fields) + ") VALUES (" + string.Join(", ", fields.Select(r => "@" + r)) + ")";
for (int i = 0; i < fields.Length; i++)
cmd.Parameters.Add("@" + fields[i], SqlDbType.SmallInt).Value = structure.Datavalues[i];