Platforms to show: All Mac Windows Linux Cross-Platform
/SQL/SQLDatabaseMBS Microsoft SQL Connect and query version
Required plugins for this example: MBS SQL Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /SQL/SQLDatabaseMBS Microsoft SQL Connect and query version
This example is the version from Sat, 9th Jan 2015.
Project "SQLDatabaseMBS Microsoft SQL Connect and query version.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control server Inherits TextField
ControlInstance server Inherits TextField
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control ConnectButton Inherits PushButton
ControlInstance ConnectButton Inherits PushButton
EventHandler Sub Action()
dim con as new SQLDatabaseMBS
// connects to Microsoft SQL Server on Windows
//
// e.g. localhost\SQLEXPRESS@test
try
// connect to database
// in this example it is Microsoft SQL Server,
// but can also be Sybase, Informix, DB2
// Oracle, InterBase, SQLBase and ODBC
dim ConnectionString as string
// working connection strings in our testing:
// Windows7PC\SQLEXPRESS@test
// Windows7PC\SQLEXPRESS@
// this is via SQLServer-ODBC or OLEDB. I had to open Firewall, put in right IP and assign ports to SQL Server for TCP/IP and enable TCP/IP access!
// tcp:192.168.2.75\SQLEXPRESS,1433@test
Select case mode
case 0 // ODBC
con.Option("UseAPI") = "ODBC"
'con.Option("SQLNCLI.LIBS") = "sqlsrv32.dll" // Library included in Windows Vista and newer
'Connection String: A connection string like "[[<server_name>@][<database_name][;<driver_connection_option_list>]"
'<server_name> - connects to a specified server. If it's ommitted SQLAPI++ tries to connect to default local server instance.
'<database_name> - connects to a database with the specified name. If it's ommitted SQLAPI++ tries to connect to default database.
'<driver_connection_option_list> - SQL Server Native ODBC driver specific option list.
'To connect to a named instance of SQL Server use <server name\instance name> instead of <server_name> .
'Since SQLNCLI (SQL Server 2005) also available protocol specific server name part of the connection string:
'Shared memory: lpc:<servername>[\instancename]
'TCP/IP: tcp:<servername>[\<instancename>],<port> or tcp:<IPAddress>[\<instancename>],<port>
'Named pipes: np:\\<computer_name>\pipe\<pipename> or np:\\<IPAddress>\pipe\<pipename>
'VIA: via:<servername> [\instancename],<nic number>:<port>
ConnectionString ="SQLServer:" + server2.Text
case 1 // OLEDB
con.Option("UseAPI") = "OLEDB"
// SQLNCLI for SQL Server 2005, SQLNCLI10 for newer version.
// we leave empty for default...
'con.Option("OLEDBProvider") = "SQLNCLI"
'con.Option("OLEDBProvider") = "SQLNCLI10"
'Connection String: One of the following formats:
'"" or "@" - empty string or '@' character, connects to a default database on a local server.
'<database name> or @ <database name> - connects to a database with the specified name on your local server.
'<server name>@ - connects to a default database on the specified server.
'<server name>@<database name> - connects to a database with the specified name on the specified server.
'To connect to a named instance of SQL Server <server name\instance name> instead of <server name>.
'Since SQLNCLI (SQL Server 2005) also available protocol specific server name part of the connection string:
'Shared memory: lpc:<servername>[\instancename]
'TCP/IP: tcp:<servername>[\<instancename>],<port> or tcp:<IPAddress>[\<instancename>],<port>
'Named pipes: np:\\<computer_name>\pipe\<pipename> or np:\\<IPAddress>\pipe\<pipename>
'VIA: via:<servername> [\instancename],<nic number>:<port>
ConnectionString = "SQLServer:" +server1.Text
case 2 // DB-Library
con.Option("UseAPI") = "DB-Library"
'Connection String: One of the following formats:
'"" or "@" - empty string or '@' character, connects to a default database on a local server.
'<database name> or @ <database name> - connects to a database with the specified name on your local server.
'<server name>@ - connects to a default database on the specified server.
'<server name>@<database name> - connects to a database with the specified name on the specified server.
ConnectionString = "SQLServer:" +server.Text
case 3 // ODBC
'Connection String without = is just the name of a data source you defined.
ConnectionString = "ODBC:" +server3.Text
end Select
// server format should be:
// PcName\SqlServerInstanceName@DatabaseName
con.DatabaseName = ConnectionString
con.UserName = user.text
con.Password = password.text
con.RaiseExceptions = true // if you like to get exceptions instead of checking error property
con.Scrollable = false // disabling scrolling cursors is much faster for Microsoft SQL Server...
if con.Connect then
dim sql as string = "SELECT @@VERSION AS 'SQL Server Version';"
dim r as RecordSet = con.SQLSelect(Sql)
if r<>nil then
MsgBox r.IdxField(1).StringValue
else
MsgBox "Failed to query."
end if
// Disconnect is optional
// autodisconnect will ocur in destructor if needed
con.Close
end if
catch r as RuntimeException
MsgBox r.message
end try
End EventHandler
End Control
Control user Inherits TextField
ControlInstance user Inherits TextField
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control password Inherits TextField
ControlInstance password Inherits TextField
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control RadioAPI Inherits RadioButton
ControlInstance RadioAPI(0) Inherits RadioButton
ControlInstance RadioAPI(1) Inherits RadioButton
ControlInstance RadioAPI(2) Inherits RadioButton
ControlInstance RadioAPI(3) Inherits RadioButton
EventHandler Sub Action(index as Integer)
mode = index
End EventHandler
End Control
Control server1 Inherits TextField
ControlInstance server1 Inherits TextField
End Control
Control StaticText11 Inherits Label
ControlInstance StaticText11 Inherits Label
End Control
Control StaticText111 Inherits Label
ControlInstance StaticText111 Inherits Label
End Control
Control server2 Inherits TextField
ControlInstance server2 Inherits TextField
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control server3 Inherits TextField
ControlInstance server3 Inherits TextField
End Control
Control StaticText112 Inherits Label
ControlInstance StaticText112 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Property mode As Integer
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
End Project
See also:
- /SQL/SQLDatabaseMBS Microsoft Access Connect
- /SQL/SQLDatabaseMBS Microsoft SQL Connect
- /SQL/SQLDatabaseMBS Microsoft SQL Stored Procedure
- /SQL/SQLDatabaseMBS Microsoft SQL via ODBC
- /SQL/SQLDatabaseMBS Microsoft SQL via ODBC on Linux
- /SQL/SQLDatabaseMBS MSSQL Execute
- /SQL/SQLDatabaseMBS SQLite Create Encrypted
- /SQL/SQLDatabaseMBS SQLite custom function
- /SQL/SQLDatabaseMBS SQLite Fetch values
- /SQL/SQLDatabaseMBS SQLite load extension
The items on this page are in the following plugins: MBS SQL Plugin.