VB.Net数据库程序(控制台应用程序)

时间:2015-03-03 00:48:17

标签: vb.net console-application

我目前正在开始研究这种编程语言,我一直想知道为什么我的程序部分工作不正常。我遇到的主要问题是在VIEW函数中,当我尝试查看记录时,只显示添加的最新记录并多次显示。程序的其他部分我无法理解我将如何处理,这包括UPDATE函数和DELETE函数,他们更新记录或删除选择的记录...任何指导表示赞赏(CODE SHOWN BELOW)< / p>


Module initialise Structure Record Public recFirstname As String Public recSurname As String Public recPhoneNum As String End Structure
    Public Contact As Record
    Public recLength As Integer = Len(Contact)
    Public numOfRecord As Integer

    Public firstName As String = Contact.recFirstname
    Public surName As String = Contact.recSurname
    Public telephone As String = Contact.recPhoneNum
    Public myFileName As String = CurDir() & "/myphonebook.dat"
    End Module

    Module Module1

    Sub Main()
        Dim input As String
        Do
            Do
                Console.WriteLine(" ************************************")

                Console.WriteLine(" If you want to VIEW existing files...")
                Console.WriteLine(" Press V, then hit enter" & vbNewLine)

                Console.WriteLine(" If you want to CREATE a new record...")
                Console.WriteLine(" Press C, then hit enter" & vbNewLine)

                Console.WriteLine(" If you want to UPDATE an existing record...")
                Console.WriteLine(" Press U, then hit enter" & vbNewLine)

                Console.WriteLine(" If you want to DELETE an existing record...")
                Console.WriteLine(" Press D, then hit enter" & vbNewLine)

                Console.WriteLine(" To QUIT: Press Q, then hit enter")

                Console.WriteLine(" ************************************")

                input = Console.ReadLine
                input = input.ToUpper

                If input <> "V" And input <> "C" And input <> "U" And input <> "D" And input <> "Q" Then
                    Console.WriteLine(" ---UNRECOGNISABLE BUTTON PRESSED.---" & vbNewLine & " ---PLEASE ENTER A VALID INPUT.---" & vbNewLine)
                End If

            Loop Until input = "V" Or input = "C" Or input = "U" Or input = "D" Or input = "Q"

            Select Case input
                Case "V" : procVIEWfile()
                Case "C" : procCREATEfile()
                Case "U" : procUPDATEfile()
                Case "D" : procDELETEfile()
                Case "Q" : End
            End Select

        Loop
    End Sub

    Private Sub procVIEWfile()
        Dim firstName As String = Contact.recFirstname
        Dim surname As String = Contact.recSurname
        Dim telephone As String = Contact.recPhoneNum
        Dim myFileName As String = CurDir() & "/myphonebook.dat"

        If numOfRecord < 1 Then
            Console.WriteLine(" ---YOU DO NOT HAVE ANY RECORDS!---" & vbNewLine & " ---PLEASE ADD THEM BEFORE YOU TRY TO VIEW THEM.---" & vbNewLine)
        End If

        FileOpen(1, myFileName, OpenMode.Random, , , recLength)

        For i As Integer = 1 To numOfRecord
            FileGet(1, numOfRecord, i)

            Console.WriteLine(" *****************" & vbNewLine)
            Console.WriteLine(" Firstname: " & firstName & vbNewLine)
            Console.WriteLine(" Surname: " & surname & vbNewLine)
            Console.WriteLine(" Phonenumber: " & telephone & vbNewLine)
            Console.WriteLine(" *****************" & vbNewLine)

            Console.WriteLine(" ---PRESS ANY KEY FOR THE NEXT FILE---" & vbNewLine)

            Console.ReadLine()
        Next

        FileClose(1)

        Main()

        End
    End Sub

    Private Sub procCREATEfile()
        Dim myFileName As String = CurDir() & "/myphonebook.dat"

        Console.WriteLine(vbNewLine & " ---CREATING A FILE...---" & vbNewLine)

        FileOpen(1, myFileName, OpenMode.Random, , , recLength)
        numOfRecord = LOF(1) / recLength

        Console.WriteLine(" ***********************")

        Console.WriteLine(vbNewLine & " Enter the Firstname")
        firstName = Console.ReadLine()

        Console.WriteLine(vbNewLine & " Enter the Surname")
        surName = Console.ReadLine()

        Console.WriteLine(vbNewLine & " Enter Phonenumber")
        telephone = Console.ReadLine()

        Console.WriteLine(" ***********************")

        Console.WriteLine(vbNewLine & " ---FILE CREATED---" & vbNewLine)

        numOfRecord += 1

        FilePut(1, Contact, numOfRecord)

        FileClose(1)

        Main()
    End Sub

    Private Sub procUPDATEfile()
        Console.WriteLine(" ---UPDATE FUNCTION ACTIVATED---")

        Dim answer As String
        For i As Integer = 1 To numOfRecord
            FileGet(1, Contact, i)
            Console.WriteLine(" *****************" & vbNewLine)
            Console.WriteLine(" Firstname: " & firstName & vbNewLine)
            Console.WriteLine(" Surname: " & surName & vbNewLine)
            Console.WriteLine(" Phonenumber: " & telephone & vbNewLine)
            Console.WriteLine(" *****************" & vbNewLine)
            Console.WriteLine(" Is this the file you wish to update? Y/N" & vbNewLine)
            answer = Console.ReadLine()
        Next

        FileClose(1)

        Main()
    End Sub

    Private Sub procDELETEfile()
        Console.WriteLine(" ---DELETE FUNCTION ACTIVATED---")

        FileClose(1)

        Main()
    End Sub
End Module

0 个答案:

没有答案