Wednesday, May 27, 2009

Continued

As an addition to the last post, I'd like to point out that certain SQL commands (ones that do not return any information) can be executed very simply from VB. For example, to delete a record, one simply needs the following code:


dim cn as adodb.connection
cn.ConnectionString = "Provider=vfpoledb;" & _
"Data Source=C:\ab\dname.dbf;" & _
"Mode=Read|Share Deny None;" & _
"Password=''"
cn.open("DELETE * FROM name")
cn.close
cn = nothing

Here we have executed a command that will delete everything from the "name" table. Whether or not the SQL command is correct, i do not know (as i haven't tested it) but that's not the important part. The rest of the code is what matters here. SQL commands themselves vary from database to database so you must do your own checking of the syntax of the command. For example, when writing to an MS Access db one must enclose the table name (and column names) in brackets (ie. [name] refers to the table named "name"). Using the foxpro db in the last example, the brackets were not necessary. The point is, the SQL command syntax varies and this example is more to show you how to execute an SQL command and less about SQL command syntax.
This type of code can be used for Delete, Insert and Update commands, as they do not return any result and therefore they do not need anywhere to store retrieved data.
However, any commands that return data require a recordset or a dataadapter to read from the table. Then, one can do what they please with the information that has been retrieved. Ill post more about this later, though I've already briefly touched on recordsets in earlier posts.