gpt4 book ai didi

c# - 你能解释一下这个错误吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:13 24 4
gpt4 key购买 nike

Cannot implicitly convert type 'System.Collections.Generic.List<string>'
to 'System.Collections.Generic.List<Parts>'

下面是产生这个错误的代码。我很确定我在做一些愚蠢的事情。

public class Parts
{


public string computername { get; set; }


public List<Parts> GetParts()
{
List<string> lst = new List<string>();
//The object that will physically connect to the database
using(SqlConnection cnx = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["request"].ConnectionString))
{
//The SQL you want to execute
SqlCommand cmd = new SqlCommand("SELECT * FROM REQUESTS", cnx);
//Open the connection to the database
cnx.Open();
//execute your command
SqlDataReader dataReader = cmd.ExecuteReader();
{
//Loop through your results
while(dataReader.Read())
{
lst.Add(Convert.ToString(dataReader["titlename"]));
}
}
}
return lst;
}

}

错误一直指向这一行:

   return lst; 

谢谢

最佳答案

您返回的是 List<string>当方法返回类型为 List<Part> 时.

关于c# - 你能解释一下这个错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11585941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com