Skip to content

Latest commit

 

History

History
62 lines (41 loc) · 2.69 KB

quickstart.md

File metadata and controls

62 lines (41 loc) · 2.69 KB

Getting Started with DatabaseCommand

DatabaseCommand is a set of components helping .NET developers to execute SQL Queries and to retrieve data.

This C# library simplify all SQL Queries to external databases, using the base system class DbConnection. Your can use your favorites libraries for SQLServer, Oracle, SQLite, ...

DatabaseCommand is an Open Source project. Go to https://github.com/Apps72/Dev.Data to fork or improve the source code.

Quick start

  1. Add the NuGet package Apps72.Dev.Data.

  2. Create a SqlConnection, or other database connection, in your project, and call the Open() method.

var mySqlConnection = new SqlConnection("Server=MyServer;Database=Scott;");
mySqlConnection.Open();
  1. Use the DatabaseCommand methods to retrieve data (see other samples below).
using (var cmd = new DatabaseCommand(mySqlConnection))
{
    cmd.CommandText = "SELECT COUNT(*) FROM EMP";
    int count = cmd.ExecuteScalar<int>();
}

Requirements: Microsoft .NET Core 2.0 or Microsoft .NET Standard 2.0 or Microsoft Framework 4.5 (Client Profile).

Execute methods

To retrieve data from a database server, you must write a correct SQL command, possibly inject parameters, and execute this query using one of the following methods:

  • ExecuteNonQuery: Execute the query and return the count of modified rows (for INSERT, UPDATE, DELETE).
  • ExecuteScalar: Execute the query and return the first column of the first row of results (for SELECT COUNT() FROM EMP).
  • ExecuteRow: Execute the query and return a new instance of typed results filled with the first row of results
  • ExecuteTable: Execute the query and return an array of new instances of typed results filled with data table result.
  • ExecuteDataSet: Execute the query and return a list or array of new instances of typed results filled with data table results (multiple tables).

Property mapping

By default, each data table columns are mapped to C# class properties with same names: SELECT ENAME FROM EMP will be mapped to the same C# property name public string Ename { get; set; }. The mapping is case insensitive.

You can use the [Column()] attribute to define the database column name to use with another C# property.

[Column("Ename")]
public string EmployeeName { get; set;}

Live samples

Watch this video to see a complete sample to retrieve data from SQL Server.

Samples