问题描述
【原数据格式】2000-01-01-00.00.00.000000滨德鲁冀2015-05-04-07.30.43.000000高密货五616.000000000000000000010039401001669200384870065432000-01-01-00.00.00.000000滨德鲁冀2015-05-30-09.23.35.000000青岛货五651.50000000000000000001004298501707970301232000-01-01-00.00.00.000000滨德鲁冀2015-06-01-00.07.27.000000章丘货二218.0000000000000000000100491620130842000-01-01-00.00.00.000000滨德鲁冀2015-05-24-18.56.30.000000淄博货五517.70000000000000000001005391500717850051312000-01-01-00.00.00.000000滨德鲁冀2015-05-28-04.27.29.000000周村货二217.700000000000000000010053992012301【SRSallStr=SRS_Replace(SRSallStr)'将字符替换为标准格式】处理结束后2000-01-01-00.00.00.000000|滨德鲁冀|2015-05-04-07.30.43.000000|高密|货五|6|16.0000000000000000000|1003940100166920038487006543|2000-01-01-00.00.00.000000|滨德鲁冀|2015-05-30-09.23.35.000000|青岛|货五|6|51.5000000000000000000|100429850170797030123|2000-01-01-00.00.00.000000|滨德鲁冀|2015-06-01-00.07.27.000000|章丘|货二|2|18.0000000000000000000|10049162013084|2000-01-01-00.00.00.000000|滨德鲁冀|2015-05-24-18.56.30.000000|淄博|货五|5|17.7000000000000000000|100539150071785005131|2000-01-01-00.00.00.000000|滨德鲁冀|2015-05-28-04.27.29.000000|周村|货二|2|17.7000000000000000000|10053992012301|2000-01-01-00.00.00.000000|滨德鲁冀|2015-05-28-03.55.19.000000|潍坊西|货二|2|18.0000000000000000000|10056052012395|【问题】MyCommandStr="InsertINTOOD货车(车牌号,入口时间,入口站,出口时间,出口站,车辆类型,轴组系数,总重量,分轴重)Values("&_CStr(("鲁"&LineLeep))&","&SRSDate(0)&","&SRSDate(1)&","&_SRSDate(2)&","&SRSDate(3)&","&_SRSDate(4)&","&SRSDate(5)&","&SRSDate(6)&","&SRSDate(7)&")"MyCommandStr但凡出现汉字、英文字母、标点符号均报错。若将其改成下属全数字格式进行试运行操作,则不会出现上述问题:MyCommandStr="InsertINTOOD货车(车牌号,入口时间,入口站,出口时间,出口站,车辆类型,轴组系数,总重量,分轴重)Values("&_CStr((LineLeep))&","&(Microsoft.VisualBasic.Left((SRSDate(0)),13))&","&(1)&","&_(Microsoft.VisualBasic.Left((SRSDate(2)),13))&","&(3)&","&_(4)&","&SRSDate(5)&","&SRSDate(6)&","&SRSDate(7)&")"【附源代码】'创建Access数据库中Tables表单PublicSubSRS_OpAccTables(ByValMyAccFileNameAsString,ByValXmIniFileAsString)CallSRS_File()'关闭所有打开的文件夹,并释放内存DimMyMdbNameAsString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&MyAccFileName'定义项目文件命令字符串'连接当前Access数据库DimMyConnectionAsOleDbConnectionDimMyCommandAsOleDbCommand'1.连接数据库MyConnection=NewOleDbConnection(MyMdbName)MyConnection.Open()'2.添加表记录MyCommand=NewOleDbCommand("CreateTableOD货车(车牌号text,入口时间string,入口站text,出口时间string,出口站text,车辆类型text,轴组系数string,总重量string,分轴重string)",MyConnection)MyCommand.ExecuteNonQuery()'关闭当前Access数据库MyConnection.Close()CallSRS_File()'关闭所有打开的文件夹,并释放内存EndSub'将大卡车数据读入ACCESS数据库文件PublicSubSRSAcc_DakaFile(ByValXmFileNameAsString,ByValOD_FileNameAsString)CallSRS_File()'关闭所有打开的文件夹,并释放内存DimSRSallStrAsString=NothingDimSRSDate()AsStringDimLineLeepAsInteger=0'连接该Access数据库DimMyConnectionAsOleDbConnectionDimMyCommandAsOleDbCommandDimMyMdbNameAsString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&XmFileName'定义项目文件命令字符串MyConnection=NewOleDbConnection(MyMdbName)MyConnection.Open()'打开连接'删除现有数据DimMdbDelStrAsString="DELETEFROMOD货车"MyCommand=NewOleDbCommand(MdbDelStr,MyConnection)MyCommand.ExecuteNonQuery()'关闭连接DimMyCommandStrAsString=Nothing'开始读入OD数据信息XmFileName=A主界面.TextBox1.Text'定义工程项目名称DimXmIniFileAsString=A主界面.TextBox2.Text'定义工程项目名称FileOpen(1,OD_FileName,OpenMode.Input)'在文件非空的情况逐行读入文件内容WhileNotEOF(1)SRSallStr=LineInput(1)'逐行读入文本信息IfSRSallStr=""OrSRSallStr=NothingOrSRSallStr=PenterThenGoToNext_BElseSRSallStr=SRS_Replace(SRSallStr)'将字符替换为标准格式IfInStr(SRSallStr,"入口站")>0ThenGoToNext_AElseSRSDate=Split(SRSallStr,"|")'将字符串赋值给字符函数MyCommandStr="InsertINTOOD货车(车牌号,入口时间,入口站,出口时间,出口站,车辆类型,轴组系数,总重量,分轴重)Values("&_CStr(("鲁"&LineLeep))&","&(Microsoft.VisualBasic.Left((SRSDate(0)),13))&","&SRSDate(1)&","&_(Microsoft.VisualBasic.Left((SRSDate(2)),13))&","&SRSDate(3)&","&_SRSDate(4)&","&SRSDate(5)&","&SRSDate(6)&","&SRSDate(7)&")"MyCommand=NewOleDbCommand(MyCommandStr,MyConnection)MyCommand.ExecuteNonQuery()MyCommandStr=NothingLineLeep=LineLeep+1EndIfNext_A:EndIfNext_B:EndWhileLineLeep=LineLeep+1FileClose(1)FileClose(2)MyConnection.Close()'关闭数据库INIWrite(XmIniFileName,"交通量","OD货车",LineLeep)CallSRS_File()'关闭所有打开的文件夹,并释放内存MsgBox("TellUthesuccessfulofOpenningJiaotong!",MsgBoxStyle.OkOnly,"Congratulation!")EndSub
解决方案
解决方案二:
Access提交sql语句,字符串要用单引号括起来,日期要用#号括起来。
解决方案三:
楼上正解。不过不建议用拼接字符串的形式来执行sql