SQL SERVER中的字符串到货币数据类型

时间:2020-12-07 01:50:26

I tried to insert a STRING from a TextBox to the COLUMN that has a MONEY DATA TYPE but an error prompt appeared and says : Error Occured, Can't Add Patient! Disallowed implicit conversion from data type nvarchar to data type money, table 'TblName', column 'pTotalDue'. Use the CONVERT function to run this query

我试图从文本框中插入一个字符串到具有货币数据类型的列,但出现了一个错误提示,并说:发生错误,不能添加耐心!不允许从数据类型nvarchar到数据类型money、表'TblName'、列'pTotalDue'的隐式转换。使用CONVERT函数运行这个查询

how can I CONVERT it? could someone tell me how to do it?

怎么转换呢?有人能告诉我怎么做吗?

here is my codes :

这是我的代码:

Private Function INSERT() As String
        Dim SQLcon As New SqlConnection
        Dim SQLdr As SqlDataReader
        Try
            SQLcon.ConnectionString = "Data Source=#####;Initial Catalog=#####;Persist Security Info=True;User ID=#####;Password=#####"
            Dim SQLcmd As New SqlCommand("INSERT INTO dbo.Patients" & _
                                         "(pIDNo,pLName,pFName,pMI,pSex,pStatus,pTelNo,pDocID,pAddr,pStreet,pBarangay,pCity,pProvince,pLNameKIN,pFNameKIN,pMIKIN,pRelationKIN,pTotalDue)" & _
                                         "VALUES(@pIDNo,@pLName,@pFName,@pMI,@pSex,@pStatus,@pTelNo,@pDocID,@pAddr,@pStreet,@pBarangay,@pCity,@pProvince,@pLNameKIN,@pFNameKIN,@pMIKIN,@pRelationKIN,@totaldue)", SQLcon)
            SQLcmd.Parameters.AddWithValue("@pIDNo", txtPNumber.Text)
            SQLcmd.Parameters.AddWithValue("@pLName", txtLname.Text)
            SQLcmd.Parameters.AddWithValue("@pFName", txtFname.Text)
            SQLcmd.Parameters.AddWithValue("@pMI", txtMI.Text)
            SQLcmd.Parameters.AddWithValue("@pSex", txtPatientSex.Text)
            SQLcmd.Parameters.AddWithValue("@pStatus", txtStatus.Text)
            SQLcmd.Parameters.AddWithValue("@pTelNo", txtPatientTelNo.Text)
            SQLcmd.Parameters.AddWithValue("@pDocID", txtPatientDoctor.Text)
            SQLcmd.Parameters.AddWithValue("@pAddr", txtStreetNumber.Text)
            SQLcmd.Parameters.AddWithValue("@pStreet", txtStreetName.Text)
            SQLcmd.Parameters.AddWithValue("@pBarangay", txtBarangay.Text)
            SQLcmd.Parameters.AddWithValue("@pCity", txtCity.Text)
            SQLcmd.Parameters.AddWithValue("@pProvince", txtProvince.Text)
            SQLcmd.Parameters.AddWithValue("@pLnameKIN", txtKinLname.Text)
            SQLcmd.Parameters.AddWithValue("@pFnameKIN", txtKinFname.Text)
            SQLcmd.Parameters.AddWithValue("@pMIKIN", txtKinMI.Text)
            SQLcmd.Parameters.AddWithValue("@pRelationKIN", txtRelationToPatient.Text)
            SQLcmd.Parameters.AddWithValue("@totaldue", txtTotalDue.Text)
            SQLcon.Open()
            MsgBox("Patient Added!", MsgBoxStyle.Information)
            SQLdr = SQLcmd.ExecuteReader()
        Catch ex As Exception
            MessageBox.Show("Error Occured, Can't Add Patient!" & ex.Message)
        Finally
            SQLcon.Close()
        End Try
        Return ""
    End Function

BTW the version of SQL SERVER that I'm currently using is 2005.

顺便说一下,我目前使用的SQL SERVER版本是2005。

1 个解决方案

#1


3  

Try changing this line:

试着改变这条线:

SQLcmd.Parameters.AddWithValue("@totaldue", txtTotalDue.Text)

to this:

:

SQLcmd.Parameters.Add("@totaldue", SqlDbType.Money).Value = Decimal.Parse(txtTotalDue.Text)

#1


3  

Try changing this line:

试着改变这条线:

SQLcmd.Parameters.AddWithValue("@totaldue", txtTotalDue.Text)

to this:

:

SQLcmd.Parameters.Add("@totaldue", SqlDbType.Money).Value = Decimal.Parse(txtTotalDue.Text)