[英]c# result in mysql = System.Collections.Generic.List
in table "skupina" we have values "pc1,pc2,pc3,..." . 在表“ skupina”中,我们具有值“ pc1,pc2,pc3,...” 。 But in this code I have result in mysql System.Collections.Generic.List
, but should be "pc1,pc2,pc3,...." . 但是在这段代码中,我得到了mysql System.Collections.Generic.List
结果,但是应该是“ pc1,pc2,pc3,....” 。 Where is my mistake? 我的错误在哪里?
private void button1_Click(object sender, EventArgs e)
{
using (MySqlConnection cnn = new MySqlConnection("Server=10.7.18.35;Database=OitDB;Uid=martin;Pwd=;"))
{
MySqlDataAdapter da = new MySqlDataAdapter("SELECT namepc FROM skupina where nazovskup= 'mojask' ", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "skupina");
List<string> skName = new List<string>();
foreach (DataRow row in ds.Tables["skupina"].Rows)
{
skName.Add(row["namepc"].ToString());
string constring = "Server=10.7.18.35;Database=OitDB;Uid=martin;Pwd=;";
var Query = "INSERT INTO OitDB.skup(uzivatel)VALUES('" + skName + "')";
MySqlConnection conDatabase = new MySqlConnection(constring);
MySqlCommand cmdDatabase = new MySqlCommand(Query, conDatabase);
MySqlDataReader myReader;
try
{
conDatabase.Open();
myReader = cmdDatabase.ExecuteReader();
MessageBox.Show("Správa odoslaná!");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
You're implicitly calling ToString()
on a List<T>
. 您在List<T>
上隐式调用ToString()
。 That doesn't do what you expect it to, because List<T>
doesn't override ToString()
. 那并没有达到您的期望,因为List<T>
不会覆盖ToString()
。 You can use 您可以使用
// Note: the ToArray() call is only required if you're targeting .NET 3.5, which
// is massively out of date, and really shouldn't be used IMO.
string.Join(", ", skName.ToArray())
to get a comma-separated result... but you really shouldn't do it where you're currently doing it. 以获得逗号分隔的结果...但是您实际上不应该在当前正在执行的操作中执行此操作。 Instead, you should use parameterized SQL, eg 相反,您应该使用参数化的SQL,例如
var query = "INSERT INTO OitDB.skup (uzivatel) VALUES (@name)";
...
cmdDatabase.Parameters.Add("@name", MySqlDbType.VarChar).Value
= string.Join(", ", skName.ToArray());
Always, always use parameterized SQL for values. 始终,始终对参数使用SQL。 You should only ever build SQL dynamically for non-value parts - and in that case you need to be really, really careful. 您应该只为非价值部分动态地构建SQL-在这种情况下,您需要非常非常小心。
I would also suggest using using
directives for your command and data reader... and currently you've got two different connections open, which seems unnecessary... 我还建议对命令和数据读取器使用using
指令...当前您打开了两个不同的连接,这似乎没有必要...
I guess you are left with flattening your List to a string like: 我想您剩下的就是将您的List展平为类似以下的字符串:
// Convert the ArrayList to a string
string separator = "|"; // Any string that doesn't collide with content
string arrayListAsString = String.Join(separator, skName.ToArray());
Then save the string in the database. 然后将字符串保存在数据库中。 When reading the string from the database you'll probably want to convert it back to an ArrayList: 从数据库中读取字符串时,您可能需要将其转换回ArrayList:
// Convert the string back to an List
ArrayList list = new ArrayList();
list.AddRange(arrayListAsString.Split(new string[]{separator}, StringSplitOptions.RemoveEmptyEntries));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.