SqlCRUDRepository 1.0.27

dotnet add package SqlCRUDRepository --version 1.0.27                
NuGet\Install-Package SqlCRUDRepository -Version 1.0.27                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SqlCRUDRepository" Version="1.0.27" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SqlCRUDRepository --version 1.0.27                
#r "nuget: SqlCRUDRepository, 1.0.27"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SqlCRUDRepository as a Cake Addin
#addin nuget:?package=SqlCRUDRepository&version=1.0.27

// Install SqlCRUDRepository as a Cake Tool
#tool nuget:?package=SqlCRUDRepository&version=1.0.27                

SqlCRUDRepository

SqlCRUDRepository is a .net library To perform read, insert, edit and delete operations on SQL Server database tables.

This project was developed for Microsoft SQL Server. You can extend this project for other databases.


Samples

1. How to create a new record in a table?

Suppose you have a table called tblCustomer in a database.

Create Table tblCustomer (ID int Identity (1,1), FirstName nvarchar (max), LastName nvarchar (max))

we create a model from this table.(In later sections we will change the class name so that it is not the same as the table name)

public class tblCustomer
{
    public Int32 ID {get; set; }
    public string FirstName {get; set; }
    public string LastName {get; set; }
}

then save it!

var Sampledb = Repository.RepositoryFactory.CreateRepository (ConnectionString);
tblCustomer customer = new tblCustomer ();
customer.FirstName = "FirstNameCustomer";
customer.LastName = "LastNameCustomer";
Sampledb.Save (customer);
Console.WriteLine (customer.ID);
  • You can use IDbConnection instead of ConnectionString for added security.
  • After saving in the table, an ID will be given to the customer.

2. How to find a customer with an ID and change its fields?

        var customerdb = Sampledb.GetByID <tblCustomer> (customer.ID.ToString (), false);
        customerdb.FirstName = "ChangedFirstNameCustomer";
        Sampledb.Save (customerdb);
  • Note: properties with null value ignored in SQl update statment .

    for example.

          tblCustomer customer = new tblCustomer ();
          customer.ID=3;
          customer.FirstName = "ChangedFirstNameCustomer";
          Sampledb.Save (customer);
    

    Update tblCustomer without change LastName field in database. (LastName is nullable and has value null in sample code)

3. How to remove customer from database?

Sampledb.Delete (customerdb); // you can also use DeleteList(batch delete) In latest version.

4. How do I use transactions?

        Sampledb.BeginTransaction ();
        tblCustomer customer = new tblCustomer ();
        customer.FirstName = "FirstNameCustomer";
        customer.LastName = "LastNameCustomer";
        Sampledb.Save (customer);
        var customerdb = Sampledb.GetByID <tblCustomer> (customer.ID.ToString (), false);
        customerdb.FirstName = "ChangedFirstNameCustomer";
        Sampledb.Save (customerdb);
        Sampledb.Delete (customerdb);
        Sampledb.CommitTransaction ();

5. How do I change the model name?

[Repository.Domain.TableInfo ("tblCustomer", "ID", true)]
public class Customer
{
    public Int32 ID {get; set; }
    public string FirstName {get; set; }
    public string LastName {get; set; }
}
  • In this section you can create a read-only or write-only table.

6. Can the name of the class property be different from the name of the fields in my table?

Yes. Using [Repository.Domain.FieldInfo ("FirstName", false)]

[Repository.Domain.TableInfo ("tblCustomer", "ID", true)]
public class Customer
{
    public Int32 ID {get; set; }
    [Repository.Domain.FieldInfo ("FirstName", false)]
    public string FirstName {get; set; }
    public string LastName {get; set; }
}

7. What can be done for properties that exist in the class but do not exist in the database?

    [Repository.Domain.FieldInfo ("CustomProperty", true)]
    public int CustomProperty {get; set; }

8. How do I call a StoredProcedure?

var result = Sampledb.Execute_StoredProcedure <Customer> (spName, null);

  • Up to 10 ResultSet can be obtained with one call.

9. How can I find specific values ​​in a table? For example, all customers whose name is equal to a certain value?

        string Filter = "FirstName = @ FirstName";
        Dictionary <string, string> parameters = new Dictionary <string, string> ();
        parameters.Add ("FirstName", "FirstNameCustomer");
        var result = Sampledb.Find <Customer> (Filter, "ID", false, parameters, "");
        Console.WriteLine (result.Count);
  • In this section you can both sort and name the fields you just want so that only those fields are provided for you.

  • In the Find and GetByID methods, a parameter called withLock is considered, which can also be used to lock table records when reading. In cases where there is a transaction, this can be useful to make the synchronization controllable.

10. Can we give the ID generation mechanism ourselves, for example not the Identity table?

Yes. To do this, you must first change the model(Both database and code).

Create Table tblCustomer2(ID nvarchar(500), FirstName nvarchar(max), CustomerLastName nvarchar(max),Description nvarchar(max))

[Repository.Domain.TableInfo("tblCustomer2","ID",false)]
public class Customer
{
    public string ID { get; set; }
    [Repository.Domain.FieldInfo("FirstName",false)]
    public string FirstName { get; set; }
    [Repository.Domain.FieldInfo("CustomerLastName", false)]
    public string LastName { get; set; }

    [Repository.Domain.FieldInfo("CustomProperty", true)]
    public int CustomProperty { get; set; }
}

There are two mechanisms for creating an ID

  1. In the program code (this model is available in version 1.0.14 and above)

    You must have a function to create an ID.

    For example

     private string keygenerator(string ClassName)
     {
         return Guid.NewGuid().ToString();
     }
    

    And at the end

    Sampledb.__ KeyGenerator = keygenerator;

  2. In the database (default)

    There is a stored procedure in the database called __KeyGenerator that must be overwritten.

11. Is it possible to update in batches?

Yes.

For example This command updates the first name column of all records whose ID is not 2.

   Customer o = new Customer();
   o.FirstName = "a";
   Sampledb.Save(o,"ID <> 2");

12. How can I find the first record matching the filter?

        string Filter = "FirstName = @FirstName";
        Dictionary <string, string> parameters = new Dictionary <string, string> ();
        parameters.Add ("FirstName", "FirstNameCustomer");
        var result = Sampledb.FindFirst <Customer> (Filter, "ID", false, parameters, "");

13. Is it possible to insert in a batch?

Yes.

For example

        List<Customer> lst = new List<Customer>();
        for (int i = 0; i < 10000; i++)
        {
            Customer o2 = new Customer();
            o2.FirstName = body.FirstName;
            o2.LastName = body.LastName;
            lst.Add(o2);
        }
        Sampledb.SaveList(lst, "ID");
        return "1";

Note: If the record exists, it will update it, if it does not exist, it will insert it.

Note: You can specify multiple fields to check, for example: "ID, FirstName".

14. Is it possible to set command timeout?

Yes.

For example

        Dictionary<string, string> parameters = new Dictionary<string, string>();
        parameters.Add("FirstName", body.FirstName);
        //you can not use writeonly object in execute procedure
        var result= Sampledb.Execute_StoredProcedure<Customer,Customer,Customer2,ReadonlyCustomer>("SP_GetCustomerDetail", parameters,100);
        return (List<Customer>)result.First();

Note: You can not use writeonly object in execute procedure.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.27 265 10/29/2023
1.0.26 388 1/21/2023
1.0.25 329 1/17/2023
1.0.23 421 10/31/2022
1.0.22 454 10/8/2022
1.0.21 452 8/27/2022
1.0.19 444 8/15/2022
1.0.18 520 2/19/2022
1.0.17 493 1/30/2022
1.0.16 507 1/30/2022
1.0.15 497 1/26/2022
1.0.14 326 1/8/2022
1.0.13 352 12/15/2021
1.0.12 400 10/31/2021
1.0.11 395 9/5/2021
1.0.10 411 8/29/2021
1.0.9 376 8/28/2021
1.0.6 388 6/16/2021
1.0.5 348 6/13/2021
1.0.4 370 6/8/2021

1.0.27: Improvments :
Added CommandTimeout to All Methods.
1.0.26: Improvments :
Adding a optional parameter to the Save method when we want to do a batch update using parameterization.
Adding a capability to SaveList function to handle multiple column names to check.

1.0.25: Fix Bug SaveList function for batch insert or update in transaction.

1.0.24: Added SaveList function for batch insert or update.

1.0.23: Running StoredProcedure inside a Transaction close the connection.

1.0.22: add non static version of RepositoryFactory(RepositoryFactory2).

1.0.21: Fix Bug FindFirst.

1.0.20: Improved performance of FindFirst function by adding Top

1.0.19: Batch Update.Added parameter to Save function to IRepository with Filter(Update all recorde in database match with specific filter).

1.0.18: GUID Column support For primary key.

1.0.17: Added Connection TimeOut For StoredProcedure.

1.0.16: Added Connection TimeOut.

1.0.15: Added DeleteList function to IRepository with Filter(Delete all recorde in database match with specific filter).

1.0.14: Added ID Generation mechanism in Code.

1.0.13: Added Transaction Count property to IRepository interface.

1.0.12: When you use special characters in the name of properties in objects such as Chinese characters!
Add brackets for the name of the properties of each object to prevent errors when query(SELECT ...  FROM TABLE) the database.

1.0.11: Add new constructor with IDbConnection parameter.