let useConnection expr =
let Expr(conn : MySqlConnection) =
try
try
conn.Open()
with
| :? MySqlException as ex
-> printfn "Exception! %s" ex.Message
expr(conn)
finally
try
conn.Close() |> ignore
with
| :? MySqlException as ex
-> printfn "Exception! %s" ex.Message
using (new MySqlConnection(ConnectionString =
"server = " + MySQLServer + ";
uid = " + MySQLUID + ";
pwd = " + MySQLPW + ";
database = " + MySQLDB + ";
Charset=utf8;")) Expr
member x.reportToDB (msg:string) =
useConnection // <--- SO HERE I WANT TO KNOW WHAT IS conn
(let cmd = new MySqlCommand(Connection = conn)
cmd.CommandText <- ("insert into "+MySQLTable+"(system,dt,logMessage);")
ignore <| cmd.Parameters.AddWithValue("?system", Net.Dns.GetHostName())
ignore <| cmd.Parameters.AddWithValue("?dt", DateTime.Now.ToString())
ignore <| cmd.Parameters.AddWithValue("?logMessage", msg)
try
try
cmd.ExecuteNonQuery() |> ignore
with
| :? MySqlException as ex when ex.Message.Contains("Duplicate entry")
-> printfn "MySQL Duplicate entry Exception: discarding the data set! %s" ex.Message
printfn ""
| :? MySqlException as ex
-> printfn "MySQL Exception, requeing data set and trying again later! %s" ex.Message
reraise()
with
| :? MySqlException as ex
-> printfn "Exception! %s" ex.Message)
這很難解釋,但我想使用委託conn從useConnection到x.reportToDB,我該怎麼做?如何使用Expr使用參數
謝謝。
@蒂姆羅賓遜,是的,我不知道康恩那裏,那是我想解決的問題, 爲什麼你認爲拉姆達在這裏是個壞主意?
此代碼看起來並不像它編譯現在 - 我不要認爲第一行應該縮進,並且您的評論之後的代碼需要是lambda函數 – 2010-12-13 12:46:54
lamba是一個好主意:)。你目前沒有使用。 – 2010-12-13 12:55:11