|
-
Jan 30th, 2008, 07:00 PM
#1
Thread Starter
New Member
VB keeps clearing my database
I am trying to write a simple VB 2008 program that adds rows to, and retreives rows from, an MS Access 2003 database. Although both code pieces work fine:
Code:
Public Function exists(ByVal filename)
Dim oConn As OleDbConnection
Dim oComm As OleDbCommand
Dim oConnect, oQuery As String
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\dlmods.mdb;User ID=;Password="
oQuery = "SELECT * FROM mods"
oConn = New OleDbConnection(oConnect)
oComm = New OleDbCommand(oQuery, oConn)
oConn.Open()
Dim dbread = oComm.ExecuteReader()
MsgBox(dbread.HasRows)
dbread.Read()
MsgBox(dbread.GetString(1))
oConn.Close()
Return True
End Function
(This function clearly doesn't do what it's supposed to yet i.e. say if a given item exists in the db, but I don't think that matters).
Code:
Public Function AddRow(ByVal mainmodfile, ByVal title, ByVal name, ByVal name2, ByVal dlmodfile)
'disable the big button that runs this, make this take parameters, have this run when a mod is installed, check when loading files whether they are in the database or not and act accordingly
Dim oConn As OleDbConnection
Dim oComm As OleDbCommand
Dim oConnect, oQuery As String
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\dlmods.mdb;User ID=;Password="
name2 = name2.Replace("'", "'")
'TODO ADD PROPER INPUT CHECKING
'TODO CHECK FOR DUPLICATES
oQuery = "INSERT INTO mods(mainmodfile,title,name,name2,dlmodfile) VALUES('" & mainmodfile & "','" & title & "','" & name & "','" & name2 & "','" & dlmodfile & "')"
oConn = New OleDbConnection(oConnect)
oComm = New OleDbCommand(oQuery, oConn)
Try
oConn.Open()
oComm.ExecuteNonQuery()
oConn.Close()
Catch ex As OleDb.OleDbException
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
Return False
Finally
If Not (oConn Is Nothing) Then
oConn.Dispose()
oConn = Nothing
End If
If Not (oComm Is Nothing) Then
oComm.Dispose()
oComm = Nothing
End If
End Try
Return True
End Function
(The stuff at the bottom probably ought to be added to the first code piece too, although I don't really know what it's there for.
These snippets were copied off the web and work fine, however - whenever I change code in my program which relates to the database, the entire contents thereof get wiped. For example, I made the program fill the database with 3 lines, then changed "oConn.Open()" to "oConn.Opn()" in the exists function, clicked away, then went back and changed it again - and all the contents of the database had gone.
The database is located in the Debug folder of the project, and it is included in the project's files so that it gets written to the output folder (I don't yet know if this setup will work). It was at one point added as a data connection, but that has now been removed.
Any suggestions for what is going on here are greatly appreciated (as are suggestions for which particular database access method I should be using).
Last edited by aphw; Jan 30th, 2008 at 07:04 PM.
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
|