【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<ValueObjectLayer.booksVAL>' to 'int'无法将类型“System.Collections.Generic.List<ValueObjectLayer.booksVAL>”隐式转换为“int”
【发布时间】:2021-12-30 12:50:56
【问题描述】:

我正在尝试制作一个 3 层 C# 库管理项目。

我正在尝试从 bookDAL (dataacceslayer) 到 booksBLL(businesslogiclayer) 中选择一本书并将其显示在 winforms 上。

我在 BLL 上收到此错误消息 error

达尔:

    public static List<booksVAL> BookSelect(string x)
    {
        List<booksVAL> sonuc = new List<booksVAL>();
        OleDbCommand cmdkitaplistele = new OleDbCommand("select * from books where id = " + Int32.Parse(x) + " ", dbConnection.conn); // 
        if (cmdkitaplistele.Connection.State != ConnectionState.Open) // bağlantı açık değise
        {
            cmdkitaplistele.Connection.Open(); // bağlantıyı aç
        }

        OleDbDataReader dr = cmdkitaplistele.ExecuteReader(); // sorgu sonuçlarını data reader ile oku
        while (dr.Read())
        {
            booksVAL book = new booksVAL();
            book.bookId = int.Parse(dr["id"].ToString());
            book.bookName = dr["bookname"].ToString();
            book.bookAuthor = dr["authorname"].ToString();
            book.bookPagecount = dr["pagecount"].ToString();
            book.bookDatepublished = dr["datepublished"].ToString();
            book.bookIsavailable = dr["isavailable"].ToString();
            book.bookCategory = dr["category"].ToString();
            sonuc.Add(book);
        }
        dr.Close();
        return sonuc;
    }

BLL:

public static int BookSelect(string x)
{
    return booksDAL.BookSelect(x);

表格:

public partial class bookupdateForm : Form
{
   booksForm f1;
    public bookupdateForm(booksForm frm1)
    {
        InitializeComponent();
        this.f1 = frm1;
        booksBLL.BookSelect(f1.selectedlabel.Text); // selectedlabel comes from another form, it works

    }
}

【问题讨论】:

  • return booksDAL.BookSelect(x) 返回 List,但您在 public static int BookSelect(string x) 中调用它并尝试返回列表,而不是 int
  • 请不要使用字符串连接创建sql查询,使用参数化语句。了解如何缓解 sql 注入攻击。
  • 我敢打赌,如果您双击该错误,它会将您带到有问题的行(您也可以查看 Output 窗格并查看相关的行号)。这可能足以让您看到错误。如果没有,您可以在您的问题中包含这些信息,让我们的工作更轻松

标签: c# winforms layered


【解决方案1】:

问题就在这里:

public static int BookSelect(string x)
{
    return booksDAL.BookSelect(x);
}

将返回类型int更改为List&lt;booksVAL&gt;

public static List<booksVAL> BookSelect(string x)
{
    return booksDAL.BookSelect(x);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-15
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多