Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

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

5.00/5 (1 vote)
1 Feb 2024CPOL2 min read 5.5K   186  
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)