First Use

Edit this page on GitHub

Add Using

Add using Norm directive to the using section of your source file:

using Norm;

//... the rest of the source code ...
using Norm;

//... the rest of the source code ...

Or, add global using Norm to use Norm in all of your source files (C# 10 or higher only):

global using Norm;

//... the rest of the source code ...
global using Norm;

//... the rest of the source code ...

Connection Reference

Obtain a database connection reference, either by creating a new instance:

using Norm;

// For PostgeSQL
using var connection = new NpgsqlConnection("Server=localhost;Database=dvdrental;Port=5432;User Id=postgres;Password=postgres;");

// For SQL Server
using var connection = new SqlConnection("Data Source=localhost;Initial Catalog=dvdrental;Integrated Security=True");

// etc ...
using Norm;

// For PostgeSQL
using var connection = new NpgsqlConnection("Server=localhost;Database=dvdrental;Port=5432;User Id=postgres;Password=postgres;");

// For SQL Server
using var connection = new SqlConnection("Data Source=localhost;Initial Catalog=dvdrental;Integrated Security=True");

// etc ...

Or, pass it down as a method parameter:

using Norm;
using System.Data.Common;

public int CountActors(DbConnection connection)
{
    //...
}
using Norm;
using System.Data.Common;

public int CountActors(DbConnection connection)
{
    //...
}

Or, get it from the database EF context:

using Norm;

public int CountActors(MyDatabaseContext context)
{
    var connection = context.Database.GetDbConnection()
    //...
}
using Norm;

public int CountActors(MyDatabaseContext context)
{
    var connection = context.Database.GetDbConnection()
    //...
}

Note: Connection must be descendant of System.Data.Common.DbConnection type.


First Commands

Execute count on table actor and return the result:

using Norm;
using System.Linq;
using System.Data.Common;

public int CountActors(DbConnection connection)
{
    return connection.Read<int>("select count(*) from actor").Single();
}
using Norm;
using System.Linq;
using System.Data.Common;

public int CountActors(DbConnection connection)
{
    return connection.Read<int>("select count(*) from actor").Single();
}

Or, perhaps, execute a command without returning a value:

// delete inactive customers
connection.Execute("delete from customer where active = 0");
// delete inactive customers
connection.Execute("delete from customer where active = 0");

Note: this documentation will use a sample database from a PostgreSQL public tutorial (postgresqltutorial.com). You can find detailed instructions on installing this sample database on this page.

Comments