在命令行参数上获取异常,但在IDE调试中有效 [英] Getting Exception on Command Line Args, but works within IDE debug
问题描述
我正在用VB 2005编写一个控制台应用程序,我接受一个文本文件作为
输入,解析它并将文本数据插入到SQL表中。我已经正确编写了代码,因为它可以正常运行
在IDE中运行它,因为你必须进入Debug
Project Properties指定参数。如果我构建文件并运行
从命令行运行它,指定参数,我得到
以下错误:
C:\> devtest.exe devtest.txt
devtest.txt
未处理的异常:System.IndexOutOfRangeException:索引在
boun
ds数组。
在DEVTEST.Module1.Main(String [] args)
注意,出于调试目的,我向控制台写入args()数组的第一个元素
。我没看到索引是如何超出数组的界限
,特别是当它在IDE中工作正常时。如果有人
有任何想法,任何建议都会有所帮助。我正在编写并且从VB 2005 Express运行这个
。以下是我的完整代码:
******************************** ****************** ******************************** ****************** *******
模块模块1
Dim LineOfText As String
Dim AllText As String
Dim LineArray(3)As String
Private ConnectionString As String =" Data
Source = localhost; Integrated Security = SSPI; Initial Catalog = DEVTEST"
Dim Con As New SqlConnection(ConnectionString)
Dim CmdInsert As New SqlCommand(" INSERT INTO DEVTEST(ID,PO_NAME,
AMOUNT)VALUES(''123'',''TEST'',''123.32'')",Con)
Dim args(2)As String
Public Sub Main(ByVal args()As String)
Console.OpenStandardOutput()
Console.WriteLine(args(0))
FileOpen(1,args(0),OpenMode.Input,OpenAccess.Read,
OpenShare.Default)
Do EOF(1)
LineOfText = LineInput(1)
AllText = AllText& LineOfText& vbCrLf
LineArray =拆分(LineOfText,,)
Con.Open()
Dim BatchQuery As String =" INSERT INTO DEVTEST(ID,
TESTVAL1,TESTVAL2)VALUES(& LineArray(0)&",''"& LineArray(1) &
"'',''"& LineArray(2)&"'')"
Dim Cmd As New SqlCommand(BatchQuery, Con)
Dim Adapter As New SqlDataAdapter(Cmd)
Cmd.CommandType = CommandType.Text
Cmd.ExecuteNonQuery()
Con.Close()
循环
End Sub
结束模块
********************************** **************** ********************************** **************** ********
谢谢,
大卫B.
I am writing a console App with VB 2005 where I accept a text file as
input, parse it and insert the text data into an SQL table. I have
written the code correctly as well as I can tell because it works
running it within the IDE, given that you have to go into the Debug
Project Properties to specify the argument. If I build the file and go
run it from the command line, specifying the argument, I get the
following error:
C:\>devtest.exe devtest.txt
devtest.txt
Unhandled Exception: System.IndexOutOfRangeException: Index was outside
the boun
ds of the array.
at DEVTEST.Module1.Main(String[] args)
Note, for debugging purposes, I write to the console the first element
of the args() array. I don''t see how the index is outside the bounds
of the array especially when it works fine within the IDE. If anyone
has any ideas, any suggestions would be helpful. I am writing and
running this from VB 2005 Express. Below is my full code:
************************************************** ************************************************** *******
Module Module1
Dim LineOfText As String
Dim AllText As String
Dim LineArray(3) As String
Private ConnectionString As String = "Data
Source=localhost;Integrated Security=SSPI;Initial Catalog=DEVTEST"
Dim Con As New SqlConnection(ConnectionString)
Dim CmdInsert As New SqlCommand("INSERT INTO DEVTEST (ID, PO_NAME,
AMOUNT) VALUES (''123'', ''TEST'', ''123.32'')", Con)
Dim args(2) As String
Public Sub Main(ByVal args() As String)
Console.OpenStandardOutput()
Console.WriteLine(args(0))
FileOpen(1, args(0), OpenMode.Input, OpenAccess.Read,
OpenShare.Default)
Do Until EOF(1)
LineOfText = LineInput(1)
AllText = AllText & LineOfText & vbCrLf
LineArray = Split(LineOfText, ",")
Con.Open()
Dim BatchQuery As String = "INSERT INTO DEVTEST (ID,
TESTVAL1, TESTVAL2) VALUES (" & LineArray(0) & ", ''" & LineArray(1) &
"'' , ''" & LineArray(2) & "'')"
Dim Cmd As New SqlCommand(BatchQuery, Con)
Dim Adapter As New SqlDataAdapter(Cmd)
Cmd.CommandType = CommandType.Text
Cmd.ExecuteNonQuery()
Con.Close()
Loop
End Sub
End Module
************************************************** ************************************************** ********
Thanks,
David B.
推荐答案
Hello大卫,
我确定这是因为文件扩展名为txt 。
为什么这样做你开始接受文件名''devtest''并开始
在程序中添加''.txt''。
这是最简单的解决方案我能想到。
Hello David,
I am sure it is because of the file extension txt.
Why dont you start accepting just the filename ''devtest'' and start
adding ''.txt'' within the program.
That is the easiest solution I can think of.
Hello David,
我确定这是因为文件扩展名为txt。
为什么不开始接受文件名''devtest''并开始
在程序中添加''.txt''。
这是我能想到的最简单的解决方案。
Hello David,
I am sure it is because of the file extension txt.
Why dont you start accepting just the filename ''devtest'' and start
adding ''.txt'' within the program.
That is the easiest solution I can think of.
Hello David,
我确定这是因为文件扩展名txt。
为什么不开始接受文件名''devtest''并开始
在程序中添加''.txt''。
这是我能想到的最简单的解决方案。
Hello David,
I am sure it is because of the file extension txt.
Why dont you start accepting just the filename ''devtest'' and start
adding ''.txt'' within the program.
That is the easiest solution I can think of.
这篇关于在命令行参数上获取异常,但在IDE调试中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!