在列表中创建一个列表,其中数据已重命名为MYSQL

时间:2017-02-22 12:51:47

标签: c# list generics

我想在列表中创建一个列表,其中的数据被重新导入MYSQL ... 我很接近,但似乎工作不正常......

当我MessageBox.Show(list_line[0][3].ToString());时 它给了我System.IndexOutOfRangeException但从技术上讲,我想要和想到的是:

list_DB[  [list_line1] , [List_line2] , [list_line3] , ... ]

和list_line看起来像[1, 22/02/2017 13:48:01, 50.0000, 004.0000]

这是我的功能......

    private List<string> list_line = new List<string>();
    private List<List<string>> list_DB = new List<List<string>>();
    private void BoatGPS_INTO_LIST()
    {
        SqlDataReader reader;
        string query = "select boat_id, boatGPS_DateTime, boatGPS_lat, boatGPS_lon from BoatGPS";

        using (SqlConnection sql = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"" + Environment.CurrentDirectory + "\\DB\\STS.mdf\"; Integrated Security = True; Connect Timeout = 30"))
        {
            sql.Open();
            using (reader = new SqlCommand(query, sql).ExecuteReader())
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Object[] line = new Object[reader.FieldCount];
                        reader.GetValues(line);                            
                        foreach (var item in line)
                        {
                            list_line.Add(item.ToString());
                        }
                        list_DB.Add(list_line);
                    }
                }
            }
            MessageBox.Show(list_line[0][3].ToString());
            sql.Close();
        }
    }

3 个答案:

答案 0 :(得分:0)

list_DB的类型为List<List<string>>。尝试:

MessageBox.Show(list_DB[0][3].ToString());

修改 关于如何填写列表list_DB,请尝试:

while (reader.Read())
{   
   List<string> list_line = new List<string>();                          
   list_line.Add(reader[0].ToString());
   list_line.Add(reader[1].ToString());
   list_line.Add(reader[2].ToString());
   list_line.Add(reader[3].ToString());

   list_DB.Add(list_line);
}

答案 1 :(得分:0)

此代码

MessageBox.Show(list_line[0][3].ToString());

尝试访问list_line对象(List<string>)中的第一项(字符串),然后在该字符串中使用索引3访问character,如果string短于4个字符,则会抛出异常。< / p>

答案 2 :(得分:0)

你使事情变得复杂。不需要这种结构。
有一个特定的类来表示内存中SELECT查询的结果,它是DataTable类。

private DataTable dt = new DataTable();
private void BoatGPS_INTO_LIST()
{
    SqlDataReader reader;
    string query = "select boat_id, boatGPS_DateTime, boatGPS_lat, boatGPS_lon from BoatGPS";

    using (SqlConnection sql = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"" + Environment.CurrentDirectory + "\\DB\\STS.mdf\"; Integrated Security = True; Connect Timeout = 30"))
    {
        sql.Open();
        using (reader = new SqlCommand(query, sql).ExecuteReader())
            dt.Load(reader);

        if(dt.Rows.Count > 0)
            // Here the syntax means: First row, fourth column
            MessageBox.Show(dt.Rows[0][3].ToString());
        else
            MessageBox.Show("Table is empty");


    }
}