dart_mssql 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 89

dart_mssql #

High Performance Microsoft SQL Server (MS-SQL Server) Driver for Dart (32 & 64bits)

Important #

  • This package is not suitable for flutter or web projects. It only runs on server-side apps.
  • It works only on Windows (32bits or 64bits)
  • You have to install OLE DB Driver
  • You have to install Microsoft Visual C++ Redistributable
  • dart_mssql_32.dll (32-bit) and dart_mssql_64.dll (64-bit) are the compiled versions of the driver. Rename to "dart_mssql.dll" according to your needs and copy it to the main directory of your project.

Example Usage #

Demo code to perform Raw SQL queries

import 'dart:io';
import 'package:dart_mssql/dart_mssql.dart';

void main() async {
  SqlConnection connection = SqlConnection(host:"SERVERNAME", db:"DBNAME", user:"USERNAME", password:"PASSWORD");
  String cmd = "select email from usuario where id_usuario=?";
  
  SqlResult result = connection.execute(cmd,[4]);
  result.rows.forEach((e) {
    print("${e.email}");
  });
  print("end of printing.");
  connection.close();
  stdin.readLineSync();
}

Troubleshooting #

Problem:
The specified module could not be found.
error: library handler failed

Cause:
Missing installing Microsoft OLE DB Driver OR missing dart_mssql.dll file into project main directory OR missing Microsoft Visual C++ Redistributable

Solution:
Copy dart_mssql.dll file into project main directory.


Problem:
%1 is not a valid Win32 application.
error: library handler failed

Cause:
incorrect dart_mssql.dll version (32 bits with dart VM 64 bits or vice versa)

Solution:
Copy correct dart_mssql.dll file into project main directory.


Compiling with Microsoft Visual Studio 2017 Community Edition #

IF AND ONLY IF you want to compile library source code (C++ part) follow the instructions below:

  • Before compile, you have to install Windows 10 SDK on your Microsoft Visual Studio 2017 Community Edition
  • Open solution file dart_mssql\cpp\dart_mssql.sln with Microsoft Visual Studio 2017 Community Edition
  • On "Solution Explorer" Panel right click on dart_mssql project and select "Rebuild"
  • Put the generated dart_mssql.dll file into your project main folder (same folder of your pubspec.yaml file)
  • Be sure to have correct dart-sdk\bin folder (32 or 64 bits) in VC++ Directories -> Library Directories. Change "F:\DartSDK64\dart-sdk\bin" to your location
  • Be sure to have correct dart.lib version (32 or 64 bits) in Linker -> Input -> Additional Dependencies. Change "F:\DartSDK64\dart-sdk\bin\dart.lib" to your location

[1.0.1]

  • Auto reconnect feature

[1.0.0]

  • Released for production use

[0.2.6]

  • lastIdentify fix!

[0.2.5]

  • Health suggestions adjustments

[0.2.4]

  • DBTYPE_NUMERIC bug fix!

example/main.dart

import 'dart:io';
import 'package:dart_mssql/dart_mssql.dart';

// class "Client" for ORM example
class Client {
  int client_id;
  String client_name;
  List<Invoice> invoices;

  Client.fromJson(Map<String, dynamic> json) {
    client_id = json['client_id'];
    client_name = json['client_name'];
  }  

  Map<String, dynamic> toJson() {  
    return {
      'client_id': client_id,
      'client_name': client_name,
    };
  }
}

// class "Invoice" for ORM example
class Invoice {
  int client_id;
  int inv_number;

  Invoice.fromJson(Map<String, dynamic> json) {
    client_id = json['client_id'];
    inv_number = json['inv_number'];
  }  

  Map<String, dynamic> toJson() {  
    return {
      'client_id': client_id,
      'inv_number': inv_number,
    };
  }
}

void main() {
  // Establish a connection
  SqlConnection connection = SqlConnection(host:"SERVERNAME", db:"DBNAME", user:"USERNAME", password:"PASSWORD");

  // Querying several rows
  String cmd = "select id_nacionalidade,nom_nacionalidade from nacionalidade where id_nacionalidade>?"; // parameters binding!  
  SqlResult result = connection.execute(cmd,[4]);
  result.rows.forEach((e) {
    print("${e.id_nacionalidade}");
  });

  // Querying one row
  cmd = "select id_nacionalidade,nom_nacionalidade from nacionalidade where id_nacionalidade=?"; 
  dynamic row = connection.selectOne(cmd,[4]); // "dynamic" var is important...
  print(row.id_nacionalidade); // ...to allow accessing fields by name

  // raw insert
  cmd = "insert into nacionalidade(id_nacionalidade,nom_nacionalidade) values (1, 'Brasileira')";
  connection.execute(cmd);

  // raw update 
  cmd = "update nacionalidade set nom_nacionalidade = 'Argentina' where id_nacionalidade=1";
  connection.execute(cmd);

  // raw delete
  cmd = "delete from nacionalidade where id_nacionalidade=1";
  connection.execute(cmd);

  // insert
  connection.insert("nacionalidade", {"id_nacionalidade": 1, "nom_nacionalidade": "Brasileira"});
  
  // update
  connection.update("nacionalidade", {"nom_nacionalidade": "Argentina"}, "id_nacionalidade=?", [1]);

  // delete
  connection.delete("nacionalidade", "id_nacionalidade=?", [1]);  

  // Bonus: How to make a master/detail ORM query:
  SqlResult master = connection.execute("select client_id, client_name from client");
  SqlResult detail = connection.execute("select client_id, inv_number from invoice");
  master.rows.forEach((r) {
    Client client = Client.fromJson(r.toJson());
    client.invoices = detail.rows.where((e) => e.client_id == client.client_id).map((e) => Invoice.fromJson(e.toJson())).toList();
  });  
  
  print("end of printing.");
  connection.close();
  stdin.readLineSync();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_mssql: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_mssql/dart_mssql.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
78
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
89
Learn more about scoring.

We analyzed this package on Jan 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/sql_connection.dart. (-0.50 points)

Analysis of lib/src/sql_connection.dart reported 1 hint:

line 64 col 9: Use contains instead of indexOf

Format lib/dart_mssql.dart.

Run dartfmt to format lib/dart_mssql.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
collection ^1.14.11 1.14.12
meta ^1.1.6 1.1.8
sql_recase ^0.2.1 0.2.2
sql_result ^0.2.2 0.2.6
sql_utils ^0.2.0 0.2.1
string_utils ^0.2.0 0.2.2
Transitive dependencies
recase 2.0.1 3.0.0