gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:11:32 25 4
gpt4 key购买 nike

关于我需要什么和我目前拥有什么的简短介绍

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

我需要比较我从事件( Action )中得到的一个和从我的数据库中得到的一个的 LongNumbers,如果它们相似,我就取名字并使用它。例如你好,亚历克斯(亚历克斯是从数据库中取出的)

问题

我得到索引超出范围,并且使用我的逻辑所有文本都会改变我想要实现的是将文本更改为从事件中获得与 longNumber 相同的长号的人的名字

代码:从数据库中获取数据

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<Data> userData = new List<Data>();

Data data;


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())
{


for (int i = 0; i < Reader.FieldCount; i++)
{
//rfid_tags.Add (Reader.GetString("UserName"));
//rfid_tags.Add(Reader.GetString("RFID_Tag"));
data = new Data(Reader.GetString("UserName"), Reader.GetString("RFID_Tag"));
userData.Add(data);
// ws.DomainUrl = reader.GetString("DomainUrl");
// rfid_tags.Add(Reader.GetValue(i).ToString() + ",");
// row += Reader.GetValue(i).ToString() + ", ";
}

Debug.Log(row);
}
}


catch (Exception x)
{
Debug.Log(x.Message);
return x.Message;
}

connection.Close();
return row;

}

}

public class Data {

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

public Data(string _name, string _rfid)
{
username = _name;
rfid = _rfid;
}


public void SetUserName(string _userName) { username = _userName; }
public void SetRFID(string _rfid) { rfid = _rfid; }


}

比较事件( Action )的值并显示文本的代码

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);
// Debug.Log(MySqlTestScript.sharedInstance().rfid_tags[0]);

Debug.Log("STEP ONE");
for (int i = 0; i < MySqlTestScript.sharedInstance().userData.Count; i++)
{
Debug.Log("STEP TWO");
if (tag.Epc.ToString().Trim() == MySqlTestScript.sharedInstance().userData[i].rfid)
{
Debug.Log("STEP THREE");
// TODO References the Name
Loom.QueueOnMainThread(() => {

namesTxt[i].text = MySqlTestScript.sharedInstance().userData[i].username;

});

}
}

正如您在事件脚本中看到的那样,它非常不灵活,如果我的数据库少于 6 行,它会给出超出范围的索引。我需要让它更友好和通用

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

最佳答案

这应该更有意义:

数据库:

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
});
}
}
}
}

关于c# - 少于 6 行数据的索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51156375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com