|
-
Dec 28th, 2014, 09:17 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] textfile as database
Hi,
I'm writing a simple program. The program will save 2 values, Website title and website URL. I normally use a database to store data, however this time if possible I would like to use a simple textfile.
I was wondering how I should initially save the data to the textfile to later recognize the website title and url.
Any advice is appriciated
Thanks
Last edited by met0555; Dec 28th, 2014 at 09:25 PM.
-
Dec 28th, 2014, 09:44 PM
#2
Re: textfile as database
One way is a CSV file where each line in consist of the web site and the url separated by a comma.
If you want a way that requires less coding, but is also less efficient data storage, put all of the values into a datatable and call the table's WriteXML method. If you have multiple tables, I think the DataSet object also has a Read/Write XML methods.
kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Dec 28th, 2014, 10:10 PM
#3
Re: textfile as database
 Originally Posted by met0555
I would like to use a simple textfile.
There are probably 100 different ways to do it, for just two string variables I probably wouldn't bother getting too fancy and just use some basic stuff , something like this,,
Code:
Public Class Form1
' store title & url.
Private websiteURL As String
Private websiteTitle As String
' path & filename where data is stored
Private websiteFilename As String = IO.Path.Combine(Application.StartupPath, "website.txt")
' Test Data Load:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' get saved data, returns true if data file found.
If LoadWebsiteData() Then ' show results in msgbox
MessageBox.Show(websiteURL, websiteTitle)
Else
MessageBox.Show("No data file found")
End If
End Sub
' Test Data Save:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' Set variables to something
websiteTitle = "Visual Basic .NET"
websiteURL = "http://www.vbforums.com/forumdisplay.php?25-Visual-Basic-NET"
' save the data, returns false if file save failed!
If SaveWebsiteData() Then
MessageBox.Show("File saved successfully!")
End If
End Sub
Private Function LoadWebsiteData() As Boolean
If IO.File.Exists(websiteFilename) Then
Dim s() As String = IO.File.ReadAllText(websiteFilename).Split(CChar(vbTab))
websiteTitle = s(0)
websiteURL = s(1)
Return True
End If
Return False
End Function
Private Function SaveWebsiteData() As Boolean
Try
IO.File.WriteAllText(websiteFilename, websiteTitle & vbTab & websiteURL)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
Return True
End Function
End Class
-
Dec 30th, 2014, 12:17 PM
#4
Thread Starter
Frenzied Member
Re: textfile as database
@kebo and @Edgemeal
Thanks for your tips and sample code. I ended up using an access database since I realized i needed to do some SQL query "Select Distinct....", if I try to do that in a text file it may get messy.
But thanks for the help I will keep your suggestions in mind for next time.
-
Dec 30th, 2014, 12:46 PM
#5
Hyperactive Member
Re: [RESOLVED] textfile as database
Just to let you know that certain text files (eg CSV, XML, etc.) can be queried just like a database using ADO.Net... As long as you are using SELECT queries, you can still use the text files as your data-store. If you are planning to run UPDATE queries, however, then an actual database is easier...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|