C# SQL查询返回列表

C# SQL查询返回列表,c#,mysql,sql,winforms,visual-studio,C#,Mysql,Sql,Winforms,Visual Studio,我似乎无法让它工作: 我的表格列标题为“流派”“艺术家”“专辑” 我要传递的参数是type,filter,value,artist,genre,Rock,其中db中有两行,其中Rock代表流派 当我跟随调试器时,“while reader.Read”必须返回false,因为循环从未被输入,因此没有写入列表 public static List<String> getWithFilter(String type, String filter, String value)

我似乎无法让它工作:

我的表格列标题为“流派”“艺术家”“专辑” 我要传递的参数是type,filter,value,artist,genre,Rock,其中db中有两行,其中Rock代表流派

当我跟随调试器时,“while reader.Read”必须返回false,因为循环从未被输入,因此没有写入列表

    public static List<String> getWithFilter(String type, String filter, String value)
    {

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

        string query = "SELECT @type FROM Music WHERE" +
            " @filter = '@value'";
        SqlConnection connection = Database.GetConnection();
        SqlCommand getData = new SqlCommand(query, connection);
        getData.Parameters.AddWithValue("@type", type);
        getData.Parameters.AddWithValue("@filter", filter);
        getData.Parameters.AddWithValue("@value", value);
        connection.Open();

        using (connection)
        {
            using (getData)
            {
                using (SqlDataReader reader = getData.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        columnData.Add(reader.GetString(0));
                    }
                }
            }
        }
        return columnData;
    }

不能对列的名称使用参数,并且在使用参数时不能在其周围加引号。现在,您的查询相当于

SELECT 'artist' FROM Music WHERE 'genre' = '@value'
您可以改为执行以下操作

string query = "SELECT " + type + " FROM Music WHERE " + filter + " = @value";
只需删除创建@type和@fitler参数的行。

您要查找的格式或字符串插值需要C 6.0:

string query = 
   $@"SELECT {type} 
        FROM Music 
       WHERE {filter} = @value";

... 

getData.Parameters.AddWithValue("@value", value);
格式有点冗长:

 string query = String.Format(
    @"SELECT {0} 
        FROM Music 
       WHERE {1} = @value", type, filter);

我假设您正在使用.NET2

DateTime current = DateTime.Now;
       Console.WriteLine(current);
        SqlConnection conn = new SqlConnection();
        string q = "SELECT @field FROM student";
        SqlDataAdapter da = new SqlDataAdapter(q, conn);
        da.SelectCommand.Parameters.AddWithValue("@field", "snName");
        DataTable dt = new System.Data.DataTable();
        conn.Open();
        da.Fill(dt);
        conn.Close();
        List<string> names = new List<string>();
        foreach (DataRow dr in dt.Rows)
        {
            names.Add(dr[0].ToString());
        }
        Console.WriteLine("Fetching {0} data for {1}", names.Count, DateTime.Now - current);
        Console.ReadKey();

您可以使用lambda表达式来映射.net>4中的datatable

您不能将参数用于列的名称,并且在使用它们时不在其周围加引号。您必须实际构建查询,如SELECT+type+FROM。。。其中+filter+=@值;使用SQL参数很好,但type和filter不能是参数,因为据我所知,它们代表列名。确定吗?如果可以这样使用参数,我不确定选择是否像从“流派”=摇滚的音乐中选择“艺术家”。@CraigHalloway否,您编写它的方式执行的查询是从“流派”=摇滚的音乐中选择“艺术家”。请注意,围绕艺术家和流派的单引号将它们转换为字符串文字。当然,WHERE条件永远不会满足。@IvanStoev事实上我相信这是'genre'='@value',因为它们在最后一个参数周围有多余的引号。这不容易受到SQL注入攻击吗?@maccettura只有在类型和或筛选器是用户输入的情况下。如果是,OP应该首先对照有效列名列表检查它们。