简体   繁体   English

使用 MySQL 的 F# 程序的基本 Model

[英]Basic Model of a F# program using MySQL

Just starting to experiment with F#.刚开始试验 F#。 I have this code working in C#:我有这个代码在 C# 中工作:

using MySql.Data.MySqlClient;
using System;
using System.IO;

namespace SandboxExecuteReader
{
    class Program
    {
        static void Main(string[] args)
        {
            MySqlConnection myConnection = new MySqlConnection();
            MySqlCommand myCommand = new MySqlCommand();
            if (!SQLdbOpen(myConnection, myCommand)) return;
            DoWork(myCommand);
            myConnection.Close();
            return;
        }

        private static void DoWork(MySqlCommand myCommand)
        {
            myCommand.CommandText = "SELECT * FROM xxx.mytable";
            using (var reader = myCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                    string Field1 = reader.GetString(reader.GetOrdinal("Field1"));
                    string Field2 = reader.GetString(reader.GetOrdinal("Field2"));
                    string Field3 = reader.GetString(reader.GetOrdinal("Field3"));
                    Console.WriteLine("{0} {1} {2}", Field1, Field2, Field3);
                }
            }
        }

        private static bool SQLdbOpen(MySqlConnection myConnection, MySqlCommand myCommand)
        {
            /* 
            * Open Connection to SQL DB
            */

            string ConnectionString = "server=(someIP); uid=Me; password=MyPass";

            try
            {
                myConnection.ConnectionString = ConnectionString;
                myConnection.Open();
                myCommand.Connection = myConnection;
            }
            catch (MySqlException E)
            {
                Console.WriteLine("Open Error: {0}", E.Message);
                Console.WriteLine("Press RETURN to continue or CONTROL-C to abort");
                Console.ReadLine();
                return false;
            }

            return true;
        }
    }
}

Questions:问题:

  1. In C#, I add a reference to MySql.Data.dll.在 C# 中,我添加了对 MySql.Data.dll 的引用。 How do I do that in F#?我该如何在 F# 中做到这一点?
  2. There seems to be something about #r... where does that line go?似乎有一些关于#r ... go 行在哪里? Somewhere in the.fsproj file? .fsproj 文件中的某个位置?
  3. How would this code look in F#?此代码在 F# 中的外观如何?

The first thing you need to decide is whether you are going to run this as a standalone script ( .fsx file) or a project (consisting of .fs source files and a .fsproj project file).您需要决定的第一件事是将其作为独立脚本( .fsx文件)还是作为项目(由.fs源文件和.fsproj项目文件组成)运行。

If this is going to be a project, it works just like a C# project: you add a reference to MySql.Data.dll in your .fsproj file.如果这将是一个项目,它就像 C# 项目一样工作:在.fsproj文件中添加对MySql.Data.dll的引用。 If you're using Visual Studio, you can right-click on the project and find MySql.Data in NuGet via the "Manage NuGet Packages..." menu item.如果您使用的是 Visual Studio,您可以右键单击该项目并通过“管理 NuGet 包...”菜单项在 NuGet 中找到MySql.Data

If it's going to be a standalone script, you can add a NuGet reference to MySql by placing this line at the very top: #r "nuget: MySql.Data" .如果它是一个独立的脚本,你可以添加一个 NuGet 引用到 MySql 通过将这一行放在最顶部: #r "nuget: MySql.Data"

Converting your code from C# is a bit more involved, but your main function might look like this (assuming this is a .fs file):从 C# 转换您的代码有点复杂,但您的main function 可能看起来像这样(假设这是一个.fs文件):

open MySql.Data.MySqlClient

let sQLdbOpen (myConnection : MySqlConnection) (myCommand : MySqlCommand) =
    // ...
    true

let doWork (myCommand : MySqlCommand) =
    // ...
    ()

[<EntryPoint>]
let main args =
    use myConnection = new MySqlConnection()
    use myCommand = new MySqlCommand()
    if sQLdbOpen myConnection myCommand then
        doWork myCommand
        myConnection.Close()
    0

(Caveat: I did this translation by eye, so it might not be exactly right.) (警告:我是用肉眼翻译的,所以可能不完全正确。)

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

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