Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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.

NotificationsYou must be signed in to change notification settings

petrfaltus/ms-sql-server-connection-source-codes

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.

Running under Windows

  1. clone this repository to your computer
  2. install theMicrosoft SQL Server (as aDocker container)
  3. prepare the user, the table and rows in the database
  4. build and run the exampleJava code
  5. compile and run the example.NET C# code
  6. run the examplePHP code

1. Cloning to your computer

  • installGIT on your computer
  • clone this repository to your computer by the GIT commandgit clone https://github.com/petrfaltus/ms-sql-server-connection-source-codes.git

2. Installation of the Microsoft SQL Server (as a Docker container)

The subdirectorydocker-database contains prepared Windows batches:

  • 01-run-database.cmd - pulls the image and runs the containerat the first time
  • 02-switch-database-OFF.cmd - stops the already existing container
  • 02-switch-database-ON.cmd - starts the already existing container
  • 03-inspect-database.cmd - shows details for already existing container
  • 04-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

3. Preparing the database

For the connection to the database use either thesqlcmd tool terminal or theMicrosoft SQL Server Management Studio

Connection using sqlcmd tool

Use prepared Windows batches, every SQL command terminate by the keywordGO

SELECT @@version;GO

Connection using Microsoft SQL Server Management Studio

Usersa (default passwordSyst3mAdm1n!)

user sa configuration

Usertestuser (default passwordT3stUs3r!)

user testuser configuration

SQL lines for sa

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;

SQL lines for testuser

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;

optional SQL check lines for testuser

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);

4. The Java client source code

  • installJava JDK on your computer
  • set the OS environment%JAVA_HOME% variable (must exist"%JAVA_HOME%\bin\java.exe")

4.1. Apache Maven

  • installApache Maven on your computer
  • add the Maven directory (where the batchmvn.cmd locates) to the OS environment%PATH% variable

The subdirectoryjava-maven contains prepared Windows batches:

  • 01-build.cmd - cleans, compiles and builds the Maven project
  • 02-run.cmd - runs the built Java archive (JAR)
  • 03-clean.cmd - cleans the Maven project

4.2. Gradle Build Tool

  • installGradle Build Tool on your computer
  • add the Gradle directory (where the batchgradle.bat locates) to the OS environment%PATH% variable

The subdirectoryjava-gradle contains prepared Windows batches:

  • 01-build.cmd - cleans, compiles and builds the Gradle project
  • 02-run.cmd - runs the built Java archive (JAR)
  • 03-clean.cmd - cleans the Gradle project

5. The .NET C# client source code

  • use thecsc.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.exe compiler)
  • 02-run.cmd - runs the Windows executable
  • 03-clean.cmd - deletes the Windows executable

6. The PHP client source code

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/Prague

The subdirectoryphp contains prepared Windows batch:

  • 01-run.cmd - runs the code through the PHP interpreter

Versions

Now in August 2020 I have the computer withWindows 10 Pro 64bit,12GB RAM and available50GB free HDD space

ToolVersionSetting
GIT2.26.0.windows.1
docker desktop2.3.0.4 (46911) stable2 CPUs, 3GB memory, 1GB swap, 48GB disc image size
Microsoft SQL Server image2017-CU8-ubuntu (14.00.3029)password for sa: Syst3mAdm1n!
Microsoft SQL Server Management Studio18.6
Java JDK14.0.1Java(TM) SE Runtime Environment (build 14.0.1+7)
Apache Maven3.6.3
Gradle Build Tool6.3
PHP7.4.47.4.4-nts-Win32-vc15-x64
Microsoft ODBC Driver17.06.0001msodbcsql64.msi, msodbcsql17.dll (version 03.80)
Microsoft Drivers for PHP for SQL Server5.8 (5.8.0+12928)SQLSRV58.exe

To do (my plans to the future)

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp