MySql查询不显示结果

时间:2014-08-12 15:40:31

标签: c# mysql sql datagridview

我有一个字符串,其值如下: A,B,C,d,E

这个想法是使用sql命令在数组中搜索条目。 sql是:

select * from pelates where Onoma in (@giorti)

我将a,b,c,d,e转换为'a','b','c','d','e'然后我用preius字符串替换@giorti值来填充数据网格视图。 问题是datagridview保持为空,除非我编辑sql语句并手动给出一个值,如:

select * from pelates where Onoma in ('Bampis')

如果我这样做,结果就像正常一样。请建议,我认为存在编码问题,因为数据是希腊语 这是我的代码:

 //Diaxorismos onomaton pou giortazoun

        String giorti = label2.Text;
        String[] name = giorti.Split(',');

        for (int i = 0; i < name.Length; i++)
        {
            StringBuilder sb = new StringBuilder(name[i]);
            sb.Insert(0, "'");
            sb.Append("'");
            name[i] = sb.ToString();
            name[i] = name[i];
        }

        String finalName = String.Join(",", name);
        finalName = finalName.Replace(" ", "");
        textBox1.Text = finalName;
        //


        showPelatesPouGiortazounCommand = login.connection.CreateCommand();
        showPelatesPouGiortazounCommand.CommandText = "select * from pelates where Onoma in (@giorti)";
        showPelatesPouGiortazounCommand.Parameters.AddWithValue("@giorti", System.Text.Encoding.UTF8.GetBytes(finalName));
        getPelatesPouGiortazounAdapter = new MySqlDataAdapter(showPelatesPouGiortazounCommand);
        pelatesPouGiortazounDataset = new DataSet();
        getPelatesPouGiortazounAdapter.Fill(pelatesPouGiortazounDataset);
        dataGridView1.DataSource = pelatesPouGiortazounDataset.Tables[0].DefaultView;

2 个答案:

答案 0 :(得分:0)

使用FIND_IN_SET函数搜索逗号分隔列表,
或者准备和绑定与字符串中的值一样多的参数。

select * from pelates where FIND_IN_SET(Onoma, @giorti)

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_find-in-set

答案 1 :(得分:0)

非常感谢你们! 通过以上添加,我的代码效果很好! 这是最终的代码:

 String giorti = label2.Text;
        String[] name = giorti.Split(',');
        String finalName = label2.Text;
        finalName = finalName.Replace(" ", "");
        textBox1.Text = finalName;

        showPelatesPouGiortazounCommand = login.connection.CreateCommand();
        showPelatesPouGiortazounCommand.CommandText = "select * from pelates where FIND_IN_SET(Onoma, @giorti)";
        showPelatesPouGiortazounCommand.Parameters.AddWithValue("@giorti", System.Text.Encoding.UTF8.GetBytes(finalName));
        getPelatesPouGiortazounAdapter = new MySqlDataAdapter(showPelatesPouGiortazounCommand);
        pelatesPouGiortazounDataset = new DataSet();
        getPelatesPouGiortazounAdapter.Fill(pelatesPouGiortazounDataset);
        dataGridView1.DataSource = pelatesPouGiortazounDataset.Tables[0].DefaultView;