|
1 | | -# SqlQueryTools |
| 1 | +# SqlQueryTools |
| 2 | + |
| 3 | +SqlQueryTools is a visual studio extension that generates code behind files for sql files. |
| 4 | +The code behinde file contains a static class with the same name as the sql file. |
| 5 | +That class has a const called query, with the content of the sql file. |
| 6 | +It also contains a const for every parameter that is declared in the sql file. |
| 7 | + |
| 8 | +Before SqlQueryTools start generating the code behind file it validates the content of the sql file with a SQL Server database. |
| 9 | + |
| 10 | +The generated code can be used to execute the query with an ORM like [Dapper](https://github.com/DapperLib/Dapper) |
| 11 | + |
| 12 | +### Download |
| 13 | + |
| 14 | +Download from [releases](https://github.com/gmarginet/SqlQueryTools/releases) |
| 15 | + |
| 16 | +### Getting started |
| 17 | + |
| 18 | +You can add a new sql file by right clicking on a .cs file and select 'Sql Query Tools/Add Sql File'. |
| 19 | +This will add a nested sql file below the selected csharp file. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +The following dialog will pop up, enter the filename and click 'Add file'. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +The added sql file will have the following content. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +Before you start writing your query you should set the SQL server connection string that Sql Query Tools should use to validate the query. |
| 32 | +The connection string can be set in the .csproj file you just added the sql file to. |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +Now you can start writing your sql query. |
| 37 | +You can remove all comments but if you use parameters in your query you should put the parameter declarations above the 'End Of Parameter Declaration' marker. |
| 38 | +And your actual query below the marker. |
| 39 | +The generated const string will only contain the lines below the marker, if the sql file doesn't contain a marker line all lines will be put in the const string. |
| 40 | + |
| 41 | +All comments that you keep in your sql file will not be put in the const string. |
| 42 | + |
| 43 | +Below you can find an example of a query. |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +When you save the sql file, Sql Query Tools will start generating the code behind. |
| 48 | +The progress can be found in the Sql Query Tools output panel. |
| 49 | +Here you will also find more info when something went wrong. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +And the generated code for the example query from above will look like this. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +The solution explorer will now look like this. |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +If you don't like the file suffixes or the parameter declaration marker. |
| 62 | +These can be changed via 'Tools/Options/SqlQueryTools |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +### License |
| 67 | + |
| 68 | +MIT |
0 commit comments