将数据类型varchar转换为numeric时出错 [英] Error Converting datatype varchar to numeric

查看:1008
本文介绍了将数据类型varchar转换为numeric时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试从数据库读取数据并进入标签文本属性。



但是我收到错误'错误将数据类型varchar转换为数字'。



我现在真的很困惑为什么......它发生在读者身上。阅读。()



任何帮助将不胜感激,谢谢。



Hello,

I am trying to read data from a database and into a labels text property.

However I am receiving the error 'Error Converting datatype varchar to numeric'.

I am now really bewildered to why... It is occurring on While reader.Read()

Any help will be much appreciated, Thanks.

Dim queryString As String = "SELECT pointsBalance FROM Accounts WHERE custID = '" & CustId & "'"
       Dim SqlConnectionString As String = My.Settings.LoyaltyConnectionString
       CreateCommand(queryString, SqlConnectionString)

   End Sub

   Public Sub CreateCommand(ByVal queryString As String, ByVal connectionString As String)

       Using connection As New SqlConnection(connectionString)
           connection.Open()
           Dim command As New SqlCommand(queryString, connection)
           Dim reader As SqlDataReader = command.ExecuteReader()
           While reader.Read()
               LabelOutput = reader(0)
           End While
       End Using

   End Sub

推荐答案

正如错误所示,varchar无法隐式转换为数字。

您需要应用强制转换来执行此类转换。
As the error suggests, varchar cannot be converted into a number implicitly.
You need to apply a cast to do this type of conversion.


检查帐户表的设计。它可能有custID作为varchar,CustId变量是数字。更改表的设计或传递CustId的字符串版本。
Check the design of the "Accounts" table. It probably has custID as a varchar and the CustId variable is numeric. Change the design of the table or pass the string version of CustId.


这篇关于将数据类型varchar转换为numeric时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆