Giter VIP home page Giter VIP logo

automation-sql's Introduction

Automation : SQL

This library is designed to allow the creation of LocalDb databases via C#. Mainly used to write integration tests, you get a fully functional SQL database that has full SQL Express capabilities.

NuGet Version NuGet Download Count

Prerequisites

To use this library, you will need SQL Express 2012 or SQL Express 2014 versions of LocalDb installed. We recommend you installing both via Chocolatey.

Quick Start

Install via:

Install-Package RimDev.Automation.Sql

The LocalDb class will perform the following tasks:

  1. Create the database files in the path specified
  2. Connect the files to your Localdb instance
  3. Return a connection string that can be utilized with any SQL Capable tool (ORM, Mini-ORM, ADO.NET)

To use the class, do the following in your test.

using (var database = new LocalDb()) {
  // your code goes here
}

A simple use case utilizing ADO.NET would look something like this.

using (var database = new LocalDb())
{
  using (var connection = database.OpenConnection())
  {
    var command =
       new SqlCommand("CREATE TABLE Customers (" +
          "DrvLicNbr NVarChar(50), " +
          "DateIssued Date," +
          "DateExpired Date," +
          "FullName nvarchar(120)," +
          "Address NVARCHAR(120)," +
          "City nvarchar(50)," +
          "State nvarchar(100)," +
          "PostalCode nvarchar(20)," +
          "HomePhone nvarchar(20)," +
          "OrganDonor bit);",
          connection);

        // created a new table
        command.ExecuteNonQuery();
  }
}

Configuration

The LocalDB class is initialized completely via the constructor. There are four options you may set. All are optional.

  1. Database name : The name of the database as seen in SQL Server Management studio. If not specified the naming will be localdb_<DateTime.Not.Ticks>.
  2. Version : There are two versions supported for LocalDB, which are v11 and v12. By default we try to use v11 as it is most likely installed.
  3. Location : The location where the database files will be created (log and mdf). By default it will be in Assembly.GetExecutingAssembly().Location.
  4. Database prefix : This is used as the prefix when creating the database name, if the database name is not specified. By default this value is "localdb".

Notice each are optional and there are safe rational defaults.

Gotcha's

LocalDB installed but can't Connect

You may have LocalDB installed, but never initialized the instance on your machine. Run this command via command prompt.

LocalDB SQL EXPRESS 2014

"C:\Program Files\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe" create "v12.0" 12.0 -s

LocalDB SQL Express 2012

"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe" create "v11.0" 11.0 -s

Verify that the command worked by using SQL Server Management Studio to connect to the instance.

Contributors

  • Khalid Abuhakmeh (@buhakmeh)
  • Justin Rusbatch (@jrusbatch)

Thanks

Thanks to Ritter IM for supporting OSS.

automation-sql's People

Contributors

jrusbatch avatar khalidabuhakmeh avatar kendaleiv avatar cottsak avatar lightyeare avatar scottschwalm avatar

Watchers

James Cloos avatar Soundararajan Karunagaran avatar

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.