0
數據類型費用爲int在SQL Server數據庫請指導如何將字符串值轉換爲整數,在vb.net
費用的,我從表中費用得到價值
'Total Expenses B/W Dates
sql = "select COALESCE (SUM (amount), 0) from tblexpense Where transactiondate >= @p1 and transactiondate <= @p2"
CmdObj = New SqlCommand(sql, ConObj)
CmdObj.Parameters.Add("@p1", SqlDbType.Date).Value = DateTimePicker1.Value.Date
CmdObj.Parameters.Add("@p2", SqlDbType.Date).Value = DateTimePicker2.Value.Date
CmdObj.ExecuteScalar()
Dim sumexpense As Integer = CmdObj.ExecuteScalar
LblExp.Text = "Rs. " & sumexpense
保存按鈕保存費用的價值爲表
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ConObj = New SqlConnection(ConStr)
ConObj.Open()
Dim sql As String = "insert into tblbalance (tcon,pcon,ecoll,pendcoll,expense,bcoll,nprofit) values(@tcon,@pcon,@ecoll,@pendcoll,@expense,@bcoll,@nprofit)"
With CmdObj
.Parameters.Add("@tcon", SqlDbType.Int).Value = lblTcon.Text
.Parameters.Add("@pcon", SqlDbType.Int).Value = LblPcon.Text
.Parameters.Add("@ecoll", SqlDbType.Int).Value = LblEColl.Text
.Parameters.Add("@pendcoll", SqlDbType.Int).Value = LblPColl.Text
.Parameters.Add("@expense", SqlDbType.Int).Value = LblExp.Text
.Parameters.Add("@bcoll", SqlDbType.Int).Value = LblBcoll.Text
.Parameters.Add("@nprofit", SqlDbType.Int).Value = LblNet.Text
End With
CmdObj.Connection = ConObj
CmdObj.ExecuteNonQuery()
MsgBox("Saved Successfully")
ConObj.Close()
End Sub
你應該使用相同的參數化的語句升你在你的第一塊代碼中做過。然後確保lblExp.Text可以解析爲一個整數,以確保來自用戶的輸入可以匹配類型,並在成功解析時將該變量傳遞給您的參數。 –
我試過但沒有成功...從字符串轉換爲整數無效 –
您是否修復了使用參數的語句?因爲在當前版本中,注入的值仍然被包含撇號,並且包含帶撇號的值表示字符串類型而不是數字類型。參數也可以解決這個問題。 –