Page 1 of 1

Check out these differences between VB(.Net) and PureBasic

Posted: Wed Jan 09, 2019 4:10 am
by RobertSF
Let's say you want to know if a database query returns something or not. You have a table of users, and you want to know if the person logged into Windows has an entry in this table of users. If yes, the person is an existing user, and if no, then the person is new user. This is a common scenario, I'm sure you will agree.

Code: Select all

' Visual Basic
Dim qry As String = "SELECT * FROM Users WHERE Login = ?;"
Dim connection As OleDbConnection = New OleDbConnection
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Desktop\pool.accdb"
Dim cmd As OleDbCommand = New OleDbCommand(qry, connection)
cmd.Connection.Open()
Dim par As New OleDbParameter
par.Value = Environ("UserName")
cmd.Parameters.Add(par)
Dim result As Object = cmd.ExecuteScalar
cmd.Connection.Close()
If result Is Nothing Then
    Stop
End If

Code: Select all

; PureBasic
Dim qry.s = "SELECT * FROM Users WHERE Login = ?;"
Dim db.i = 1
Dim result.b
UseODBC()
OpenDatabase(db, "dbPool")
SetDatabaseString(db, 0, UserName())
QueryDatabase(db, qry)
result = NextDatabaseRow(db)
CloseDatabase(db)
If result = 0
  CallDebugger
EndIf
There is actually not a big difference in the number of lines, but it's clear that the PureBasic code is more readable.

(Can you simplify the VB code? What I'm posting is the best of my efforts.)

But just look, to set a parameter. In PureBasic, you choose whether it's string or numeric and use the appropriate function -- SetDatabase[String/Long](internal database number, ordinal number of the parameter within the collection of parameters, parameter value). That's it.

In VB, you have to create an object of type Parameter, set the value of the parameter, and then add the parameter to the collection of parameters!

Re: Check out these differences between VB(.Net) and PureBas

Posted: Wed Jan 09, 2019 8:26 am
by Kiffi
RobertSF wrote:But just look, to set a parameter. In PureBasic, you choose whether it's string or numeric and use the appropriate function -- SetDatabase[String/Long](internal database number, ordinal number of the parameter within the collection of parameters, parameter value). That's it.

In VB, you have to create an object of type Parameter, set the value of the parameter, and then add the parameter to the collection of parameters!
not really. This is my attempt using AddWithValue():

Code: Select all

Dim Query As String = "SELECT * FROM Users WHERE Login = @Login"

Using CN As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Desktop\pool.accdb")
	CN.Open()
	Using CMD As OleDbCommand = CN.CreateCommand
		CMD.CommandText = Query
		CMD.Parameters.AddWithValue("@Login", Environ("UserName"))
		Dim result As Object = CMD.ExecuteScalar
		If result Is Nothing Then
			Stop
		End If
	End Using
	CN.Close()
End Using
Greetings ... Peter

Re: Check out these differences between VB(.Net) and PureBas

Posted: Wed Jan 09, 2019 9:58 am
by Josh
Did you ever test your code before publishing? I don't think so.
RobertSF wrote:

Code: Select all

; PureBasic
Dim qry.s = "SELECT * FROM Users WHERE Login = ?;"
Dim db.i = 1
Dim result.b

Re: Check out these differences between VB(.Net) and PureBas

Posted: Thu Jan 24, 2019 4:35 am
by RobertSF
Josh wrote:Did you ever test your code before publishing? I don't think so.
Oops, you caught me. I guess that makes my point wrong as well. Microsoft indeed is better than PureBasic. Yay!

Re: Check out these differences between VB(.Net) and PureBas

Posted: Thu Jan 24, 2019 4:37 am
by RobertSF
Kiffi wrote:
RobertSF wrote:But just look, to set a parameter. In PureBasic, you choose whether it's string or numeric and use the appropriate function -- SetDatabase[String/Long](internal database number, ordinal number of the parameter within the collection of parameters, parameter value). That's it.

In VB, you have to create an object of type Parameter, set the value of the parameter, and then add the parameter to the collection of parameters!
not really. This is my attempt using AddWithValue():

Code: Select all

Dim Query As String = "SELECT * FROM Users WHERE Login = @Login"

Using CN As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Desktop\pool.accdb")
	CN.Open()
	Using CMD As OleDbCommand = CN.CreateCommand
		CMD.CommandText = Query
		CMD.Parameters.AddWithValue("@Login", Environ("UserName"))
		Dim result As Object = CMD.ExecuteScalar
		If result Is Nothing Then
			Stop
		End If
	End Using
	CN.Close()
End Using
Greetings ... Peter
Greetings. That doesn't exactly make VB more readable, though, does it?

Re: Check out these differences between VB(.Net) and PureBas

Posted: Thu Jan 24, 2019 7:54 am
by Kiffi
RobertSF wrote:That doesn't exactly make VB more readable, though, does it?
It's a matter of taste.