Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从SQL查询生成列表?_C#_Sql_List_Visual Studio 2005_Dbcommand - Fatal编程技术网

C# 如何从SQL查询生成列表?

C# 如何从SQL查询生成列表?,c#,sql,list,visual-studio-2005,dbcommand,C#,Sql,List,Visual Studio 2005,Dbcommand,如果我定义了一个DbCommand来执行如下操作: SELECT Column1 FROM Table1 生成返回记录列表的最佳方法是什么 没有Linq等,因为我正在使用VS2005。我想这就是您要找的 List<String> columnData = new List<String>(); using(SqlConnection connection = new SqlConnection("conn_string")) { connection.Open

如果我定义了一个DbCommand来执行如下操作:

SELECT Column1 FROM Table1
生成返回记录列表的最佳方法是什么


没有Linq等,因为我正在使用VS2005。

我想这就是您要找的

List<String> columnData = new List<String>();

using(SqlConnection connection = new SqlConnection("conn_string"))
{
    connection.Open();
    string query = "SELECT Column1 FROM Table1";
    using(SqlCommand command = new SqlCommand(query, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                columnData.Add(reader.GetString(0));
            }         
        }
    }
}

未测试,但这应该可以正常工作。

循环浏览项目并添加到集合中。你可以使用这个方法


如果要查询所有列

List<Users> list_users = new List<Users>();
MySqlConnection cn = new MySqlConnection("connection");
MySqlCommand cm = new MySqlCommand("select * from users",cn);
try
{
    cn.Open();
    MySqlDataReader dr = cm.ExecuteReader();
    while (dr.Read())
    {
        list_users.Add(new Users(dr));
    }
}
catch { /* error */ }
finally { cn.Close(); }

用户的构造函数将执行所有dr.GetStringi

,其中返回的数据是字符串;您可以强制转换为其他数据类型:

(from DataRow row in dataTable.Rows select row["columnName"].ToString()).ToList();

或者嵌套列表好的,OP是针对单个列的,这是针对多个列的

        //Base list is a list of fields, ie a data record
        //Enclosing list is then a list of those records, ie the Result set
        List<List<String>> ResultSet = new List<List<String>>();

        using (SqlConnection connection =
            new SqlConnection(connectionString))
        {
            // Create the Command and Parameter objects.
            SqlCommand command = new SqlCommand(qString, connection);

            // Create and execute the DataReader..
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                var rec = new List<string>();
                for (int i = 0; i <= reader.FieldCount-1; i++) //The mathematical formula for reading the next fields must be <=
                {                      
                    rec.Add(reader.GetString(i));
                }
                ResultSet.Add(rec);

            }
        }

因此,如果不循环浏览记录,就没有办法做到这一点?没有。如果使用DataReader,则需要循环浏览项目。如果您使用一个DataTable,您可以在那里获取它,并使用一些LINQ您可以将其作为字符串列表获取。您如何在不使用LINQ的情况下从DataTable获取列表?如果我写错了,请原谅。如果不想循环,则需要LINQ。在内部,LINQ扩展方法也做同样的事情,我猜好了,VB6 ADO有一个从记录集中获取数组的函数。似乎很奇怪.NET的功能会降低。只是一个提示。无需显式关闭连接,因为您使用的是using BLOCK。同样的注释适用于SqlDataReaderI,我修改了我的答案以反映这一点。谢谢我在这个问题上说不。
        //Base list is a list of fields, ie a data record
        //Enclosing list is then a list of those records, ie the Result set
        List<List<String>> ResultSet = new List<List<String>>();

        using (SqlConnection connection =
            new SqlConnection(connectionString))
        {
            // Create the Command and Parameter objects.
            SqlCommand command = new SqlCommand(qString, connection);

            // Create and execute the DataReader..
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                var rec = new List<string>();
                for (int i = 0; i <= reader.FieldCount-1; i++) //The mathematical formula for reading the next fields must be <=
                {                      
                    rec.Add(reader.GetString(i));
                }
                ResultSet.Add(rec);

            }
        }