, langauges. F # , , .
None, - ( , None). . , , .
:
exception SqlUtilException of string
let raiseSql fmt =
Printf.kprintf (SqlUtilException >> raise) fmt
.NET F #, :
let connName = ConfigHandler.GetConnectionString "MyDB"
use conn = new SqlConnection(connName)
try conn.Open()
with ex -> raiseSql "Failed to connect to DB %s with Error %s " connName ex.Message
use cmd =
new SqlCommand( Connection = conn, CommandText = "dbo.usp_MyStordProc",
CommandType = CommandType.StoredProcedure )
let param1 = new SqlParameter("@User", SqlDbType.NVarChar, 50, Value = user)
let param2 = new SqlParameter("@PolicyName", SqlDbType.NVarChar, 10, Value = policyName)
cmd.Parameters.AddRange [| param1; param2 |]
use reader =
try cmd.ExecuteReader()
with ex -> raiseSql "Failed to execute reader with error %s" ex.Message
()
.NET-, . F # , . F #, - ?, - :
let connName = ConfigHandler.GetConnectionString "MyDB"
// A wrapper that provides dynamic access to database
use db = new DynamicDatabase(connName)
// You can call stored procedures using method call syntax
// and pass SQL parameters as standard arguments
let rows = db.Query?usp_MyStordProc(user, policy)
// You can access columns using the '?' syntax again
[ for row in rows -> row?Column1, row?Column2 ]
. MSDN: