- Notifications
You must be signed in to change notification settings - Fork0
Console applications for connection to the Microsoft SQL Server, how to update rows, how to read the table and how to call the stored function and the stored procedure. Complete example collection including Docker and SQL init. Code for Java built by the Maven, for Java built by the Gradle, for C# built by the .NET Framework and for PHP.
petrfaltus/ms-sql-server-connection-source-codes
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Small example console source codes how to connect to the Microsoft SQL Server, how to update rows and how to read the table.
- clone this repository to your computer
- install theMicrosoft SQL Server (as aDocker container)
- prepare the user, the table and rows in the database
- build and run the exampleJava code
- compile and run the example.NET C# code
- run the examplePHP code
- installGIT on your computer
- clone this repository to your computer by the GIT command
git clone https://github.com/petrfaltus/ms-sql-server-connection-source-codes.git
- installdocker desktop on your computer
- refer theMicrosoft SQL Server image
The subdirectorydocker-database contains prepared Windows batches:
01-run-database.cmd- pulls the image and runs the containerat the first time02-switch-database-OFF.cmd- stops the already existing container02-switch-database-ON.cmd- starts the already existing container03-inspect-database.cmd- shows details for already existing container04-exec-connection-to-database-sa.cmd- executes thesqlcmd tool terminal into running database container (as the usersa)04-exec-connection-to-database-testuser.cmd- executes thesqlcmd tool terminal into running database container (as the usertestuser)containers.cmd- lists currently running containers and list of all existing containers
For the connection to the database use either thesqlcmd tool terminal or theMicrosoft SQL Server Management Studio
Use prepared Windows batches, every SQL command terminate by the keywordGO
SELECT @@version;GOUsersa (default passwordSyst3mAdm1n!)
Usertestuser (default passwordT3stUs3r!)
CREATEDATABASEtestdb;USE testdb;CREATE LOGIN testuser WITH PASSWORD='T3stUs3r!';CREATEUSERtestuser FOR LOGIN testuser;GRANTSELECT TO testuser;GRANT CREATE TABLE TO testuser;GRANT INSERT TO testuser;GRANTUPDATE TO testuser;GRANT ALTER TO testuser;GRANT EXECUTE TO testuser;ALTER LOGIN testuser WITH DEFAULT_DATABASE=testdb;
USE testdb;CREATETABLEanimals ( nameVARCHAR(40)NOT NULL, legs TINYINTNOT NULL, created DATETIME DEFAULT GETDATE(), updated DATETIME, remarkVARCHAR(80), idINT IDENTITY(1,1)PRIMARY KEYNOT NULL);CREATETRIGGERanimals_updateON animals AFTERUPDATEASBEGINSET NOCOUNTON;UPDATE animalsSET updated= GETDATE()WHERE idIN (SELECT idFROM Inserted)END;INSERT INTO animals (name, legs)VALUES ('chicken',2);INSERT INTO animals (name, legs)VALUES ('fox',4);INSERT INTO animals (name, legs)VALUES ('eagle',2);INSERT INTO animals (name, legs)VALUES ('ant',6);INSERT INTO animals (name, legs)VALUES ('horse',4);CREATEOR ALTER FUNCTION factorial(@nINT) RETURNSINTASBEGIN IF (@n<0) RETURN-1; DECLARE @resultINT=1; IF (@n<2) RETURN @result; DECLARE @ijkINT=2; WHILE @ijk<= @nBEGINSET @result= @result* @ijk;SET @ijk= @ijk+1; END; RETURN @result;END;CREATEOR ALTER PROCEDURE add_and_subtract(@aINT, @bINT, @xINT OUT, @yINT OUT)ASBEGINSET @x= @a+ @b;SET @y= @a- @b;END;
USE testdb;SELECT*FROM animals;SELECTcount(*)FROM animals;SELECTcount(*)FROM animalsWHERE id!=1;SELECTdbo.factorial(2);SELECTdbo.factorial(3);SELECTdbo.factorial(4);DECLARE @aINT=12;DECLARE @bINT=5;DECLARE @xINT;DECLARE @yINT;PRINT'a ='+CONVERT(VARCHAR(6), @a);PRINT'b ='+CONVERT(VARCHAR(6), @b);EXECUTEdbo.add_and_subtract @a, @b, @x OUT, @y OUT;PRINT'x ='+CONVERT(VARCHAR(6), @x);PRINT'y ='+CONVERT(VARCHAR(6), @y);
- installJava JDK on your computer
- set the OS environment
%JAVA_HOME%variable (must exist"%JAVA_HOME%\bin\java.exe")
- installApache Maven on your computer
- add the Maven directory (where the batch
mvn.cmdlocates) to the OS environment%PATH%variable
The subdirectoryjava-maven contains prepared Windows batches:
01-build.cmd- cleans, compiles and builds the Maven project02-run.cmd- runs the built Java archive (JAR)03-clean.cmd- cleans the Maven project
- installGradle Build Tool on your computer
- add the Gradle directory (where the batch
gradle.batlocates) to the OS environment%PATH%variable
The subdirectoryjava-gradle contains prepared Windows batches:
01-build.cmd- cleans, compiles and builds the Gradle project02-run.cmd- runs the built Java archive (JAR)03-clean.cmd- cleans the Gradle project
- use the
csc.exe.NET C# compiler that is the part of Microsoft .NET Framework (part of OS)
The subdirectorycsharp contains prepared Windows batches:
01-compile.cmd- compiles the source code (contains the path definition to thecsc.execompiler)02-run.cmd- runs the Windows executable03-clean.cmd- deletes the Windows executable
- installPHP on your computer
- set the OS environment
%PHP_HOME%variable (must exist"%PHP_HOME%\php.exe") - installMicrosoft ODBC Driver (or already installed as a part ofMicrosoft SQL Server Management Studio)
- installMicrosoft Drivers for PHP for SQL Server
- copy
php_pdo_sqlsrv_74_nts_x64.dllfromMicrosoft Drivers for PHP for SQL Server to the PHP directory%PHP_HOME%
To thephp.ini in the PHP directory%PHP_HOME% add lines
[PHP]extension_dir = "ext"extension=php_pdo_sqlsrv_74_nts_x64.dll[Date]date.timezone = Europe/PragueThe subdirectoryphp contains prepared Windows batch:
01-run.cmd- runs the code through the PHP interpreter
Now in August 2020 I have the computer withWindows 10 Pro 64bit,12GB RAM and available50GB free HDD space
| Tool | Version | Setting |
|---|---|---|
| GIT | 2.26.0.windows.1 | |
| docker desktop | 2.3.0.4 (46911) stable | 2 CPUs, 3GB memory, 1GB swap, 48GB disc image size |
| Microsoft SQL Server image | 2017-CU8-ubuntu (14.00.3029) | password for sa: Syst3mAdm1n! |
| Microsoft SQL Server Management Studio | 18.6 | |
| Java JDK | 14.0.1 | Java(TM) SE Runtime Environment (build 14.0.1+7) |
| Apache Maven | 3.6.3 | |
| Gradle Build Tool | 6.3 | |
| PHP | 7.4.4 | 7.4.4-nts-Win32-vc15-x64 |
| Microsoft ODBC Driver | 17.06.0001 | msodbcsql64.msi, msodbcsql17.dll (version 03.80) |
| Microsoft Drivers for PHP for SQL Server | 5.8 (5.8.0+12928) | SQLSRV58.exe |
About
Console applications for connection to the Microsoft SQL Server, how to update rows, how to read the table and how to call the stored function and the stored procedure. Complete example collection including Docker and SQL init. Code for Java built by the Maven, for Java built by the Gradle, for C# built by the .NET Framework and for PHP.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.

