在VB中读取文本文件

时间:2014-03-12 16:45:07

标签: vb.net

这里有新的Visual Basic程序员。试图让程序读取文本文件,但它似乎根本不起作用,没有错误消息或任何东西。它根本没有抓住值

文本文件名完全相同。

Public Sub ReadDef()

    Dim DefSR As IO.StreamReader = IO.File.OpenText("BikeDefault.txt")

    GlobalTotBikes = DefSR.ReadLine()
    GlobalRentRate = DefSR.ReadLine()
    GlobalHSTRate = DefSR.ReadLine()
    GlobalTourRate = DefSR.ReadLine()
    GlobalGPSRate = DefSR.ReadLine()
    GlobalInsurRate = DefSR.ReadLine()
    GlobalWaterBotRate = DefSR.ReadLine()
    GlobalNextBookNum = DefSR.ReadLine()
    GlobalNextCustNum = DefSR.ReadLine()
    GlobalNextInvoiceNum = DefSR.ReadLine()

    DefSR.Close()

End Sub

我将这段代码多次与我给出的例子进行了比较,但我没有看到任何不同。

感谢。

2 个答案:

答案 0 :(得分:3)

在Google http://www.dotnetperls.com/streamreader-vbnet

上进行简单搜索

100%确定BikeDefault.txt存在。如果您希望确保将文件复制到C:\云端硬盘以简化操作,请将BikeDefault.txt替换为"C:\\BikeDefault.txt"

您可以像这样使用StreamReader:

Imports System.IO

Module Module1

    Sub Main()
    ' Store the line in this String.
    Dim line As String
    Dim FilePath As String = "C:\\BikeDefault.txt"
    ' Create new StreamReader instance with Using block.
    Using reader As StreamReader = New StreamReader(FilePath)
        ' Read one line from file
        line = reader.ReadLine
    End Using

    ' Write the line we read from "file.txt"
    Console.WriteLine(line)
    End Sub

End Module

或者使用File.ReadAllLines保持简单。

For Each line As String In File.ReadLines("MyTextFile.txt")
    'Code here to read each line
Next line

答案 1 :(得分:1)

我用

CREATE SEQUENCE seqrefno
AS INT 
START WITH 1
INCREMENT BY 1 
GO


SELECT 'NX-'+CONVERT( CHAR(6),GETDATE() ,12) +'-'+  CAST(NEXT VALUE FOR seqrefno AS VARCHAR(10))