在Mysql中选择查询的第二个结果c#

时间:2014-04-10 15:41:52

标签: c# mysql

我不能从第二个查询中返回值。 部分代码......

 MySqlConnectionStringBuilder mysqlSB = new MySqlConnectionStringBuilder();
                mysqlSB.Server = "localhost";
                mysqlSB.Database = "test";
                mysqlSB.UserID = "admin";
                mysqlSB.Password = "1111";


                MySqlConnection con = new MySqlConnection();
                con.ConnectionString = mysqlSB.ConnectionString;

                MySqlCommand Select = new MySqlCommand("select name from table_1 where id='1' ", con);      
                MySqlDataReader myReader;
                con.Open();
                myReader = Select.ExecuteReader();

                while (myReader.Read())
            {
                count++;
            }
            string name = myReader["name"].ToString();

  if (count == 1)
                {

                    MySqlCommand Select2 = new MySqlCommand("select country from table_2 where name='"+name+"'", con);
                     MySqlDataReader myReader2;
                    myReader2 = Select2.ExecuteReader();
                    while (myReader2.Read())
                    {
                        count++;
                    }

                   return myReader2["id"].ToString();
                }

如果我删除第二部分,之后if(count == 1)并返回name = all,但是当我返回id时会出错。 Plase告诉为什么,因为我需要返回查询的第二,第三......值。 谢谢!

2 个答案:

答案 0 :(得分:0)

如果我是你,我会用你的查询结果填充数据表

见这里 Connecting to a Mysql DB with C# - Need some with Datasets

我不知道您要对所选国家/地区做什么,但如果它在数据表中,您可以将其绑定到下拉菜单

答案 1 :(得分:0)

在您的第二个中,您将返回id字段,并且仅从数据库中选择country字段。它不会返回一个id字段供您从中读取错误。

相关问题