【问题标题】:Second result of select query in Mysql c#Mysql c#中选择查询的第二个结果
【发布时间】:2014-04-11 00:06:14
【问题描述】:

我无法从第二个查询返回值。 部分代码...

 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 ok,但是当我返回 id 时会出错。请告诉为什么,因为我需要返回第二个,第三个......查询值。 谢谢!

【问题讨论】:

    标签: c# mysql


    【解决方案1】:

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

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

    我不知道您想对您选择的国家/地区做什么,但如果它在数据表中,您可以将其绑定到下拉列表或其他东西

    【讨论】:

      【解决方案2】:

      在您的第二个中,您将返回 id 字段,并且仅从数据库中选择国家/地区字段。它没有返回一个 id 字段供您读取,因此是一个错误。

      【讨论】:

        猜你喜欢
        • 2019-09-06
        • 2013-07-16
        • 2015-03-16
        • 2013-02-02
        • 1970-01-01
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 2013-11-01
        相关资源
        最近更新 更多