我写了这段代码:

Try
    Uff_IdW = Me.DataGridView1.Item(DgvCol, IdWInsDB).Value.ToString.Trim().Split("_")
    If Uff_IdW.Length = 2 Then SplitOk = True Else SplitOk = False
Catch ex As Exception
    mErr = "split error: " & ex.ToString
    SQLStr = "INSERT INTO errlog (Sect, ErrDescr) VALUES ('GP', '" & Errore & "')"
    InsertDBase(cnString, SQLStr)
    SplitOk = False
End Try


但由于ex.tostring中有“ vbcrlf”,因此插入查询无法正常工作。

由代码产生的查询字符串如下所示:

"INSERT INTO errlog (Sect, ErrDescr) VALUES ('GP', 'split error: System.NullReferenceException: Object reference not set to an instance of an object." & vbCrLf & "   in Test_GP.Form1.FirstOption_DGV_Click(Int32 DgvRow, Int32 DgvCol)')"


如何解决此问题并将ex.tostring插入到数据库中而没有错误?

最佳答案

在字符串上使用Replace函数去除回车符:

SQLStr = "INSERT INTO errlog (Sect, ErrDescr) VALUES ('GP', '" & Errore & "')"
SQLStr = SQLStr.Replace(Environment.NewLine, " ")
InsertDBase(cnString, SQLStr)

10-07 21:03