在下面的代碼中,我想通過F#插入或更新SQL表中的行。 它需要一個代表用戶的元組矩陣以及一些F#計算結果的相關分數(usrID,分數)。
現在我想更新一個名爲UserScoresTable的SQL表。
我寫的代碼正在工作,但速度很慢。如何在F#中包含存儲過程
let cnn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings.Item("Database").ConnectionString)
cnn.Open()
// Definition d'une fonction qui execute une demande sous MySQL
let execNonQuery s =
let comm = new System.Data.SqlClient.SqlCommand(s, cnn, CommandTimeout = 10)
comm.ExecuteNonQuery() |> ignore
// Definition d'une fonction qui utilise les éléments d'une matrice pour updater une table dans MySQL
let updateMySQLTable (m : Matrix<float * float>) =
for j in 0 .. (snd m.Dimensions) - 1 do
for i in 1 .. (fst m.Dimensions) - 1 do
// first check if the user and score and date do not exist
sprintf "IF NOT EXISTS (SELECT * FROM ProductScores WHERE UserID = %f AND ProductID = %f) INSERT INTO ProductScores (UserID, Score, Date) VALUES (%f, %f,CURRENT_TIMESTAMP)" (fst m.[i, j]) (snd m.[i, j])
|> execNonQuery
// otherwise update the row
sprintf "UPDATE ProductScores SET Score = %f, Date = CURRENT_TIMESTAMP WHERE UserID = %f " (snd m.[i, j]) (fst m.[i, j])
|> execNonQuery
我想通過使用存儲過程,如
CREATE PROCEDURE updateScoreByUsers
-- Add the parameters for the stored procedure here
@UserID int,
@Score float
AS
BEGIN
IF NOT EXISTS
(SELECT * FROM ProductScores WHERE UserID = @UserID)
INSERT INTO
ProductScores (UserID, Score, Date) VALUES (@UserID,@Score,CURRENT_TIMESTAMP)
ELSE
UPDATE ProductScores
SET Score = @Score, Date = CURRENT_TIMESTAMP
WHERE UserID = @UserID
END
GO
,以避免在代碼中兩個execNonQuery請求,但我不知道該怎麼稱呼F#中的SQL存儲過程。
我如何用F#調用它?
您是否還有其他改進方法?
您對ADO.NET對象的管理似乎有點脆弱。 – ChaosPandion 2012-01-17 18:27:26
你使用的是MS SQL還是MySQL? – ChaosPandion 2012-01-17 18:28:18
對不起,我正在使用MS SQL。 – fabco63 2012-01-17 18:31:16