GithubHelp home page GithubHelp logo

zspitz / stringassql Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 1.0 60 KB

Run SQL statements against ADO.NET providers without the distraction of ADO.NET objects

License: MIT License

C# 100.00%
ado-net sql oledb

stringassql's Introduction

StringAsSql

AppVeyor NuGet

This library enables you to use SQL statements without a mess of multiple objects and using blocks. For example, you can write this:

// setup ConnectionFactory -- not shown
// optionally, setup ParameterNameBuilder -- not shown

List<Person> persons = "SELECT * FROM Persons WHERE LastName LIKE ? + '%'".AsSql("An").ToList<Person>();

instead of this:

var persons = new List<Person>();
using (var conn = new OleDbConnection(connectionString)) {
    conn.Open();
    var cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT * FROM Persons WHERE LastName LIKE ? + '%'";
    cmd.Parameters.Add(
        new OleDbParameter("Parameter1", "An")
    );
    using (var rdr = cmd.ExecuteReader()) {
        while (rdr.Read()) {
            persons.Add(
                new Person {
                    ID = (int)rdr["ID"],
                    LastName = (string)rdr["LastName"],
                    FirstName = (string)rdr["FirstName"]
                }
            );
        }
    }
}

Setting up the ConnectionFactory

Before using the .AsSql method, or creating an instance of SqlString, set the global ConnectionFactory:

// using System.Data.OleDb;
// using static StringAsSql.SqlString;

// Define a connection string against an Access database
var connectionString = new OleDbConnectionStringBuilder {
    // ...
}.ToString();
ConnectionFactory = () => new OleDbConnection(connectionString);

See the wiki for more information.

Some more examples:

Create a table:

@"CREATE TABLE Persons (
  ID COUNTER PRIMARY KEY, 
  LastName TEXT,
  FirstName TEXT
)".AsSql().Execute();

Insert rows using parameters, via a collection of values:

var insertSql = "INSERT INTO Persons (FirstName, LastName) VALUES (?, ?)";
insertSql.AsSql(new [] {"Artie", "Choke"}).Execute();
insertSql.AsSql(new [] {"Anna", "Lytics"}).Execute();
insertSql.AsSql(new [] {"Gerry", "Atric"}).Execute();

Get a scalar value:

int count = "SELECT COUNT(*) FROM Persons".AsSql().ExecuteScalar<int>();

Get a list of objects, using a TableDirect command type:

// using static System.Data.CommandType;
List<Person> persons = "Persons".AsSql(TableDirect).ToList<Person>();

stringassql's People

Contributors

zspitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

erisonliang

stringassql's Issues

Transactions

  1. If an SQL statement should be executed on a specific transaction, where should it go in the argument list?
  2. Do we need to check that the transaction and connection match? both that their types match, and that the transaction is on the given connection?

Probably have an overload that takes a ref IDbTransaction, and if the argument is null, begin a new transaction.

Transaction rollback / commit is left up to the calling code.

What about TransactionScope?

Tests using server-based database

Create a new test project for which tests will only execute on appveyor
Inverse of regular tests -- will build only in the ReleaseCI configuration, not the Debug or Release configurations.

SQL Server? MySQL?

Allow returning a List<dynamic>

The same behavior should happen when the type parameter is object -- return a List<ExpandoObject> with the relevant properties set.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.