All Projects → jdforsythe → Mysqlconnection

jdforsythe / Mysqlconnection

Simple library to make it much easier to use MySQL in Visual Studio projects

Projects that are alternatives of or similar to Mysqlconnection

Leafpub
Simple, beautiful, open source publishing.
Stars: ✭ 645 (+10650%)
Mutual labels:  database, mysql
Metabase
The simplest, fastest way to get business intelligence and analytics to everyone in your company 😋
Stars: ✭ 26,803 (+446616.67%)
Mutual labels:  database, mysql
Laravel Db Snapshots
Quickly dump and load databases
Stars: ✭ 650 (+10733.33%)
Mutual labels:  database, mysql
Manticoresearch
Database for search
Stars: ✭ 610 (+10066.67%)
Mutual labels:  database, mysql
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+128433.33%)
Mutual labels:  database, mysql
Dbshield
Database firewall written in Go
Stars: ✭ 620 (+10233.33%)
Mutual labels:  database, mysql
Database rewinder
minimalist's tiny and ultra-fast database cleaner
Stars: ✭ 685 (+11316.67%)
Mutual labels:  database, mysql
Typeorm
ORM for TypeScript and JavaScript (ES7, ES6, ES5). Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
Stars: ✭ 26,559 (+442550%)
Mutual labels:  database, mysql
Db Dumper
Dump the contents of a database
Stars: ✭ 744 (+12300%)
Mutual labels:  database, mysql
Baikaldb
BaikalDB, A Distributed HTAP Database.
Stars: ✭ 707 (+11683.33%)
Mutual labels:  database, mysql
Beekeeper Studio
Modern and easy to use SQL client for MySQL, Postgres, SQLite, SQL Server, and more. Linux, MacOS, and Windows.
Stars: ✭ 8,053 (+134116.67%)
Mutual labels:  database, mysql
Eralchemy
Entity Relation Diagrams generation tool
Stars: ✭ 767 (+12683.33%)
Mutual labels:  database, mysql
Mariadb4j
MariaDB Embedded in Java JAR
Stars: ✭ 579 (+9550%)
Mutual labels:  database, mysql
Easydb
Easy-to-use PDO wrapper for PHP projects.
Stars: ✭ 624 (+10300%)
Mutual labels:  database, mysql
Entityframework Reverse Poco Code First Generator
EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
Stars: ✭ 562 (+9266.67%)
Mutual labels:  database, visual-studio
Blog
Everything about database,business.(Most for PostgreSQL).
Stars: ✭ 6,330 (+105400%)
Mutual labels:  database, mysql
Darkui
Dark themed control and docking library for .NET WinForms.
Stars: ✭ 539 (+8883.33%)
Mutual labels:  visual-studio, dotnet-framework
Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (+8883.33%)
Mutual labels:  database, mysql
Tidb
TiDB is an open source distributed HTAP database compatible with the MySQL protocol
Stars: ✭ 29,871 (+497750%)
Mutual labels:  database, mysql
Bookshelf
A simple Node.js ORM for PostgreSQL, MySQL and SQLite3 built on top of Knex.js
Stars: ✭ 6,252 (+104100%)
Mutual labels:  database, mysql

MySQLConnection

MySQLConnection makes it easier to use MySQL in your Visual Studio applications by abstracting the implementation of the MySQL .NET Connector away so you can work with strictly native .NET data types.

This class implements the IDisposable interface, and should be used exclusively with the using() pattern to ensure proper cleanup of connections.

Quick Example

string myValue;

using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "SELECT my_column FROM table LIMIT 1";
  myValue = sql.selectQueryForSingleValue();
}

Console.WriteLine(myValue);

Create a connection

Again, always use using(). The connectionString parameter passed to the MySQLConn constructor is the same format as you would use for the MySQL .NET Connector library:

[https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html]

string connectionString = "server=127.0.0.1; uid=root; pwd=12345; database=test;";
// or
string connectionString = "Data Source=127.0.0.1; user id=root; password=12345; database=test;";
// and add any options at the end, such as AllowZeroDateTime=false

using (MySQLConn sql = new MySQLConn(connectionString)) {}

Or you can pass the parameters individually

// (server, database, username, password, options_string)
using (MySQLConn sql = new MySQLConn("127.0.0.1", "test", "root", "12345", "AllowZeroDateTime=false")) {}

Setting a query

Queries are set on the .Query property.

sql.Query = "SELECT * FROM table";

Parameterized queries

Parameters are set in the query the same way as the standard MySQL .NET Connector.

sql.Query = "SELECT * FROM table WHERE [email protected]";

Add parameter value

sql.addParam("@IdNumber", "1");

Remove previously-set parameter value

sql.removeParam("@IdNumber");

Update previously-set parameter with new value

sql.updateParam("@IdNumber", "2");

Clear all previously-set parameters

sql.clearParams();

Available SQL methods

selectQueryForSingleValue()

  • returns a string, or NULL, if there is no result from the database
  • used when querying for a single column in a single record
string myValue;
using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "SELECT my_column FROM table LIMIT 1";
  myValue = sql.selectQueryForSingleValue();
}

selectQueryForSingleRecord()

  • returns a Dictionary<string, string>, or NULL if there is no result from the database
  • used when querying for a single record but any number of columns
  • dictionary is of form <column_name, value>
Dictionary<string, string> myRecord;
using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "SELECT * FROM my_column WHERE [email protected] LIMIT 1";
  sql.addParam("@IdNumber", "42");
  myRecord = sql.selectQueryForSingleRecord();
}

selectQueryForSingleColumn()

  • returns a List<string>
  • returns an empty List<string> if there is no result from the database (this allows a foreach on the result without a NullReferenceException)
  • used when querying for a single column, but any number of records
List<string> myColVals;
using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "SELECT my_column FROM table";
  myColVals = sql.selectQueryForSingleColumn();
}

selectQuery()

  • returns a List<Dictionary<string, string>> representing a list of the records
  • returns an empty List<Dictionary<string, string>> if there is no result from the database (this allows a foreach on the result without a NullReferenceException)
  • used when querying for multiple columns and rows
List<Dictionary<string, string>> tableRows;
using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "SELECT * FROM table";
  tableRows = sql.selectQuery();
}

insertQuery()

  • returns an int indicating the number of affected rows
int affectedRows = 0;
using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "INSERT INTO table (my_column_1, my_column_2) VALUES (@Col1, @Col2)";
  sql.addParam("@Col1", "value_one");
  sql.addParam("@Col2", "value_two");
  affectedRows = sql.insertQuery();
}

updateQuery()

  • returns an int indicating the number of affected rows
int affectedRows = 0;
using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "UPDATE table SET [email protected], [email protected] WHERE [email protected] LIMIT 1";
  sql.addParam("@Col1", "value_one");
  sql.addParam("@Col2", "value_two");
  sql.addParam("@IdNumber", "42");
  affectedRows = sql.updateQuery();
}

deleteQuery()

  • returns an int indicating the number of affected rows
int affectedRows = 0;
using (MySQLConn sql = new MySQLConn(connectionString)) {
  sql.Query = "DELETE FROM table WHERE [email protected]";
  sql.addParam("@IdNumber", "42");
  affectedRows = sql.deleteQuery();
}

loadDataInfile()

  • returns an int indicating the number of affected rows
  • uses LOAD DATA INFILE to load bulk records into the database
int affectedRows = 0;
string tableName = "table",
       fieldTerminator = ",",
       lineTerminator = "\r\n",
       filename = "infile.csv",
       linesToSkip = "1";

using (MySQLConn sql = new MySQLConn(connectionString)) {
  affectedRows = sql.loadDataInfile(tableName, fieldTerminator, lineTerminator, filename, linesToSkip);
}

Exceptions

  • Throws ObjectDisposedException if the class has been disposed - this probably shouldn't happen
  • Throws generic Exception if no connection was opened (not connection error) - also probably shouldn't happen
  • Throws generic Exception if the Query property is empty
  • Throws generic Exception if the any of the connection paramters are null or empty strings
  • Throws standard MySQL Connector exceptions for any database connection or query errors

Contributions

Issues and pull requests are always welcome

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].