少于6行数据的索引超出范围

时间:2018-07-03 13:52:38

标签: c# mysql .net

我所需要的和我目前所拥有的的简短摘要

我连接到数据库并从数据库中获取数据,然后我得到了(Name,LongNumber),基本上我有一个事件(操作),该事件在事件发生时触发(该操作也给我一个LongNumber)。

我需要比较从事件(操作)获得的LongNumber和从数据库获得的LongNumber,如果它们相似,我将使用该名称并使用它。 例如Hello,Alex(alex是从数据库中提取的)

问题

我将Index超出了范围,并且使用我的逻辑,所有文本都会更改我想要达到的目的,就是将文本更改为将Long数字与事件中的longNumber相同的人的名字< / p>

代码:从数据库获取数据

CheckButton

用于比较事件(操作)中的值并显示文本的代码

var transporter = nodemailer.createTransport({
  host: 'smtpout.secureserver.net',
  secure: true,
  port: 465,
  auth: {
    user: mail,
    pass: password
  }
});

var mailOptions = {
  from: mail,
  to: emailTo,
  subject: subject,
  headers: headers,
  html: html
};

正如您在事件脚本中看到的那样,它是如此灵活,并且如果我的数据库少于6行,它将给出超出范围的索引。我需要使其更加友好和通用

任何帮助将不胜感激,我希望我的问题很清楚谢谢:)!

2 个答案:

答案 0 :(得分:1)

解决此问题的一种方法是将每个案例包装在另一个if语句中,以检查数据库是否有那么多行

if (MySqlTestScript.sharedInstance().longNumbers.Length > 1) {
    if (tag.Epc.ToString().Trim() == MySqlTestScript.sharedInstance().longNumbers[0].ToString()) {

        if(MySqlTestScript.sharedInstance().userNames[0] != null)
           txt1.text = "Hello "+ MySqlTestScript.sharedInstance().userNames[0];
        }
    }
}
else {
    txt1.text = "";
}

这将避免索引超出范围异常。在每种包装方式下,您都需要将比较值增加为1、2、3等。

这不是最漂亮的解决方案,但可以避免出现异常。另一种选择是包装在try-catch块中。再次不是最漂亮,但可以完成任务。

答案 1 :(得分:1)

这应该更有意义:

数据库:

using UnityEngine;
using System.Collections;
using MySql.Data.MySqlClient;
using System;
using System.Linq;
using System.Collections.Generic;

public class MySqlTestScript : MonoBehaviour
{

private static MySqlTestScript _instnace;
public static MySqlTestScript sharedInstance()
{
    return _instnace;
}


string row = "";

public string host = "*****";
public string database = "*******";
public string usrename= "*******";
public string password = "******";

public List<AppUser> users = new List<AppUser>();

void Awake()
{
    _instnace = this;
}


// Use this for initialization
public void Start()
{

    GetDataFromDatabase();
}

public string GetDataFromDatabase()
{
  string myConnectionString = "Server=" + host + ";Database=" + database + ";Uid=" + usrename + ";Pwd=" + password + ";";

  MySqlConnection connection = new MySqlConnection(myConnectionString);
  MySqlCommand command = connection.CreateCommand();
  command.CommandText = "SELECT * FROM users";
  MySqlDataReader Reader;

  try
  {
    connection.Open();
    Reader = command.ExecuteReader();

    while (Reader.Read())
    {
      users.Add(new AppUser() { 
        username = Reader.GetString("UserName").Trim(),
        rfid = Reader.GetString("longNumbers ").Trim()
      });
    }
  }
  catch (Exception x)
  {
    Debug.Log(x.Message);
    return x.Message;
  }

  connection.Close();
  return row;
}
}

数据对象:

public Class AppUser 
{ 
  public string rfid { get; set; } 
  public string username { get; set; } 
}

事件/数据比较:

void OnTagsReported(ImpinjReader sender, TagReport report)
{
  Debug.Log("OnTagsReported");

  // This event handler is called asynchronously 
  // when tag reports are available.
  // Loop through each tag in the report 
  // and print the data.
  foreach (Tag tag in report)
  {
    Debug.Log(tag.Epc);
    List<AppUser> appUsers = MySqlTestScript.sharedInstance().users;
    int numUsers = appUsers.Count;
    Debug.Log(numUsers);

    for (int i = 0; i < numUsers; i++)
    {
      if (tag.Epc.ToString().Trim() == appUsers[i].rfid)
      {
        // TODO References the Name 
        Loom.QueueOnMainThread(() => {
          if (i < namesTxt.Count) namesTxt[i].Text = appUsers[i].username; //assumes textnames is a "List" of textboxes and is already populated with instances of text1, text2 text3 etc. The if is just to guard against there being more rows in the DB than textboxes
        });
      }
    }
  }
}