猿问

SQL 查询得到错误“指定的强制转换无效”。

我有如下示例代码:-


public List<Announcement_User> announcementUser([FromBody]MyAnnouncementUser value)

{

    MySqlConnection conn = WebApiConfig.conn();

    MySqlCommand query = conn.CreateCommand();

    query.CommandText = "select a.title,a.description,a.date_created,ua.read,ua.announcement_id,ua.user_announcement_id from announcement a left join user_announcement ua on a.announcement_id = ua.announcement_id where ua.user_id = @user_id";


    query.Parameters.AddWithValue("@user_id", value.user_id);


    var prodWishlist = new List<Announcement_User>();


    try

    {

        conn.Open();

    }

    catch (MySql.Data.MySqlClient.MySqlException ex)

    {

        prodWishlist.Add(new Announcement_User(null, null,null, false, 0, 0, ex.ToString()));

    }


    MySqlDataReader fetch_query = query.ExecuteReader();


    while (fetch_query.Read())

    {

        prodWishlist.Add(new Announcement_User(fetch_query["title"].ToString(), fetch_query["description"].ToString(), fetch_query["date_created"].ToString(), (bool)fetch_query["read"], fetch_query.GetInt32(4), fetch_query.GetInt32(5), null));

    }

    conn.Close();

    return prodWishlist;

}

我遇到了如下错误:-


"Message": "发生错误。",


"ExceptionMessage": "指定的演员表无效。",


"ExceptionType": "System.InvalidCastException",


现在怀疑是bool导致的错误。我可以知道如何为 bool in(fetch_query.Read()) 编写正确的方法吗?请帮忙。谢谢你。


皈依舞
浏览 230回答 2
2回答

qq_笑_17

尝试使用以下GetBoolean方法:fetch_query.GetBoolean("read")
随时随地看视频慕课网APP
我要回答