Foxtable(狐表)用户栏目专家坐堂 → [求助]向数据库写入内容时如何自动去掉非法的字符


  共有1905人关注过本帖树形打印复制链接

主题:[求助]向数据库写入内容时如何自动去掉非法的字符

帅哥哟,离线,有人找我吗?
huangfanzi
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1104 积分:8956 威望:0 精华:0 注册:2014/10/25 11:24:00
[求助]向数据库写入内容时如何自动去掉非法的字符  发帖心情 Post By:2020/3/31 18:38:00 [只看该作者]

窗口中的提交按钮有以下代码,在使用中,使用者会将从微信中的信息复制到窗口的各种控件中,有时,微信中的信息是二行,也就是二行之间有个回车的,但照样可以复制到ComboBox中,并且看不出异样,还是在一行,可是向数据库写入时就会报错,在服务端会弹出一个字符串会截断的报错窗口,然后服务端就当机了,所以,请老师教导下如何写代码能把各种非法字符去掉
Dim cmbgs As WinForm.ComboBox = e.Form.Controls("ComboBox_公司")
Dim txbdm As WinForm.TextBox = e.Form.Controls("TextBox_代码")
Dim cmbgcmc As WinForm.ComboBox = e.Form.Controls("ComboBox_工程名称")
Dim cmbgcdd As WinForm.ComboBox = e.Form.Controls("ComboBox_工程地点")
Dim txbhtlx As WinForm.TextBox = e.Form.Controls("TextBox_合同类型")
Dim txbhtbh As WinForm.TextBox = e.Form.Controls("TextBox_合同编号")
Dim dtpkpsj As WinForm.DateTimePicker = e.Form.Controls("DateTimePicker_开盘时间")
Dim cmbjzff As WinForm.ComboBox = e.Form.Controls("ComboBox_浇注方法")
Dim cmbjzbw As WinForm.ComboBox = e.Form.Controls("ComboBox_浇注部位")
Dim cmbhntbj As WinForm.ComboBox = e.Form.Controls("ComboBox_混凝土标号")
Dim cmbtld As WinForm.ComboBox = e.Form.Controls("ComboBox_塌落度")
Dim ncbjhfl As WinForm.NumericComboBox = e.Form.Controls("NumericComboBox_计划方量")
Dim ncbyslc As WinForm.NumericComboBox = e.Form.Controls("NumericComboBox_运输里程")
Dim ncblcdj As WinForm.NumericComboBox = e.Form.Controls("NumericComboBox_里程单价")
Dim txbclpl As WinForm.TextBox = e.Form.Controls("TextBox_车辆及频率")
Dim cmbxflxr As WinForm.ComboBox = e.Form.Controls("ComboBox_需方联系人")
Dim txbxfdh As WinForm.TextBox = e.Form.Controls("TextBox_需方电话")
Dim txbywy As WinForm.TextBox = e.Form.Controls("TextBox_业务员")
Dim txbywydh As WinForm.TextBox = e.Form.Controls("TextBox_业务员电话")
Dim ccbjsyq As WinForm.CheckedComboBox = e.Form.Controls("CheckedComboBox_其它技术要求")
Dim txbbz As WinForm.TextBox = e.Form.Controls("TextBox_备注")
Dim cmbfhdw As WinForm.ComboBox = e.Form.Controls("ComboBox_发货单位")
Dim cmbjsz As WinForm.ComboBox = e.Form.Controls("ComboBox_接收站")
Dim cbtgfh As WinForm.CheckBox = e.Form.Controls("CheckBox_跳过复核")
Dim rbcg As WinForm.RadioButton = e.Form.Controls("RadioButton_常规")
Dim str As String = "以下内容不可为空或录入内容错误"
If cmbgs.Value = Nothing Then
    str = str & vbcrlf & "需方名称"
End If
If txbdm.Value = Nothing Then
    str = str & vbcrlf & "需方代码"
End If
If cmbgcmc.Value = Nothing Then
    str = str & vbcrlf & "工程名称"
End If
If txbhtlx.Value = Nothing Then
    str = str & vbcrlf & "合同类型"
End If
If txbhtbh.Value = Nothing Then
    str = str & vbcrlf & "合同编号"
End If
If dtpkpsj.Value = Nothing Then
    str = str & vbcrlf & "开盘时间"
End If
If cmbjzff.Value = Nothing Then
    str = str & vbcrlf & "浇注方法"
End If
If cmbjzbw.Value = Nothing Then
    str = str & vbcrlf & "浇注部位"
End If
If cmbhntbj.Value = Nothing Then
    str = str & vbcrlf & "混凝土标号"
End If
If ncbjhfl.Value <= 0 Then
    str = str & vbcrlf & "计划方量"
End If
If cmbxflxr.Value = Nothing Then
    str = str & vbcrlf & "需方联系人"
End If
If txbxfdh.Value = Nothing Then
    str = str & vbcrlf & "需方电话"
End If

If rbcg.Checked = False Then
    If ncbyslc.Value = Nothing Then
        str = str & vbcrlf & "运输里程"
    End If
End If

If str <> "以下内容不可为空或录入内容错误" Then
    MessageBox.Show(str,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error)
    Return
End If


Dim dr As DataRow
If QQClient.Ready = False Then
    PopMessage("服务端未启动,无法生成单号!","提示",PopIconEnum.Infomation,5)
Else
    Dim cmd As New SQLCommand
    Dim dt As Date
    cmd.C
    cmd.CommandText = "Select GetDate()"
    dt = cmd.ExecuteScalar()
    Dim dqsj As Date = Format(dt, "yyyy-MM-dd HH:mm:ss")
    'Dim dqsj As Date = Date.Now
    Dim bh As String = Format(dqsj,"yyyyMMdd")
    Dim rt As String =  QQClient.SendWait(":x" & bh & "x:")
    Dim id As Integer
    If rt > "" Then
        If  Integer.TryParse(rt,id)  Then
            dr = _dt生产任务通知.AddNew
            dr("单号") = "SCXD-" & bh & "-" &  Format(id,"000")
            dr("录入人") = _UserName
            dr("录入时间") = Format(dt, "yyyy-MM-dd HH:mm:ss")
            dr("公司") = cmbgs.Value
            dr("代码") = txbdm.Value
            dr("工程名称") = cmbgcmc.Value
            dr("工程地点") = cmbgcdd.Value
            dr("合同类型") = txbhtlx.Value
            dr("合同") = txbhtbh.Value
            dr("开盘时间") = dtpkpsj.Value
            dr("浇注方法") = cmbjzff.Value
            dr("浇注部位") = cmbjzbw.Value
            dr("混凝土标号") = cmbhntbj.Value
            dr("塌落度") = cmbtld.Value
            dr("运输里程") = ncbyslc.Value
            dr("里程单价") = ncblcdj.Value
            dr("计划方量") = ncbjhfl.Value
            dr("车辆及频率") = txbclpl.Value
            dr("需方联系人") = cmbxflxr.Value
            dr("需方电话") = txbxfdh.Value
            dr("业务员") = txbywy.Value
            dr("业务员电话") = txbywydh.Value
            dr("其它技术要求") = ccbjsyq.Value
            dr("备注") = txbbz.Value
            dr("发货单位") = cmbfhdw.Value
            dr("接收站") = cmbjsz.Value
            If cbtgfh.Checked = True Then
                dr("复核人") = _UserName
                dr("复核时间") = Date.Now
                dr("复核意见") = "通过"
                PopMessage("单号:" & dr("单号") & vbcrlf & "可在待收单中查看","提示:生成新的通知单号",PopIconEnum.Infomation,5)
            Else
                PopMessage("单号:" & dr("单号") & vbcrlf & "可在待复核中查看","提示:生成新的通知单号",PopIconEnum.Infomation,5)
            End If
            dr.Save
            
        Else
            PopMessage("服务器返回错误信息:" & rt,"提示",PopIconEnum.Infomation,5)
        End If
    Else
        PopMessage("服务器无响应,无法生成编号!","提示",PopIconEnum.Infomation,5)
    End If
End If

 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106763 积分:543016 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/31 21:07:00 [只看该作者]

字符串会截断的报错是因为列字符长度不够,改大即可:http://www.foxtable.com/webhelp/topics/0036.htm

文本框、组合框也有一个“最大长度”设置,把最大长度改为和列字符长度一致即可

 回到顶部
帅哥哟,离线,有人找我吗?
huangfanzi
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1104 积分:8956 威望:0 精华:0 注册:2014/10/25 11:24:00
  发帖心情 Post By:2020/3/31 21:17:00 [只看该作者]

字符串长度是够的,我设了100位,我还特意数过,我测试了,现在的问题其实是就是从微信中复制过来时,二行文本直接复制到窗口文本框时把回车给带进去了,可这个回车符还看不到,感觉还是一行
我在电脑上操作到是没什么问题,但用下机操作就报错了,老师,如果去掉这个回车,谢谢!

图片点击可在新窗口打开查看此主题相关图片如下:3.jpg
图片点击可在新窗口打开查看

[此贴子已经被作者于2020/3/31 21:23:28编辑过]

 回到顶部
帅哥,在线噢!
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106763 积分:543016 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/31 21:34:00 [只看该作者]

回车符属于正常字符,不是什么特殊字符回车符,再说也就占1个字符长度,正常是回车+换行符比如vbcrlf也就是2个字符的长度,不可能会影响存储的。估计应该是复制的时候数据后面有大量的空格,赋值的时候trim一下

dr("工程名称") = cmbgcmc.text.trim()

 回到顶部