F# Dapper how to create connection string.

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
Hello:
I recently want to learn Dapper for F#, in order to access SQL Server database.
I found some article for this topic.
And I created a simple database [myDB] using SQL Server 2017, and created a small table:

USE [MyDB]
GO

CREATE TABLE [dbo].[Users] (
[ID] [int] NOT NULL,
[FirstName] [nvarchar](30) NOT NULL,
[LastName] [nvarchar](30) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY(ID))
GO

Add some records:
INSERT INTO USERS VALUES(1, 'Jo', 'Jo')
INSERT INTO USERS VALUES(2, 'John', 'Doe')

Now I want to use someone's code to retrieve records from [Users]:

module DapperFSharp =
open System.Data.SqlClient
open System.Dynamic
open System.Collections.Generic
open Dapper

let dapperQuery<'Result> (query:string) (connection:SqlConnection) =
connection.Query<'Result>(query)

let dapperParametrizedQuery<'Result> (query:string) (param:obj) (connection:SqlConnection) : 'Result seq =
connection.Query<'Result>(query, param)

let dapperMapParametrizedQuery<'Result> (query:string) (param : Map<string,_>) (connection:SqlConnection) : 'Result seq =
let expando = ExpandoObject()
let expandoDictionary = expando :> IDictionary<string,obj>
for paramValue in param do
expandoDictionary.Add(paramValue.Key, paramValue.Value :> obj)

connection |> dapperParametrizedQuery query expando


However, I can't figure out how to write the connection string.
And how to use his query helper to do something like in T-SQL, I can run the following query:
SELECT FirstName, LastName FROM Users WHERE ID = 2

You can install Dapper from Package Manager Console:
PM> Install-Package Dapper

I am using Visual Studio 2017 on Windows 10.
Please advise.

Continue reading...
 
Back
Top