c# - 从 SQL 获取数据并将其放入列表/访问另一个没有静态的类?

标签 c# sql list

我正在 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/

相关文章:

c# - ModelState 始终被认为是有效的,无论必填字段中的空值如何

c# - 服务器和浏览器 ASP.NET MVC 之间的 CultureInfo 冲突

sql - 如何获取每个外键的最小日期记录的记录 ID?

c# - LinQ Sum Distinct Values Quantity 和 Total

r - 更改数据框列表中的特定列 - R

c# - Clickonce + .NET client profile 4 framework + offline

c# - 工具窗口的永久焦点

mysql - 如何结合多个 COUNT() 和 GROUP BY 实现 MySQL Query

c++ - STL List 复制一个结构,但复制的值偏移了两个内存地址

python - 遍历时从字符串列表中删除元素