如何在VB.NET中从CSV文件中读取ip地址?
问题描述:
嗨
我有一个带有列ip地址的csv文件,当我读取csv数据并绑定在datatable时,我只会得到第一个dight。
如果这样的IP地址为192.168.0.13
i只会获得192.168,其余的东西都会跳过。 csv文件中的IP地址列的行为类似于十进制值。如何读取完整值。
Hi
I have one csv file with column ip address,when i read csv data and bind in datatable,i will get only 1st dight.
if ip address like this 192.168.0.13
i will get 192.168 only,rest of thing skipped. Ip address column in csv file behave like decimal value.How to read full values.
Dim fileneme As String = file1.FileName
Dim filepath As String = Server.MapPath(".") & "\" & Session("userid")
Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath & ";Extended Properties=Text;"
Dim conn As New OleDbConnection(strConnString)
Try
conn.Open()
Dim cmd As New OleDbCommand("SELECT IPADDRESS FROM [" & fileneme & "]", conn)
Dim da As New OleDbDataAdapter()
da.SelectCommand = cmd
Dim dt As New DataTable()
da.Fill(dt)
da.Dispose()
我是什么尝试过:
如果我在csv中的ip地址值中添加双引号,那么我将在datatable中使用双引号获得完整值,但是无法添加双引号。
请尽快回复
问候
Aravind
What I have tried:
If i add double quotes in ip address value in csv,then i will get full value with double quotes in datatable,but cant add double quotes.
Pls reply asap
Regards
Aravind
答
这是因为当Jet读取CSV数据时,它假定192.168是一个数字,并按此处理它。它不会知道它是一个IP地址,它只是查看逗号后面的第一个字符并说啊,数字。这是一个数字,我会解析它。
IP CSV文件中的地址需要用双引号分隔才能使它们成为字符串,除非您编写(或修改现有的)CSV读取器。
That's because when Jet reads the CSV data, it assumes that 192.168 is a number, and processes it as such. It doesn't "know" it's an IP address, it just looks at the first character after the comma and says "Ah, numeric. This is a number, I'll parse it."
IP addresses in CSV files need to be delimited with double quotes to make them strings, unless you write (or modify an existing) CSV reader.
正如OriginalGriff已经说过,没有双引号,ip地址格式与十进制格式冲突,因此问题。
有一些解决方案:
- 最标准的解决方案是在双引号之间嵌入ip。阅读程序应该照顾它,因为它符合CSV标准。
- 制作一个自定义例程直接进行阅读,文件io ...
- 使用变种。我总是使用CSV格式的变体,而不是逗号而是文本字段。我做了直接阅读的例程。
As OriginalGriff already said, without double quotes, the ip address format collide with decimal format, thus the problem.
There is a few solutions:
- the most standard solution is to embed the ip between double quotes. The reading program should take care of it since it conform to CSV standard.
- craft a custom routine to do the reading directly, file io ...
- Use a variant. I always use a variant of CSV format with tabs instead of commas and nothing around text fields. And I make a routine to do direct reading.