Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/karenpayneoregon/classes-from-sqlserver
Sample code to create classes from sql-server database
https://github.com/karenpayneoregon/classes-from-sqlserver
csharp netcore5 sqlserver windowsforms
Last synced: about 1 month ago
JSON representation
Sample code to create classes from sql-server database
- Host: GitHub
- URL: https://github.com/karenpayneoregon/classes-from-sqlserver
- Owner: karenpayneoregon
- Created: 2021-07-02T16:14:45.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-27T15:05:41.000Z (almost 2 years ago)
- Last Synced: 2024-09-29T12:01:18.673Z (about 1 month ago)
- Topics: csharp, netcore5, sqlserver, windowsforms
- Language: C#
- Homepage:
- Size: 144 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Classes from SQL-Server for C# and VB.NET
Code presented provides a developer with a tool to generate classes from a database.
An alternate is to install the following Visual Studio [extension](http://www.gui-innovations.com/visual-studio-add-ins/paste-sql-classes.html) which requires a developer to open SQL-Server Management Studio (SSMS), select a table, right click and select CREATE To Clipboard and repeat for each table.
While the code in this repository allow a developer to create classes for an entire database at the click of a button and places these classes in a folder, ready to use but recommend adding your namespace to each class.
![img](assets/classGenScreenShot.png)
Supports `VB.NET` and `C#` (written in C#)
![img](assets/Figure1.png)
Code presented here provide methods to `create concrete classes` from `SQL-Server database tables` written with Microsoft Visual Studio 2019, C#, .NET Core 5.
When use of conventional DataSet/DataTable to store data are too heavy, similarly Entity Framework is overkill creating simple classes can be a great solution.
When there are a handful of columns in a database table it's easy to hand code a class with properties to store data while a table with many columns may be cumbersome to write by hand.
Within this repository is a Windows Form project which creates concrete classes for a select database.
# How to use
* In the form load event in the project `CreateClassesFromSqlServer` change the server name if not using SQL-Express edition of SQL-Server to your server name.
```csharp
DataOperations.Server = ".\\SQLEXPRESS";
```* Set the project `CreateClassesFromSqlServer` as the startup project
* Build the project `CreateClassesFromSqlServer`
* Start the project
* Select a database in the listbox
* Click Run/Create buttonOnce clicking the Run/Create button a folder is created under the application folder named Classes, the classes are create under `Classes\DatabaseNamme`.
Example
![img](assets/generated.png)
Copy all or required classes to your project and add a namespace if so desired.
Originally written to answer a stackoverflow [question](https://stackoverflow.com/questions/68225326/how-to-create-a-models-class-from-sql-server) to Declarate each
property with a specific property attribute using a select statement found in `ClassQuery.txt`. Have changed to the code to exclude the property attribute using a
query in `ClassQueryPlain.txt`No guarantees that the code fits all situations
# Test project
The project `DataLibraryTestProject` uses one of the classes generated with the addition of editing the class and adding a namespace.
# Requires
- Visual Studio 2019 or higher
- NuGet package [System.Data.SqlClient](https://www.nuget.org/packages/System.Data.SqlClient/)# Important
In some cases the type for a property may not be known and in these cases will have `UNKNOWN` prepended to the SQL-Server type. In these cases a conversion will be needed.
For example [hierarchyid] needs to be parses using [SqlHierarchyId.Parse](https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ee642858(v=sql.105)?redirectedfrom=MSDN) via a read-only property.
![img](assets/unknown.png)
# Note
Base code from a [Stackoverflow post](https://stackoverflow.com/questions/5873170/generate-class-from-database-table) to a response for a [question](https://stackoverflow.com/questions/68225326/how-to-create-a-models-class-from-sql-server).