如何从SQL Server表中转换TinyInt值?

问题描述

我有此SQL Server(Express)表:

enter image description here

...具有TinyInt的GenreId(最多只有几十种不同的流派)。

此C#代码失败,显示“指定的转换无效”。

int genreId = 0;
. . .
genreId = GetGenreIdForGenre(_genre);

故障点的“ _genre”值为“冒险”。对GetGenreIdForGenre()的调用应返回“ 1”:

enter image description here

这是在GetGenreIdForGenre()中失败的行:

return (int)command.ExecuteScalar();

在上下文中,GetGenreIdForGenre()方法为:

private int GetGenreIdForGenre(string genre)
{
    try
    {
        string qry = "SELECT GenreId FROM dbo.GENRES WHERE Genre = @genre";
        using (SqlConnection connection = new SqlConnection(_connectionString))
        {
            using (SqlCommand command = new SqlCommand(qry,connection))
            {
                command.Parameters.AddWithValue("@genre",genre);
                connection.Open();
                return (int)command.ExecuteScalar();
            }
        }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         return 0;
     }
 }

没有(TinyInt)转换可用。 Int32也失败。我需要怎么做才能检索TinyInt值?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)