F#连接到在线MySQL数据库执行查询 [英] F# Connect to Online MySQL DB execute query

查看:72
本文介绍了F#连接到在线MySQL数据库执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个F#项目,需要对在线mysql数据库进行一些数据库查询.谁能帮帮我吗.我需要这样的东西

I am making a F# project and need to do some database queries to an online mysql db. Can anyone please help me. I need something like this

    \\  Connect to DB
    let servername = "localhost"
    let username = "username"
    let password = "password"
    \\ Code that connects to db
    \\ Print error message if can connect 

    \\ Query 
    let query = "SELECT * FROM table ..."
    \\ Code that executes query 
    \\ Error Message if query not executed  

推荐答案

您应该为MySQL安装.NET驱动程序.然后安装SQLprovider.在文档中有MySQL的示例.您将连接到数据库并像这样查询它:

You should install the .NET driver for MySQL. Then install the SQLprovider. There are samples for MySQL in the docs. You would connect to the db and query it like this:

type sql = SqlDataProvider<
                dbVendor,
                connString,
                ResolutionPath = resPath,
                IndividualsAmount = indivAmount,
                UseOptionTypes = useOptTypes,
                Owner = "HR"
            >
let ctx = sql.GetDataContext()

let employees = 
    ctx.Hr.Employees 
    |> Seq.map (fun e -> e.ColumnValues |> Seq.toList)
    |> Seq.toList

connstring 将如下所示:

[<Literal>]
let connString  = "Server=localhost;Database=HR;User=root;Password=password"

您还应该阅读 https://msdn.microsoft.com/visualfsharpdocs/conceptual/walkthrough-accessing-a-sql-database-by-using-type-providers-%5bfsharp%5d

这篇关于F#连接到在线MySQL数据库执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆