public
Created

Quick And dirty example of IronPython using Dapper.NET (w/MySql backend and stored procedures)

  • Download Gist
DapperTest.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#DapperTest.py - Quick And dirty example of IronPython using Dapper.NET (w/MySql backend and stored procedures)
import System
 
import clr
#clr.AddReferenceByPartialName("System.Core")
clr.AddReferenceByPartialName("System.Data")
 
from System.Collections import IDictionary
from System.Collections.Generic import IDictionary, IList
from System.Data import IDbConnection, IDbCommand, CommandType
 
import sys
import os
sys.path.append(os.getcwd() + '\\lib') #external dependencies are located in a 'lib' folder in same path as this script
 
clr.AddReferenceToFile('Dapper.dll')
import Dapper
clr.ImportExtensions(Dapper)
from Dapper import DynamicParameters, SqlMapper
 
clr.AddReferenceToFile('MySql.Data.dll')
import MySql.Data
 
clr.AddReferenceToFile('App.dll') #custom .NET assembly used for when IronPython doesn't fit the need.
import App.Core.Models as Models #get our "dumb" model objects from the projects assembly.
cn = MySql.Data.MySqlClient.MySqlConnection("Server=localhost;Database=northwinddb;Uid=springqa;Pwd=springqa;" \
"Convert Zero Datetime=true;Allow Zero Datetime=false")
cn.Open()
 
params = DynamicParameters()
params.Add('@in_CustomerId', 'ALFKI')
procName = 'GetCustomerById'
customer = cn.Query[Models.Customer](procName, params,commandType=CommandType.StoredProcedure)[0]
cn.Close()
 
print customer.CustomerId
print customer.ContactName
print customer.ContactTitle

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.