Jump to content
Xtreme .Net Talk

F# Dapper how to create connection string.


Recommended Posts

Guest zydjohn
Posted

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...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...