none
F# Dapper how to create connection string. RRS feed

  • Question

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


    Thursday, April 12, 2018 9:24 PM

All replies

  • hello, 

    I'm not good using Dapper, but If I remember it add extensions method to Ado.Net objects, and then simply you should use SqlConnection, with you connectionString in the config file :

            let getConnectionString (name:string) = 
                ConfigurationManager.ConnectionStrings.[name].ConnectionString
            
            let getConnection (name:string) = 
                name
                |> getConnectionString
                |> fun s -> SqlConnection(s)
            
            let connection = getConnection "dev"

    I have another approach to get connectionString name from another app setting here is the Idea : 

    <-- App.config --!> <connectionStrings> <add name="dev" connectoinString="......."/> <add name="tests" connectoinString="......."/> </connectionStrings> <appSettings> <add key="connectionStringToUse" value="dev" /> </appSettings>

    let getConnectionString = ConfigurationManager.AppSettings.["connectionStringTouse"] |> fun s -> ConfigurationManager.ConnectionStrings.[s].ConnectionString

    hope it help;





    Monday, September 3, 2018 9:31 PM