{"id":15003331,"url":"https://github.com/petrfaltus/ms-sql-server-connection-source-codes","last_synced_at":"2026-01-27T13:02:50.282Z","repository":{"id":54784124,"uuid":"289835775","full_name":"petrfaltus/ms-sql-server-connection-source-codes","owner":"petrfaltus","description":"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.","archived":false,"fork":false,"pushed_at":"2021-10-12T22:28:11.000Z","size":72,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-05T01:11:39.229Z","etag":null,"topics":["batch","batchfile","client","csharp","docker","dotnet","dotnet-framework","gradle","java","maven","microsoft-sql-server","ms-sql-server","php","pl-sql","sql","sql-functions","sql-procedures"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petrfaltus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-24T05:25:20.000Z","updated_at":"2022-06-11T13:52:33.000Z","dependencies_parsed_at":"2022-08-14T02:51:26.415Z","dependency_job_id":null,"html_url":"https://github.com/petrfaltus/ms-sql-server-connection-source-codes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/petrfaltus/ms-sql-server-connection-source-codes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrfaltus%2Fms-sql-server-connection-source-codes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrfaltus%2Fms-sql-server-connection-source-codes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrfaltus%2Fms-sql-server-connection-source-codes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrfaltus%2Fms-sql-server-connection-source-codes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petrfaltus","download_url":"https://codeload.github.com/petrfaltus/ms-sql-server-connection-source-codes/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrfaltus%2Fms-sql-server-connection-source-codes/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28813226,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T12:25:15.069Z","status":"ssl_error","status_checked_at":"2026-01-27T12:25:05.297Z","response_time":168,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batch","batchfile","client","csharp","docker","dotnet","dotnet-framework","gradle","java","maven","microsoft-sql-server","ms-sql-server","php","pl-sql","sql","sql-functions","sql-procedures"],"created_at":"2024-09-24T18:58:00.667Z","updated_at":"2026-01-27T13:02:50.267Z","avatar_url":"https://github.com/petrfaltus.png","language":"Java","readme":"# Microsoft SQL Server connection source codes\nSmall example console source codes how to connect to the Microsoft SQL Server, how to update rows and how to read the table.\n\n## Running under Windows\n1. clone this repository to your computer\n2. install the **Microsoft SQL Server** (as a **Docker** container)\n3. prepare the user, the table and rows in the database\n4. build and run the example **Java** code\n5. compile and run the example **.NET C#** code\n6. run the example **PHP** code\n\n### 1. Cloning to your computer\n- install [GIT] on your computer\n- clone this repository to your computer by the GIT command\n  `git clone https://github.com/petrfaltus/ms-sql-server-connection-source-codes.git`\n\n### 2. Installation of the Microsoft SQL Server (as a Docker container)\n- install [docker desktop] on your computer\n- refer the [Microsoft SQL Server image]\n\nThe subdirectory `docker-database` contains prepared Windows batches:\n- `01-run-database.cmd` - pulls the image and runs the container **at the first time**\n- `02-switch-database-OFF.cmd` - stops the already existing container\n- `02-switch-database-ON.cmd` - starts the already existing container\n- `03-inspect-database.cmd` - shows details for already existing container\n- `04-exec-connection-to-database-sa.cmd` - executes the **sqlcmd tool** terminal into running database container (as the user *sa*)\n- `04-exec-connection-to-database-testuser.cmd` - executes the **sqlcmd tool** terminal into running database container (as the user *testuser*)\n- `containers.cmd` - lists currently running containers and list of all existing containers\n\n### 3. Preparing the database\nFor the connection to the database use either the **sqlcmd tool** terminal or the [Microsoft SQL Server Management Studio]\n\n#### Connection using sqlcmd tool\nUse prepared Windows batches, every SQL command terminate by the keyword **GO**\n```sql\nSELECT @@version;\nGO\n```\n\n#### Connection using Microsoft SQL Server Management Studio\nUser *sa* (default password *Syst3mAdm1n!*)\n\n![user sa configuration](sql.server.management.studio.sa.png)\n\nUser *testuser* (default password *T3stUs3r!*)\n\n![user testuser configuration](sql.server.management.studio.testuser.png)\n\n#### SQL lines for sa\n```sql\nCREATE DATABASE testdb;\nUSE testdb;\n\nCREATE LOGIN testuser WITH PASSWORD = 'T3stUs3r!';\nCREATE USER testuser FOR LOGIN testuser;\n\nGRANT SELECT TO testuser;\nGRANT CREATE TABLE TO testuser;\nGRANT INSERT TO testuser;\nGRANT UPDATE TO testuser;\nGRANT ALTER TO testuser;\nGRANT EXECUTE TO testuser;\n\nALTER LOGIN testuser WITH DEFAULT_DATABASE=testdb;\n```\n\n#### SQL lines for testuser\n```sql\nUSE testdb;\n\nCREATE TABLE animals (\n  name VARCHAR(40) NOT NULL,\n  legs TINYINT NOT NULL,\n  created DATETIME DEFAULT GETDATE(),\n  updated DATETIME,\n  remark VARCHAR(80),\n  id INT IDENTITY(1,1) PRIMARY KEY NOT NULL\n);\n\nCREATE TRIGGER animals_update\n  ON animals\n  AFTER UPDATE\nAS \nBEGIN\n    SET NOCOUNT ON;\n    UPDATE animals\n    SET updated = GETDATE()\n    WHERE id IN (SELECT id FROM Inserted)\nEND;\n\nINSERT INTO animals (name, legs) VALUES ('chicken', 2);\nINSERT INTO animals (name, legs) VALUES ('fox', 4);\nINSERT INTO animals (name, legs) VALUES ('eagle', 2);\nINSERT INTO animals (name, legs) VALUES ('ant', 6);\nINSERT INTO animals (name, legs) VALUES ('horse', 4);\n\nCREATE OR ALTER FUNCTION factorial(@n INT) RETURNS INT AS\nBEGIN\n  IF (@n \u003c 0)\n    RETURN -1;\n  DECLARE @result INT = 1;\n  IF (@n \u003c 2)\n    RETURN @result;\n  DECLARE @ijk INT = 2;\n  WHILE @ijk \u003c= @n\n    BEGIN\n      SET @result = @result * @ijk;\n      SET @ijk = @ijk + 1;\n    END;\n  RETURN @result;\nEND;\n\nCREATE OR ALTER PROCEDURE add_and_subtract(@a INT, @b INT, @x INT OUT, @y INT OUT) AS\nBEGIN\n  SET @x = @a + @b;\n  SET @y = @a - @b;\nEND;\n```\n\n#### optional SQL check lines for testuser\n```sql\nUSE testdb;\n\nSELECT * FROM animals;\n\nSELECT count(*) FROM animals;\nSELECT count(*) FROM animals WHERE id!=1;\n\nSELECT dbo.factorial(2);\nSELECT dbo.factorial(3);\nSELECT dbo.factorial(4);\n\nDECLARE @a INT = 12;\nDECLARE @b INT = 5;\nDECLARE @x INT;\nDECLARE @y INT;\nPRINT 'a = ' + CONVERT(VARCHAR(6), @a);\nPRINT 'b = ' + CONVERT(VARCHAR(6), @b);\nEXECUTE dbo.add_and_subtract @a, @b, @x OUT, @y OUT;\nPRINT 'x = ' + CONVERT(VARCHAR(6), @x);\nPRINT 'y = ' + CONVERT(VARCHAR(6), @y);\n```\n\n### 4. The Java client source code\n- install [Java JDK] on your computer\n- set the OS environment `%JAVA_HOME%` variable (must exist `\"%JAVA_HOME%\\bin\\java.exe\"`)\n\n#### 4.1. Apache Maven\n- install [Apache Maven] on your computer\n- add the Maven directory (where the batch `mvn.cmd` locates) to the OS environment `%PATH%` variable\n\nThe subdirectory `java-maven` contains prepared Windows batches:\n- `01-build.cmd` - cleans, compiles and builds the Maven project\n- `02-run.cmd` - runs the built Java archive (JAR)\n- `03-clean.cmd` - cleans the Maven project\n\n#### 4.2. Gradle Build Tool\n- install [Gradle Build Tool] on your computer\n- add the Gradle directory (where the batch `gradle.bat` locates) to the OS environment `%PATH%` variable\n\nThe subdirectory `java-gradle` contains prepared Windows batches:\n- `01-build.cmd` - cleans, compiles and builds the Gradle project\n- `02-run.cmd` - runs the built Java archive (JAR)\n- `03-clean.cmd` - cleans the Gradle project\n\n### 5. The .NET C# client source code\n- use the `csc.exe` .NET C# compiler that is the part of Microsoft .NET Framework (part of OS)\n\nThe subdirectory `csharp` contains prepared Windows batches:\n- `01-compile.cmd` - compiles the source code (contains the path definition to the `csc.exe` compiler)\n- `02-run.cmd` - runs the Windows executable\n- `03-clean.cmd` - deletes the Windows executable\n\n### 6. The PHP client source code\n- install [PHP] on your computer\n- set the OS environment `%PHP_HOME%` variable (must exist `\"%PHP_HOME%\\php.exe\"`)\n- install [Microsoft ODBC Driver] (or already installed as a part of [Microsoft SQL Server Management Studio])\n- install [Microsoft Drivers for PHP for SQL Server]\n- copy `php_pdo_sqlsrv_74_nts_x64.dll` from [Microsoft Drivers for PHP for SQL Server] to the PHP directory `%PHP_HOME%`\n\nTo the `php.ini` in the PHP directory `%PHP_HOME%` add lines\n```\n[PHP]\nextension_dir = \"ext\"\nextension=php_pdo_sqlsrv_74_nts_x64.dll\n\n[Date]\ndate.timezone = Europe/Prague\n```\n\nThe subdirectory `php` contains prepared Windows batch:\n- `01-run.cmd` - runs the code through the PHP interpreter\n\n## Versions\nNow in August 2020 I have the computer with **Windows 10 Pro 64bit**, **12GB RAM** and available **50GB free HDD space**\n\n| Tool | Version | Setting |\n| ------ | ------ | ------ |\n| [GIT] | 2.26.0.windows.1 | |\n| [docker desktop] | 2.3.0.4 (46911) stable | 2 CPUs, 3GB memory, 1GB swap, 48GB disc image size |\n| [Microsoft SQL Server image] | 2017-CU8-ubuntu (14.00.3029) | password for sa: Syst3mAdm1n! |\n| [Microsoft SQL Server Management Studio] | 18.6 | |\n| [Java JDK] | 14.0.1 | Java(TM) SE Runtime Environment (build 14.0.1+7) |\n| [Apache Maven] | 3.6.3 | |\n| [Gradle Build Tool] | 6.3 | |\n| [PHP] | 7.4.4 | 7.4.4-nts-Win32-vc15-x64 |\n| [Microsoft ODBC Driver] | 17.06.0001 | msodbcsql64.msi, msodbcsql17.dll (version 03.80) |\n| [Microsoft Drivers for PHP for SQL Server] | 5.8 (5.8.0+12928) | SQLSRV58.exe |\n\n## To do (my plans to the future)\n\n\n[GIT]: \u003chttps://git-scm.com/\u003e\n[docker desktop]: \u003chttps://docs.docker.com/desktop/\u003e\n[Microsoft SQL Server image]: \u003chttps://hub.docker.com/_/microsoft-mssql-server\u003e\n[Microsoft SQL Server Management Studio]: \u003chttps://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver15\u003e\n[Java JDK]: \u003chttps://www.oracle.com/java/technologies/javase-downloads.html\u003e\n[Apache Maven]: \u003chttps://maven.apache.org/\u003e\n[Gradle Build Tool]: \u003chttps://gradle.org/\u003e\n[PHP]: \u003chttps://www.php.net/\u003e\n[Microsoft ODBC Driver]: \u003chttps://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15\u003e\n[Microsoft Drivers for PHP for SQL Server]: \u003chttps://docs.microsoft.com/en-us/sql/connect/php/microsoft-php-driver-for-sql-server?view=sql-server-ver15\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrfaltus%2Fms-sql-server-connection-source-codes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetrfaltus%2Fms-sql-server-connection-source-codes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrfaltus%2Fms-sql-server-connection-source-codes/lists"}