Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从sqldatabase表c获取公共id号_C#_Mysql_Visual Studio 2013 - Fatal编程技术网

C# 从sqldatabase表c获取公共id号

C# 从sqldatabase表c获取公共id号,c#,mysql,visual-studio-2013,C#,Mysql,Visual Studio 2013,我需要知道如何获取所有2的clientID,这样我就可以获取分配给它们的程序…到目前为止,我的代码只获取第一个包含2的clientID int progs; string Command = @"select * from clientprogram where clientProgClientID = @clientID;"; using (MySqlConnection mConnection = new MySqlConnection(mycon)) { mConnection.

我需要知道如何获取所有2的clientID,这样我就可以获取分配给它们的程序…到目前为止,我的代码只获取第一个包含2的clientID

int progs;
string Command = @"select * from clientprogram where clientProgClientID = @clientID;";
using (MySqlConnection mConnection = new MySqlConnection(mycon))
{
     mConnection.Open();
     using (MySqlCommand cmd2 = new MySqlCommand(Command, mConnection))
     {
          cmd2.Parameters.Add(new MySqlParameter("@clientID", lblcID.Text));
          using (MySqlDataReader reader = cmd2.ExecuteReader())
          {
                if (reader.Read())
                {
                     progs = (int)reader["clientProgramID"];
                     cmbProgram.Items.Add(progs);
                }
          }
     }
     mConnection.Close();
}

当然,它只能得到第一个结果,因为你只阅读了一次。 使用whilereader.Read而不是ifreader.Read


当然,它只能得到第一个结果,因为你只阅读了一次。 使用whilereader.Read而不是ifreader.Read

改变

if (reader.Read())

旁注;当您只需要单列数据时,不要使用select*,请从您的表中使用select Coumnname

if (reader.Read())

旁注;当您只需要单列数据时,不要使用select*,请从您的表中使用select Coumnname

if (reader.Read())

似乎递归地添加记录应该是一个循环,而不仅仅是第一次更改

if (reader.Read())
int progs;
string Command = @"select * from clientprogram where clientProgClientID =     @clientID;";
using (MySqlConnection mConnection = new MySqlConnection(mycon))
{
 mConnection.Open();
 using (MySqlCommand cmd2 = new MySqlCommand(Command, mConnection))
  {
      cmd2.Parameters.Add(new MySqlParameter("@clientID", lblcID.Text));
      using (MySqlDataReader reader = cmd2.ExecuteReader())
      {
            while (reader.Read()) // CHANGE TO THIS
            {
                 progs = (int)reader["clientProgramID"];
                 cmbProgram.Items.Add(progs);
            }
      }
 }
 mConnection.Close();

似乎应该在循环中递归地添加记录,而不仅仅是第一个记录

int progs;
string Command = @"select * from clientprogram where clientProgClientID =     @clientID;";
using (MySqlConnection mConnection = new MySqlConnection(mycon))
{
 mConnection.Open();
 using (MySqlCommand cmd2 = new MySqlCommand(Command, mConnection))
  {
      cmd2.Parameters.Add(new MySqlParameter("@clientID", lblcID.Text));
      using (MySqlDataReader reader = cmd2.ExecuteReader())
      {
            while (reader.Read()) // CHANGE TO THIS
            {
                 progs = (int)reader["clientProgramID"];
                 cmbProgram.Items.Add(progs);
            }
      }
 }
 mConnection.Close();
}

}

if reader.Read应该是循环吗?if reader.Read应该是循环吗?