在c#上读取两个MySql查询

时间:2014-09-10 09:33:09

标签: c# mysql sql

SELECT @gb FROM parameters WHERE Form_Factor = @ ff我是c#的新手,这是我的第一个程序。 我正在尝试连接mysql服务器并运行查询。 然后,我想获取第一个查询的结果并使用它们运行另一个查询。 第一个查询工作正常,第二个mysqldataread不工作,我不明白为什么。 (我正在使用vs 2013)。

代码:

myCommand.CommandText = "SELECT GB,Form_Factor FROM devices WHERE Serial_Number = @Sn";
myCommand.Parameters.AddWithValue("@Sn", Sn);

connect.Open();

MySqlDataReader reader = myCommand.ExecuteReader();

if (reader.Read())
{
    textBox2.Text = reader["GB"].ToString();
    textBox3.Text = reader["Form_Factor"].ToString();
}
reader.Close();
connect.Close();


string gb = textBox2.Text;
string ff = textBox3.Text;
MySqlCommand myCommand2 = connect.CreateCommand();
myCommand2.Parameters.AddWithValue("@gb", gb);
myCommand2.Parameters.AddWithValue("@ff", ff);
myCommand2.CommandText = "SELECT @gb FROM parameters WHERE Form_Factor =@ff ";

connect.Open();

MySqlDataReader reader2 = myCommand2.ExecuteReader();

reader2.Read();

if (reader2.Read())
{
    string command = reader2["@gb"].ToString();
    MessageBox.Show(command);
}

if (reader2.Read() == false)
{
    MessageBox.Show("read failed!");
}

reader2.Close();
connect.Close();

3 个答案:

答案 0 :(得分:0)

您需要阅读更多herehere

但是,我编辑你的代码:

    myCommand.CommandText = "SELECT GB,Form_Factor FROM devices WHERE Serial_Number = @Sn";
myCommand.Parameters.AddWithValue("@Sn", Sn);

connect.Open();

MySqlDataReader reader = myCommand.ExecuteReader();

if (reader.Read())
{
    textBox2.Text = reader["GB"].ToString();
    textBox3.Text = reader["Form_Factor"].ToString();
}
reader.Close();
connect.Close();

string gb = textBox2.Text;
string ff = textBox3.Text;


myCOmmand.CommandText = @"SELECT * FROM parameters WHERE Form_Factor = 'Mercury';";

connect.Open();

MySqlDataReader reader2 = myCommand.ExecuteReader();

reader2.Read();

if (reader2.Read())
{
    string command = reader2["columnName"].ToString();
    MessageBox.Show(command);
}

if (reader2.Read() == false)
{
    MessageBox.Show("read failed!");
}

reader2.Close();
connect.Close();

确保编辑reader2 [“columnName”]。TOString(); - >将columnName编辑为要获取的columnName。

答案 1 :(得分:0)

1 - 你不需要这一行:

reader2.Read();

第二 - reader2["@gb"].ToString();错误地将您需要的ColumnName设置为"@gb"

3rd - AddWithValue在今年秋天没有任何意义,只有在sql-query中有动态变量时才需要它

编辑 - 第二部分:

string gb = textBox2.Text;
string ff = textBox3.Text;
MySqlCommand myCommand2 = connect.CreateCommand();
myCommand2.CommandText = "SELECT " + gb + " FROM parameters WHERE Form_Factor = @ff;";
myCommand2.Parameters.AddWithValue("@ff", ff);    
connect.Open();
MySqlDataReader reader2 = myCommand2.ExecuteReader();

if (reader2.Read())
{
  string command = reader2[gb].ToString();
  MessageBox.Show(command);
}
else
{
   MessageBox.Show("read failed!");
}

reader2.Close();
connect.Close();

答案 2 :(得分:0)

我可以猜测@gb不是参数表中的现有字段,这就是为什么你不能在第二个查询中得到任何结果。在使用sqlcommands时,使用语句也是一种很好的做法:

myCommand2.CommandText = "SELECT @gb FROM parameters WHERE Form_Factor =@ff ";
myCommand2.Parameters.AddWithValue("@gb", gb);
myCommand2.Parameters.AddWithValue("@ff", ff);
     using (connect)
           {
             connect.Open();
             MySqlDataReader reader2 = myCommand2.ExecuteReader();

                while (reader2.Read())
                {
                    string command = reader2[0].ToString();
                   MessageBox.Show(command);
                }
           }