SqlDataReader 显示 InvalidCastException(C# Windows 窗体)

发布于 2024-12-05 08:54:37 字数 455 浏览 0 评论 0原文

SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString);
sqlConn.Open();
SqlCommand sqlComm = new SqlCommand("SELECT Price FROM Pricing WHERE FoodID = 1", sqlConn);
SqlDataReader r = sqlComm.ExecuteReader();
while (r.Read())
{
    price1 = (float)r["Price"];
}
r.Close(); 
sqlConn.Close();

我得到的 InvalidCastException 错误指向“price1 = (float)r["Price"];”我是 C# 和任何编程语言的新手,请指导我!

SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString);
sqlConn.Open();
SqlCommand sqlComm = new SqlCommand("SELECT Price FROM Pricing WHERE FoodID = 1", sqlConn);
SqlDataReader r = sqlComm.ExecuteReader();
while (r.Read())
{
    price1 = (float)r["Price"];
}
r.Close(); 
sqlConn.Close();

The InvalidCastException error i get points to "price1 = (float)r["Price"];" im new to c# and any of the programming languages , please guide me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2024-12-12 08:54:37

假设价格是浮点数,您应该使用 GetFloat< /a> 相反:

price1 = r.GetFloat(0); // first column

Assuming Price is a float, you should use GetFloat instead:

price1 = r.GetFloat(0); // first column
情仇皆在手 2024-12-12 08:54:37

您可以以更安全的方式重写代码,如下所示:

using(var sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
    sqlConn.Open();

    SqlCommand sqlComm = new SqlCommand("SELECT Price FROM Pricing WHERE FoodID = 1", sqlConn);

    using(var reader = sqlComm.ExecuteReader())
    {
        while (reader.Read())
        {
            var price1 = reader["Price"];
        }
    }
}

这样您就可以确定,无论发生什么情况,阅读器和连接都将关闭并为您处理。如果您调试此代码,您将看到运行时将在 Price1 变量中存储哪种类型的对象,然后您可以在需要时转换为该类型,因为如果您转换为 float 失败,我认为您没有得到正确地从读卡器上浮动。

SqlDataReader 还有其他方法来检索数据,如果您确定 Price 是一个 fload,则可以使用 reader.GetSingle 方法,例如,如果它是 int,则可以使用 reader.GetInt等等。

you can rewrite your code in a much safer way, like this:

using(var sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
    sqlConn.Open();

    SqlCommand sqlComm = new SqlCommand("SELECT Price FROM Pricing WHERE FoodID = 1", sqlConn);

    using(var reader = sqlComm.ExecuteReader())
    {
        while (reader.Read())
        {
            var price1 = reader["Price"];
        }
    }
}

so you are sure that no matter what happens reader and connection will be closed and disposed for you. if you debug this code you will see what kind of object will be stored in the price1 variable at runtime and you can then cast to that type afterwards, if needed, because if your cast to float was failing, I thing you were not getting the float properly from the reader.

The SqlDataReader has also other methods to retrieve data, if you are sure that Price is a fload you can use reader.GetSingle method for example, if it was an int you could use reader.GetInt and so on.

往日 2024-12-12 08:54:37

这意味着您的“价格”列不是浮点值 - 如果您像这样进行转换,则类型必须完全正确,您可以尝试更宽松的方法,将其转换为浮点值:

price1 = Convert.ToSingle(r["Price"]);

如果“price”列包含不同的数据类型(这很可能),则应使用本机类型并将变量设置为该类型,即如果它是 double make price1< /code> 一个 double 变量代替:

price1 = reader.GetDouble(0);

This means that your "price" column is not a float value - if you cast like this, the type has to be exactly right, you can try the more lenient way by converting it to a float value:

price1 = Convert.ToSingle(r["Price"]);

If the "price" column contains a different data type (which is very likely), you should use the native type instead and make your variable that type, i.e. if its a double make price1 a double variable instead:

price1 = reader.GetDouble(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文