从数据库填充列表(SQLite)

时间:2015-07-23 16:43:20

标签: c#

我正在尝试用数据库SQlite填充两个带有信息的列表。

public class Investor // List
{
    public int iID { get; set; }
    public string iName { get; set; }
    public string iDisplayName { get; set; }
    public string iArea { get; set; }
}

public class Area // List
{
    public int aID { get; set; }
    public string aName { get; set; }
    public string aDisplayName { get; set; }
}

public class training : MonoBehaviour {

public List<Investor> Investor()
{
    var listOfInvestor = new List<Investor>();

    string conn = "URI=file:" + Application.dataPath + "/db_01.s3db";
    IDbConnection dbconn;
    dbconn = (IDbConnection) new SqliteConnection(conn);
    dbconn.Open(); 
    IDbCommand dbcmd = dbconn.CreateCommand();

    string sqlQuery = "SELECT * "+" FROM investor; SELECT * "+" FROM Area;";
    dbcmd.CommandText = sqlQuery;
    IDataReader reader = dbcmd.ExecuteReader();



    while(reader.Read())
    {
        var investor = new Investor();

        investor.iID = Convert.ToInt32(reader["i_id"]);
        investor.iName = reader["i_name"].ToString();
        investor.iDisplayName = reader["i_display_name"].ToString();
        investor.iArea = reader["i_area"].ToString();

        listOfInvestor.Add(investor);
    }

    reader.Close();
    reader = null;
    dbcmd.Dispose();
    dbcmd = null;
    dbconn.Close();
    dbconn = null;

    return listOfInvestor;

}

我可以获得第二个填充,但这意味着打开两倍的数据库并调用另一个公共列表。

public List<Area> Area(){}

这是唯一的方法吗?

或许有办法做Public List<string>[] LoadData() ?{}

之类的事情

2 个答案:

答案 0 :(得分:1)

你可以在做完之后简单地进入第二个结果。我还建议使用使用语句。

var listOfInvestor = new List<Investor>();

            string conn = "URI=file:" + Application.dataPath + "/db_01.s3db";
            using (var dbConnection = (IDbConnection)new SqlLiteConnection(conn))
            using (var dbcmd = dbConnection.CreateCommand())
            {
                dbConnection.Open();

                string sqlQuery = "SELECT * " + " FROM investor; SELECT * " + " FROM Area;";
                dbcmd.CommandText = sqlQuery;
                IDataReader reader = dbcmd.ExecuteReader();

                //Read first result set
                while (reader.Read())
                {
                    var investor = new Investor();

                    investor.iID = Convert.ToInt32(reader["i_id"]);
                    investor.iName = reader["i_name"].ToString();
                    investor.iDisplayName = reader["i_display_name"].ToString();
                    investor.iArea = reader["i_area"].ToString();

                    listOfInvestor.Add(investor);
                }


                //Repeat for your other result set
                reader.NextResult();
                while (reader.Read())
                {
                    //Do areas stuff here
                }
            }

答案 1 :(得分:0)

如果其他人遇到问题。

以下是我为解决问题所做的工作。

public class Investor // List
{
public int iID { get; set; }
public string iName { get; set; }
public string iDisplayName { get; set; }
public string iArea { get; set; }
}

public class Area // List
{
public int aID { get; set; }
public string aName { get; set; }
public string aDisplayName { get; set; }
}

public class Data
{
public List<Investor> Investor { get; set; }
public List<Area> Area { get; set; }
}

然后

public Data LoadData()
{
    var listOfInvestor = new List<Investor>();
    var listOfArea = new List<Area>();

    string conn = "URI=file:" + Application.dataPath + "/db_01.s3db";

    var dbconn  = new SqliteConnection(conn);
    var dbcmd = dbconn.CreateCommand();
    using (dbconn) 
    using (dbcmd)

    {
        dbconn.Open(); 

        string sqlQuery = "SELECT * "+" FROM investor; SELECT * "+" FROM area;";
        dbcmd.CommandText = sqlQuery;
        IDataReader reader = dbcmd.ExecuteReader();



        while(reader.Read())
        {
            var investor = new Investor();

            investor.iID = Convert.ToInt32(reader["i_id"]);
            investor.iName = reader["i_name"].ToString();
            investor.iDisplayName = reader["i_display_name"].ToString();
            investor.iArea = reader["i_area"].ToString();

            listOfInvestor.Add(investor);


        }
        reader.NextResult();
        while(reader.Read())
        {
            var area = new Area();

            area.aID = Convert.ToInt32(reader["a_id"]);
            area.aName = reader["a_name"].ToString();
            area.aDisplayName = reader["a_display_name"].ToString();

            listOfArea.Add(area);
        }

        reader.Close();
        reader = null;
        dbcmd.Dispose();
        dbcmd = null;
        dbconn.Close();
        dbconn = null;

        return new Data() {Investor = listOfInvestor, Area = listOfArea };
    }     
}

我真的不知道手动关闭连接是否更好,所以我让“关闭部分”。

感谢大家的帮助;)