Skip to content

Instantly share code, notes, and snippets.

@OnorioCatenacci
Created October 10, 2013 12:56
Show Gist options
  • Select an option

  • Save OnorioCatenacci/6917875 to your computer and use it in GitHub Desktop.

Select an option

Save OnorioCatenacci/6917875 to your computer and use it in GitHub Desktop.

Revisions

  1. OnorioCatenacci created this gist Oct 10, 2013.
    44 changes: 44 additions & 0 deletions fortunecookie.fsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    open System
    open System.Data.Odbc
    open System.Windows.Forms


    let connectToAccess filename =
    let connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;FileDSN=" + filename + ";User Id=admin;Password=;"
    new OdbcConnection(connectionString)

    let connectToDb() =
    let userProfileDir = Environment.GetEnvironmentVariable("UserProfile")
    let accessFile = userProfileDir + @"\Documents\FortuneCookie.accdb"
    connectToAccess accessFile

    let pasteStringToClipboard string =
    Clipboard.SetText string

    let buildQuoteString quote author =
    sprintf "%s\n -- %s" quote author


    let getRandomQuote()=
    let connection = connectToDb()
    let tableName = "FortuneCookieQuotes"
    //Note: the order by clause has to be in the order it is or you get the same quote repeatedly
    let randomQuoteSQL = "SELECT TOP 1 ID,FortuneCookieText,CreditedTo FROM " + tableName + " ORDER BY LastUsed ASC, RND(ID)"
    let getQuoteCommand = new OdbcCommand(randomQuoteSQL,connection)
    connection.Open()
    let result = getQuoteCommand.ExecuteReader()
    let ableToReadResult = result.Read()
    let s = buildQuoteString (result.["FortuneCookieText"] :?> string) (result.["CreditedTo"] :?> string)
    let quoteID = result.["ID"] :?> int
    let updateQuoteSQL = "UPDATE " + tableName + " SET LastUsed = '" + DateTime.Now.ToString("d") + "' WHERE ID = " + quoteID.ToString()
    let updateResultCommand = new OdbcCommand(updateQuoteSQL,connection)
    let updateResult = updateResultCommand.ExecuteNonQuery()
    connection.Close()
    s
    [<STAThread>]
    let main (argv:string[]) =
    let quote = getRandomQuote()
    pasteStringToClipboard quote
    0

    main fsi.CommandLineArgs;;