我似乎无法得到这个工作:SQL查询返回列表
我的表列标题是“流派”“艺术家”“专辑” ,我传递的(类型,过滤器的参数,可以价值)(“艺术家”,“风格”,“摇滚”),其中有两行的数据库与“摇滚”的流派。
当我按照调试器,写入到列表中的“而(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;
}
您的查询是有点乱的 - 你不能使用参数,你使用它们的方式。你能解释你想要查询的内容吗?还请显示您的表格结构,您传递的参数以及所需的输出。 – Siyual
您不能使用参数列名称的名称,并且在使用它们时不要将引号放在它们周围。您必须实际构建查询,比如'“SELECT”+ type +“FROM ... WHERE”+ filter +“= @value”;' – juharr
使用SQL参数很好,但'type'和'filter '不能是参数,因为它们表示列名,据我所知。 –