如何检查输入的数据是否已存在

时间:2015-06-23 14:09:22

标签: vb.net

这是我的代码,用于将数据提交到我的csv文件。在保存之前,如何检查数据是否已存在。

Dim csvFile As String = My.Application.Info.DirectoryPath & "\HoseData.csv"

Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)

    'If My.Computer.FileSystem.FileExists(csvFile) Then

'outFile.WriteLine("job number, sales order number, date, requested by, serial number, hose type, hose size, fitting 1, fitting 2, qty, oal, cut off, offset, crimp 1, crimp 2, cleaned, pigged", False)

    'End If

outFile.WriteLine(TextBox1.Text & "," & TextBox2.Text & "," & """" & DateTimePicker1.Text & """" & "," & ComboBox1.Text & "," & TextBox3.Text & "," & ListBox2.Text & "," & ListBox3.Text & "," & TextBox11.Text & "," & TextBox12.Text & "," & NumericUpDown1.TextAlign & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox9.Text & "," & TextBox10.Text & "," & ListBox4.Text & "," & ListBox5.Text)

        MessageBox.Show("INPUT WAS SAVED")


        Dim butt As System.Windows.Forms.Button = DirectCast(sender, System.Windows.Forms.Button)
        butt.Enabled = False

        'butt.Visible = False
        outFile.Close()

        Console.WriteLine(My.Computer.FileSystem.ReadAllText(csvFile))

1 个答案:

答案 0 :(得分:0)

这是你要找的吗?

    Dim fn As String = My.Application.Info.DirectoryPath & "\HoseData.csv"
    Dim fileText As String
    Dim outString As String = TextBox1.Text & "," & TextBox2.Text & "," & """" & DateTimePicker1.Text & """" & "," & ComboBox1.Text & "," & TextBox3.Text & "," & ListBox2.Text & "," & ListBox3.Text & "," & TextBox11.Text & "," & TextBox12.Text & "," & NumericUpDown1.TextAlign & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox9.Text & "," & TextBox10.Text & "," & ListBox4.Text & "," & ListBox5.Text
    Using reader As StreamReader = File.OpenText(fn)
        fileText = reader.ReadToEnd
    End Using
    Using outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(fn, True)
        If fileText.Contains(outString) Then
            'do stuff when the string is already present, e.g.:
            MessageBox.Show("You've already entered that one.")
        Else
            'do stuff when it isn't, e.g.:
            outFile.WriteLine(outString)
        End If
    End Using