我尝试将STRING
从TextBox
插入COLUMN
MONEY DATA TYPE
,但出现了错误提示,并说:错误发布,可以加病人!不允许从数据类型nvarchar到数据类型money,表'TblName',列'pTotalDue'的隐式转换。使用CONVERT函数运行此查询
我该如何转换呢?有人能告诉我怎么做吗?
这是我的代码:
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我目前使用的SQL SERVER版本是2005。
答案 0 :(得分:3)
尝试更改此行:
SQLcmd.Parameters.AddWithValue("@totaldue", txtTotalDue.Text)
到此:
SQLcmd.Parameters.Add("@totaldue", SqlDbType.Money).Value = Decimal.Parse(txtTotalDue.Text)