简体   繁体   English

F#获取数据库表数据

[英]F# Get database table data

I'm trying to get data from an Azure SQL Server. 我正在尝试从Azure SQL Server获取数据。 I've Been able to get the data through this method: 我已经能够通过这种方法获取数据:

let db = dbSchema.GetDataContext()
    let serviceType = db.table
    serviceType 

I then go on to do some type casting where I get the actual data. 然后,我继续进行一些类型转换,以获取实际数据。 But as my program has progressed I need a new way of getting the data. 但是随着程序的进展,我需要一种获取数据的新方法。

I'm able to get a list of column names with this piece of code: 我可以使用以下代码获取列名列表:

 let columnList = (new SqlCommandProvider<"select * from Information_schema.Columns where table_name = @tableName",connectionString).Execute(tableName)

I'm wondering if there's a similar way to get the data. 我想知道是否有类似的方法来获取数据。

I've tried: 我试过了:

let data = (new SqlCommandProvider<"select * from @tableName",connectionstring).Execute(tableName)

But I get this error: "Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of object. This may allow the lookup to be resolved." 但是我遇到了这样的错误:“基于此程序点之前的信息对不确定类型的对象进行查找。在此程序点之前可能需要类型注释,以约束对象的类型。这可能使查找得以解决。”

I came up with this. 我想到了这个。

let GetData (tableName : string) =
    let cn = new SqlConnection(connectionstring)
    cn.Open()
    let sQL = "select * from [" + tableName + "]"
    let db = new SqlCommand(sQL, cn)
    db.ExecuteReader()

From here you can access your data. 从这里您可以访问您的数据。 So assign db.ExecuteReader() to a variable then... 因此,将db.ExecuteReader()分配给一个变量,然后...

let dataSource = db.ExecuteReader()
let mutable tableData = List.empty

while dataSource.Read() do
    let rowLength = dataSource.FieldCount
    let rowData = Array.zeroCreate(rowLength)

    for i = 0 to dataSource.FieldCount-1 do 
        rowData.SetValue(dataSource.GetValue(i),i)

    tableData <- rowData :: tableData

tableData |> List.toArray

This returns all the values in the table 这将返回表中的所有值

This is one way (using the SqlClient type provider ) to select all data from a table. 这是一种(使用SqlClient类型的provider )从表中选择所有数据的方法。 If your table is too large it will return a lot. 如果您的桌子太大,它将返回很多。 So I just select the Top 1000 rows. 所以我只选择前1000行。 Replace tablenn with your choice of table name. 用您选择的表名替换tablenn。 You can parametrize the columns, but if you need to put the table name itself as a parameter you might need to use a Stored Procedure that takes a Table as a parameter (or use quotations in other type providers). 您可以对列进行参数化,但是如果需要将表名本身作为参数,则可能需要使用将表作为参数的存储过程(或在其他类型提供程序中使用引号)。

However, this is a string so it's very easy to build it. 但是,这是一个字符串,因此很容易构建它。 Then again, it has to be a literal. 再说一遍,它必须是文字。

#r @"..\packages\FSharp.Data.SqlClient.1.8.2\lib\net40\FSharp.Data.SqlClient.dll"

open FSharp.Data
open System

[<Literal>]
let connectionString = @"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=C:\Users\userName\Documents\Test.sdf.mdf;Integrated Security=True;Connect Timeout=10"

[<Literal>]
let tblnn = "MyBigTable"

[<Literal>]
let qry = "SELECT TOP 1000 * FROM " + tblnn

let cmd = new SqlCommandProvider<qry, connectionString>(connectionString)
cmd.Execute() |> Seq.toArray 

You can also use the stock SqlDataConnection type provider . 您还可以使用普通的SqlDataConnection类型提供程序 It also makes it easy to select all data from a table. 它还使从表中选择所有数据变得容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM