Author | Message | Time |
---|---|---|
NicoQwertyu | Well, I finally managed to make my first piece of reusable code in VB.NET, and I'd like to share it with the community. Maybe it will be useful to someone, maybe it will end up being an example of what not to do. ;D Abstract Class Database: Public MustOverride Property Table() As String Public MustOverride Property Filename() As String Public MustOverride ReadOnly Property Position() As Integer Public MustOverride ReadOnly Property Count() As Integer Public MustOverride Overloads Function GetRecord(ByVal column As String) As String Public MustOverride Overloads Function GetRecord() As String Public MustOverride Function EndOfTable() As Boolean Public MustOverride Sub GoToEnd() Public MustOverride Sub GoToBeginning() Public MustOverride Sub NextRecord() Public MustOverride Sub PreviousRecord() Class MsAccessDatabase : Inherits Database : Implements IDisposable: Constructor overloads: Public Sub New(ByVal filename As String) Public Sub New(ByVal filename As String, ByVal table As String) Public Sub New(ByVal filename As String, ByVal table As String, ByVal query As String) Public Methods: Public Overloads Sub AddTable(ByVal table As String) Public Overloads Sub AddTable(ByVal table As String, ByVal query As String) Private Methods: Private Sub SetPosition(ByVal newpos As Integer) Private Sub IncrementPosition() Private Sub DecrementPosition() Private Sub CreateConnection(ByVal filename As String) Private Sub CreateAdapter(ByVal query As String) Private Sub CreateDataset() Class SQLDatabase : Inherits Database: 'Unimplemented Class OracleDatabase : Inherits Database: 'Unimplemented Test database used: [img]http://img237.imageshack.us/img237/3406/tableste5.jpg[/img] Simple Example Usage: [code] Imports Asylum.Database Using myDatabase As MsAccessDatabase = New MsAccessDatabase("C:\Documents and Settings\Kyle\My Documents\Simple Database.mdb") myDatabase.AddTable("Users") myDatabase.AddTable("UserSettings") myDatabase.AddTable("GlobalSettings") myDatabase.Table = "Users" Do Until myDatabase.EndOfTable = True 'Manually specify a column: myDatabase.GetRecord("Username") 'Manually specify a column: myDatabase.GetRecord("Access") 'myDatabase.GetRecord() will return the record of the first column in the table 'In this example, this is the same as myDatabase.GetRecord("Username") 'DoSomething >_< Loop myDatabase.Table = "GlobalSettings" 'Etc... End Using [/code] When you change tables with the Database.Table property, Database.Position will automatically be reset to 0. Querys can be specified instead of the default "SELECT * FROM " in one of the constructor overloads. Example query usage: [code] Using myDatabase As MsAccessDatabase = New MsAccessDatabase("C:\Documents and Settings\Kyle\My Documents\Simple Database.mdb", _ "Users", "SELECT Username FROM ") '... End Using [/code] Link to database classes: [img]http://img522.imageshack.us/img522/7776/lolrarzc7.jpg[/img] [list] [o]Right click + Save as... [o]Change extension from .jpg to .rar [o]Extract files... [/list] Edit: Well, I was so excited to post this that I forgot to add ChangeRecord, and Save methods. I tried to update it to allow this functionality, but couldn't figure it out. OleDBDataAdapter.Update kept throwing exceptions. So all the classes can do in their current state is READ databases. But enjoy anyway, it was fun coding. | March 25, 2008, 2:48 AM |