我正在 Unity 中使用 SQLite 来尝试显示数据库中的一些信息。
但是我遇到了一些问题,我不知道缺少什么才能使其正常工作。
public class City // List
{
public int vID { get; set; }
public string vName{ get; set; }
}
还有
public class training : MonoBehaviour {
public List<City> LoadCity()
{
var listOfCity = new List<City>();
string conn = "URI=file:" + Application.dataPath + "/DataBase/db_01.s3db";
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open();
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT r_id,r_name "+" FROM region ";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while(reader.Read())
{
var city = new City();
city.vID = Convert.ToInt32(reader["r_id"]);
city.vName = reader["r_name"].ToString();
listOfCity.Add(city);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
return listOfCity;
}
void OnGUI()
{
for (int ai = 0; ai < LoadCity().Count; ai++)
{
GUI.Button(new Rect (25, 50+(ai+1)*35, 210, 30),(City.vName[ai]));
Debug.Log(City.vName[ai]);
}
}
}
为什么在 void GUI 中我无法调用 City.vName?
感谢您的帮助。
最佳答案
试试这个:
void OnGUI()
{
int ai = 0;
foreach(var city in LoadCity())
{
GUI.Button(new Rect (25, 50+(ai+1)*35, 210, 30),(city.vName));
Debug.Log(city.vName);
ai++;
}
}
如果有任何问题请告诉我。
关于c# - 从 SQL 获取数据并将其放入列表/访问另一个没有静态的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31558354/