简体   繁体   English

mysql = System.Collections.Generic.List中的C#结果

[英]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);
            }
        }           
    }
}

here is error 这是错误

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.

相关问题 C# 中的 System.Collections.Generic.List`1[System.Char] - System.Collections.Generic.List`1[System.Char] in C# C# System.Collections.Generic.List 中的错误<T> ? - Bug in C# System.Collections.Generic.List<T>? C# SelectMany 错误“无法隐式转换类型‘System.Collections.Generic.List<char> ' 到 'System.Collections.Generic.List<list> ’”</list></char> - C# SelectMany error " Cannot Implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<List>' " C#:无法隐式转换类型“System.Collections.Generic.List”<char> &#39; 到 &#39;System.Collections.Generic.List<string> &#39; - C#: Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>' Cannot convert type 'System.Collections.Generic.List&lt; &gt;' to 'System.Collections.Generic.IList&lt; &gt;' c# MongoDB - Cannot convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >' c# MongoDB C# LINQ - 道具字符串的值为 System.Collections.Generic.List`1[System.String] - C# LINQ - Prop string has value of System.Collections.Generic.List`1[System.String] 新手:C#列表返回System.Collections.Generic.List - Newbie: C# List returns back System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List <Model.Common.Province> &#39;l C#MVc - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<Model.Common.Province>'l C# MVc Gurobi,C#:无法从'int'转换为System.Collections.Generic.List <int> - Gurobi, C#: Can't convert from 'int' to System.Collections.Generic.List<int> 无法在gridview(C#),System.Collections.Generic.List中查看sqlite表数据 - can't view sqlite table data in gridview (C#), System.Collections.Generic.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM