Doug said:
I'm working in VB.net with an ADOBD connection to an Access
database. With a SAVE button click I want to be able to check if
there is an instance of the file already in the database before it is
saved in the database. How can I do this? I've played around
with this and can not figure it out. I've tried to diplay the results in
a DataGrid, if there is such an instance, but yet again I can not get it
to work properly. Please can someone help. I'am new to all this,
and enjoy playing around with this language.
I'm not exactly sure what you're asking. When you press a Save button,
you want to store a string containing a filename to your database? And you
want to see if it's already there before you add it?
If you just want a SQL query to check, it's probably better to avoid
complicated classes like the DataSet and DataGrid. There are some
less-visible but sometimes more-useful methods of data objects that are
lighter weight, faster, and easier to use. Here's the code I would try:
Dim myConnection As OleDbConnection
myConnection = New OleDbConnection("Insert Connection String Here")
myConnection.Open()
Dim queryString As String
queryString = "SELECT COUNT(*) FROM MyTable" & _
" WHERE FileName='" & [Whatever].ToString.Replace("'", "''")
& "'"
Dim myCommand As OleDbCommand
myCommand = New OleDbCommand(queryString, myConnection)
' ExecuteScalar returns the first column of the first row of the query,
' for situations just like this where there's only one value we care about.
Dim matchingRows As Integer = CInt(myCommand.ExecuteScalar)
myConnection.Close()
If matchingRows = 0 Then
' Add the row to the database
End If
If this isn't the problem you're having, let me know and I'll see what I
can do!
Jeremy