C# 使用c windows窗体循环mssql语句时出错

C# 使用c windows窗体循环mssql语句时出错,c#,sql-server,loops,C#,Sql Server,Loops,在决定更新或插入之前,我试图检查数据库是否有以下数据,但当前我遇到了一个无法循环的问题,因为它只执行一次 //It is suppose to loop based on the value in account.devices.Count --> last tried with 10 values for (int z = 0; z < account.devices.Count; ) have 10 data inside account.device.Count {

在决定更新或插入之前,我试图检查数据库是否有以下数据,但当前我遇到了一个无法循环的问题,因为它只执行一次

//It is suppose to loop based on the value in account.devices.Count --> last tried with 10 values 
for (int z = 0; z < account.devices.Count; ) have 10 data inside account.device.Count
{
    SqlCommand checkForDevice = new SqlCommand("select * from status where device = '" + account.devices[z].deviceid + "' and Dates = '" + DateTime.Now.ToString("yyyy-MM-dd") + "'", myConnection);
    SqlDataReader myReader = checkForDevice.ExecuteReader();

    if (myReader.Read())
    {
        String sqlStatus = myReader["status"].ToString().Trim();
        myReader.Close();
        if (sqlStatus.Equals(account.devices[z].state.ToString()))
        {
        }
        else
        {
        }
    }
    else
    {
        //SqlCommand myCommand = new SqlCommand("INSERT INTO records(device,Dates,Time,status ) Values (" + account.devices[i].deviceid + ","+DateTime.Now.ToString("yyyy-MM-dd") + "','" + DateTime.Now.ToString("HH:mm:ss") + "','" +account.devices[i].state+")", myConnection);
        Console.Write("CALLED");
    }
    Console.Write("LOOP");
    z++;
}
提前谢谢

更新工作代码
account.devices.count表示什么?您是否检查过account.devices.count中有多少个值?代码的哪部分仅执行1?for循环?或者你期望myReader得到多个结果?@VishalSuthar我用debug检查它里面有10个项目假设循环10个times@ah_hau感谢您提供的提示解决此问题:
try
                    {
                        for (int z = 0; z < account.devices.Count; )
                        {
                            SqlCommand checkForDevice = new SqlCommand("select * from status where device = '" + account.devices[z].deviceid + "' and Dates = '" + DateTime.Now.ToString("yyyy-MM-dd") + "'", myConnection);
                            SqlDataReader myReader = checkForDevice.ExecuteReader();

                            if (myReader.Read())
                            {
                                String sqlStatus = myReader["status"].ToString().Trim();
                                if (sqlStatus.Equals(account.devices[z].state.ToString()))
                                {

                                }
                                else
                                {

                                }
                            }
                            else
                            {
                                //SqlCommand myCommand = new SqlCommand("INSERT INTO records(device,Dates,Time,status ) Values (" + account.devices[i].deviceid + ","+DateTime.Now.ToString("yyyy-MM-dd") + "','" + DateTime.Now.ToString("HH:mm:ss") + "','" +account.devices[i].state+")", myConnection);
                                Console.Write("CALLED");

                            }
                            Console.Write("LOOP");

                            myReader.Close();
                            z++;
                        }
                    }
                    catch(Exception e)
                    {
                        this.listBox1.BeginInvoke((MethodInvoker)delegate()
                       {
                           listBox1.Items.Add(DateTime.Now.ToString("hh:mm:ss tt") + " [DB Insert] " + e.ToString());
                           this.listBox1.SelectedIndex = listBox1.Items.Count - 1;
                           this.listBox1.SelectedIndex = -1;
                       });

                    }