简体   繁体   English

F#从SQL Server表中选择同义词

[英]F# Select from SQL Server table synonym

I have a synonym for a table in my SQL Server database. 我有一个SQL Server数据库中表的同义词。

I want to select rows from the table but when I create data context with a type provider I don't see the synonym in the created data context. 我想从表中选择行,但是当我使用类型提供程序创建数据上下文时,我在创建的数据上下文中看不到同义词。

Synonym is created like this: 同义词的创建方式如下:

CREATE SYNONYM [AnotherDatabase_dbo].[MyTable] FOR [AnotherDatabase].[dbo].[MyTable]

I create data context like this: 我创建这样的数据上下文:

type dbSchema = SqlDataConnection<"Data Source=MyServer\MyInstance;Initial Catalog=MyDatabase;Integrated Security=SSPI;">
let db = dbSchema.GetDataContext ()

How can I access the synonym from F#? 如何从F#访问同义词?

More of a workaround than a solution, but the code below allows you to access a synonym through F#. 更多的解决方法而不是解决方案,但下面的代码允许您通过F#访问同义词。 This uses the FSharp.Data library, available through NuGet or here: http://fsharp.github.io/FSharp.Data/ . 这使用FSharp.Data库,可以通过NuGet或这里获得: httpFSharp.Data NB: Requires SQL 2012 or above. 注意:需要SQL 2012或更高版本。

open FSharp.Data

[<Literal>]
let connectionString = @"Data Source=MyServer\MyInstance;Initial Catalog=MyDatabase;Integrated Security=SSPI;"

[<Literal>]
let query = "select Id, Name from MyTable" 

type OptionQuery = SqlCommandProvider<query, connectionString>
let cmd = new OptionQuery()

cmd.AsyncExecute() 
|> Async.RunSynchronously
|> Seq.iter (fun row -> printfn "Found row: %d %s" row.Id row.Name)

System.Console.Write("Done")
System.Console.ReadKey()

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

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