Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#

DbSharpApplication - C# Source Code Generator of DAL for Stored Procedure on .NET8

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
1 Feb 2024CPOL2 min read 4.1K   113   7   5
A C# source code generator that generates a stored procedure client class that enables you to call SP easily
DbSharpApplication is a C# source code generator. It generates a stored procedure client class that enables you to call stored procedure easily. DbSharpApplication also creates user defined table type classes. Latest update 2024.01.26.

Introduction

DbSharpApplication is a new version of the article, DbSharp -DAL Generator Tool on .NET6, which I posted just 10 years ago.

Last year, I decided to enhance its design. I started from scratch, eliminated redundant features, and redesigned it for ease of use.

Feature Summary

DbSharpApplication generates C# code. You set up project and add some of the references via Nuget.

Now you can easily call stored procedure like this:

C#
var sp = new Usp_Structure();
sp.BigIntColumn = 1;
//User defined table type parameter. TVP(table value parameter)
var r = new MyTableType.Record();
r.BigIntColumn = 2;
sp.StructuredColumn.Records.Add(r);
var spResult = await sp.ExecuteNonQueryAsync();

var sp1 = new Usp_SelectMultiTable();
var resultSetList = await sp1.GetResultSetsListAsync();
foreach (var item in resultSetList.ResultSetList)
{
    //Do something...
    var text = item.CharColumn;
    Console.WriteLine(item.BigIntColumn);
}

You can call easily call stored procedure from C#. Generated classes are strong typed, so you can select by intellisense.

Image 1

DbSharpApplication supports Table Value Parameter, out parameter, resultset, enum values, and so on. DbSharpApplication continues to provide the functionality of the previous version on .NET6. You can see it by this article.

DbSharp -DAL Generator Tool on .NET6

 

How to Generate and Setup Project

  1. Download DbSharpApplication.
  2. Set up connection string of database.
  3. Generate C# source code to project.
  4. Add reference from Nuget.

Download DbSharpApplication

You can download from this link.

Launch .exe

Image 2

Set Up generate setting

You can generate setting by press Add button in left panel.

Add connection string by Settings button. Window will open.

Image 3

You can add your connection string list.

These connection string will saved to file at user folder.

ex) C:\Users\Higty\HigLabo\DbSharpApplication\ConfigData.xml

Generate C# Source Code

Select connection string that you added before, button is shown.

Image 4

Press Load stored procedure button. Stored procedure list are loaded.

Image 5

Select stored procedure and press Generate button at the bottom.

Stored procedure parameters are loaded.

Image 6

Set output folder, namespace, database key. Press Generate button. C# source code will be generated to specified folder.

Image 7

If stored procedure has resultset, press Load result set button.

Image 8

Resultset is loaded.

Image 9

Add Reference from Nuget

To compile project, you must add reference of these.

  • HigLabo.Core
  • HigLabo.Data
  • HigLabo.Data.SqlServer
  • HigLabo.DbSharp
  • HigLabo.DbSharp.SqlServer

Now, you can compile the project.

I created a sample project on GitHub.

How to Use the Class

Set up DatabaseFactory class. If you set Database key "HigLabo", you pass "HigLabo" to the first parameter.

C#
StoredProcedure.DatabaseFactory.SetCreateDatabaseMethod
    ("HigLabo", () => new SqlServerDatabase("My connection string..."));

Now, you can easily call stored procedure with user defined table type.

C#
var sp = new Usp_Structure();
sp.BigIntColumn = 1;
//User defined table type parameter. TVP(table value parameter)
var r = new MyTableType.Record();
r.BigIntColumn = 2;
sp.StructuredColumn.Records.Add(r);
var spResult = await sp.ExecuteNonQueryAsync();

You can get result set.

C#
var sp1 = new Usp_SelectMultiTable();
var resultSetList = await sp1.GetResultSetsListAsync();
foreach (var item in resultSetList.ResultSetList)
{
    //Do something...
    var text = item.CharColumn;
    Console.WriteLine(item.BigIntColumn);
}

You can see the sample code at the link below:

Conclusion

DbSharpApplication is not ORM, it is a generator of DAL. Once C# code is generated, you can easily call it.

History

  • 26th January, 2024: Initial version
  • 2nd February, DbSharpApplication updated to 8.1.0.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO TinyBetter, Inc
Japan Japan
I'm a CEO of TinyBetter, Inc in Japan.

Comments and Discussions

 
QuestionSuggestion Pin
pkfox30-Jan-24 21:37
professionalpkfox30-Jan-24 21:37 
AnswerRe: Suggestion Pin
Higty1-Feb-24 16:38
Higty1-Feb-24 16:38 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA26-Jan-24 17:34
professionalȘtefan-Mihai MOGA26-Jan-24 17:34 
GeneralRe: My vote of 5 Pin
Higty27-Jan-24 21:17
Higty27-Jan-24 21:17 
SuggestionFix layout issues Pin
Akram El Assas26-Jan-24 6:28
professionalAkram El Assas26-Jan-24 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.