我總是得到一個「字符串或二進制數據將被截斷」我搜索谷歌的答案,但我找不到相同的值爲我的文本框。在我的程序中,我有一個文本框(multiLine),它具有數值,逗號,小數點(2位)& vbCrlf,它看起來像這樣。可以存儲數字,逗號,小數點(2位)和vbcrlf的VB.net/SQL數據類型?
1,234,567.89
9,876,543.21
上圖是我的文本框所具有的值的一個示例。 (注:請注意小心地將上面的數字有2個逗號,因爲它是百萬元),我不知道什麼是背後的原因,但如果它只有1COMMA,我的代碼工作完全正常一樣
123,456.78
987,654.32
我已經設置我的vb.net數據類型爲字符串,小數,雙精度和對象,但我仍然得到相同的錯誤。關於我的SQL數據類型,我已經將它設置爲varchar,nvarchar,char,但仍然得到相同的錯誤。這裏是我的代碼:
*This is my CLASS code title SQLControl
Public Sub AddToJournalEntry(DrCrAmount As Object)
Try
Dim strInsert As String = "INSERT INTO JournalEntry (DrCrAmount) " & _
"VALUES (" & _
"'" & DrCrAmount & "') "
SQLCon.Open()
SQLCmd = New SqlCommand(strInsert, SQLCon)
SQLCmd.ExecuteNonQuery()
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
*This my code in my frmMain
Dim sql As New SQLControl
Private Sub btnGenerate_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerate.Click
Dim format1 As String = "{0,-10} {1,12}"
Dim Line1Amount, Line2Amount As String
If lbl1.Text = "Dr" Then
Line1Amount = String.Format(format1, txtAmount1.Text, vbNullString)
Line1Amount = Line1Amount.TrimEnd(" ", vbNullString)
ElseIf lbl1.Text = "Cr" Then
Line1Amount = String.Format(format1, vbNullString, txtAmount1.Text)
Line1Amount = Line1Amount.TrimEnd(" ", vbNullString)
Else
Line1Amount = String.Format(format1, vbNullString, vbNullString)
Line1Amount = Line1Amount.TrimEnd(" ", vbNullString)
lbl1.Text = vbNullString
End If
If lbl2.Text = "Dr" Then
Line2Amount = String.Format(format1, txtAmount2.Text, vbNullString)
Line2Amount = Line2Amount.TrimEnd(" ", vbNullString)
ElseIf lbl2.Text = "Cr" Then
Line2Amount = String.Format(format1, vbNullString, txtAmount2.Text)
Line2Amount = Line2Amount.TrimEnd(" ", vbNullString)
Else
Line2Amount = String.Format(format1, vbNullString, vbNullString)
Line2Amount = Line2Amount.TrimEnd(" ", vbNullString)
lbl2.Text = vbNullString
End If
Dim myString2 As String = Line1Amount & vbCrLf & Line2Amount
****This code is to delete unnecessary spaces and new lines
myString2 = myString2.TrimEnd(" ", vbNullString, Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed))
txtDrCrAmount.Text = myString2
End Sub
Private Sub btnRecord_Save_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord_Save.Click
sql.AddToJournalEntry(txtDrCrAmount.Text)
End Sub
注意:我的代碼已經被編輯,爲了您的方便,我已經刪除了不必要的代碼。
謝謝=)順便說一句,我是一個新手程序員。
如果使用數據類型varchar創建數據庫表,則不應該有任何問題。 –
嗨Akhil先生,是的,它正在工作,但問題在於上面提到的2comcom。它工作正常,在1 comma.tnx – Nadz
但同樣適用於我與兩個甚至三個逗號和decimal.please檢查錯誤發生的地方。 –