-
Apr 18th, 2021, 06:20 AM
#1
Thread Starter
Lively Member
[RESOLVED] Insert records in Ms Access Database sql error
I am trying to add records to a table of my access database.
When I create the SQL string I get an error. I am not sure what is the problem but I am guessing the path.
Code:
Private Sub AddRecords(sPath As String)
Dim SQLstr As String
Dim Command As OleDbCommand
Dim icount As Integer
Dim di As New DirectoryInfo(sPath)
' Get a reference to each file in that directory.
Dim fiArr As FileInfo() = di.GetFiles()
' Display the names of the files.
Dim fri As FileInfo
Try
Dim sName As String
Dim sLastAcc As String
Dim sType As String
Dim Size As String
For Each fri In fiArr
sType = Path.GetExtension(fri.Name).ToLower()
sLastAcc = fri.LastAccessTime
Size = FileLen(sPath & fri.Name)
sName = UCase(fri.Name)
SQLstr = "INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES '" & sPath & "','" & sType & "','" & sLastAcc & "','" & Size & "','" & sName & "','" & sName & "'"
Command = New OleDbCommand(SQLstr, con)
icount = Command.ExecuteNonQuery
Next fri
Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try
End Sub
This is the SQLstr that creates the error:
Code:
INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES 'C:\INFORMATIE OPSLAG\2021\','.pdf','17-4-2021 14:29:02','137992','MY.FILE.IN.DIRECTORY.20210414.PDF','MY.FILE.IN.DIRECTORY.20210414.PDF'
-
Apr 18th, 2021, 07:11 AM
#2
Re: Insert records in Ms Access Database sql error
Firstly, what error? Secondly, don't use string concatenation to build SQL code in the first place. Always use parameters. If you do that then it will likely fix your problem. Follow the Blog link in my signature and check out my thread on using parameters in ADO.NET to learn how.
-
Apr 18th, 2021, 01:01 PM
#3
Thread Starter
Lively Member
Re: Insert records in Ms Access Database sql error
I solved it. An error in the sql string.
Code:
SQLstr = "INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES ('" & sPath & "','" & sType & "','" & sLastAcc & "','" & Size & "','" & sName & "','" & sName & "')"
-
Apr 18th, 2021, 07:46 PM
#4
Re: [RESOLVED] Insert records in Ms Access Database sql error
That is a bad solution. If you were doing it the right way in the first place, i.e. using parameters, then your code would be far more readable and you'd be less likely to make mistakes like that at all. Don't be lazy and keep doing it the wrong way because it would take effort to learn to do it the right way. Learn the right way now and do it the right way hereafter. It will save you time and effort in the long run.
-
Apr 19th, 2021, 12:57 AM
#5
Re: Insert records in Ms Access Database sql error
Originally Posted by clausowitz
I solved it. An error in the sql string.
Code:
SQLstr = "INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES ('" & sPath & "','" & sType & "','" & sLastAcc & "','" & Size & "','" & sName & "','" & sName & "')"
you should take the advice from Post#2 = Always use parameters.
it really is a small change, something like this
Code:
'Create the command.
Dim insertSQL As String = "INSERT INTO Documents "
insertSQL &= "([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName])"
insertSQL &= "VALUES (?,?,?,?,?,?)"
Dim cmd As New OleDb.OleDbCommand(insertSQL, Con)
cmd.Parameters.AddWithValue("Document Location", sPath)
cmd.Parameters.AddWithValue("Document Type", sType)
'add the others
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 19th, 2021, 01:31 AM
#6
Re: Insert records in Ms Access Database sql error
Originally Posted by ChrisE
you should take the advice from Post#2 = Always use parameters.
it really is a small change, something like this
Code:
'Create the command.
Dim insertSQL As String = "INSERT INTO Documents "
insertSQL &= "([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName])"
insertSQL &= "VALUES (?,?,?,?,?,?)"
Dim cmd As New OleDb.OleDbCommand(insertSQL, Con)
cmd.Parameters.AddWithValue("Document Location", sPath)
cmd.Parameters.AddWithValue("Document Type", sType)
'add the others
No need for any concatenation to build the SQL. If you don't like a long(ish) SQL statement in one line then use a multiline String literal, which VB has supported for a while now (since 2017 or maybe 2015, I think). Before that, XML literals were the best option.
-
Apr 20th, 2021, 11:05 AM
#7
Thread Starter
Lively Member
Re: [RESOLVED] Insert records in Ms Access Database sql error
I changed my code an used the Parameters. Only question remains why the use of parameters is better?!
-
Apr 20th, 2021, 01:56 PM
#8
Re: [RESOLVED] Insert records in Ms Access Database sql error
Using Parameters helps avoid many problems, including the kind of thing you experienced here, data type issues, security issues, and more.
For further explanation, see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of the Database Development forum).
-
Apr 20th, 2021, 02:04 PM
#9
Re: [RESOLVED] Insert records in Ms Access Database sql error
Of course the most famous reason to use parameters is of course the story of Little Bobby Drop Tables;
-tg
-
Apr 20th, 2021, 07:55 PM
#10
Re: [RESOLVED] Insert records in Ms Access Database sql error
Originally Posted by clausowitz
Only question remains why the use of parameters is better?!
Why would that question remain when I specifically explain the answer in the blog post I directed you to multiple times? It's almost like you were too lazy to look at it all to begin with and then, when you did look, you were too lazy top read it properly. If you ask for help and people provide it, maybe use it.
Tags for this Thread
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
|