Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,
i just want to update only one record in a table without using where statement
i think i can do that by call recordset
this my code:

VB
Dim cs As String = ("Provider=Microsoft.ace.OLEDB.12.0; Data Source=" & Application.StartupPath & "\vbdata.accdb")
con = New OleDbConnection(cs)
con.Open()
Dim cb As String = ("update tblmoney set [amount] = '" & txt1.Text & "' , [aname] = '" & txt2.Text & "' ")
Dim cmd As New OleDbCommand(cb)
cmd.Connection = con
cmd.ExecuteReader()
con.Close()
Posted
Comments
[no name] 24-Feb-15 15:00pm    
First of all use Parameters instead of concatenating SQL - commands.
And after that I'm not safe in this stuff, but I think you should call cmd.ExecuteNonQuery() instead of ExecuteReader....Try it, maybe it helps :)
Bass Alfan 24-Feb-15 15:23pm    
how can i use Parameters?!!
can you please describe that by code?
[no name] 24-Feb-15 15:36pm    
Have a look to example inhttps://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx[^].

BTW did you tried ExecuteNonQuery instead ExecuteReader?
Sergey Alexandrovich Kryukov 24-Feb-15 15:42pm    
Good points; I answered on SQL injection crediting your comments, please see Solution 1.
I also added more specific MSDN link more specifically explaining how to prevent this exploit.
—SA
[no name] 24-Feb-15 15:46pm    
Thank you, I also allready voted a 5 :)
Bruno

1 solution

Bruno Sprecher is right: your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327[^].

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection[^].

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

—SA
 
Share this answer
 
Comments
[no name] 24-Feb-15 16:18pm    
After reading again the question after some time I added another comment for OP.

"And after reading your question again: Updating _one_ record without WHERE Statement is only possible in case the table has only one record :)"
Sergey Alexandrovich Kryukov 24-Feb-15 16:43pm    
Good point, indeed. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900