用c#在mysql中插入多行的最佳方法?

时间:2016-04-21 15:59:57

标签: c# mysql

我需要使用C#将多行数据输入到MySQL中。 enter image description here

我有以下代码插入一行,但我不知道如何一次接近多行。我需要在表格中输入最多8行。我应该制作多个构造函数还是有更好的方法?

public class Vo2phases
{
    //properties for vo2max phases
    public int VO2TestID { get; set; }
    public int Phase { get; set; }
    public DateTime Time { get; set; }
    public int Intensity { get; set; }
    public double RER { get; set; }
    public int Borgscale { get; set; }
    public int Pulse { get; set; }

    public Vo2phases(int _vo2testid, int _phase, 
                     DateTime _time, int _intensity, 
                     double _rer, int _borgscale, int _pulse)
    {
        //set parameters for phases
        VO2TestID = _vo2testid;
        Phase = _phase;
        Time = _time;
        Intensity = _intensity;
        RER = _rer;
        Borgscale = _borgscale;
        Pulse = _pulse;
    }

    public void addToDB()
    {
        try
        {
            //prepare for query
            var cmd = MySQL.readyQuery();

            //insert testdata to Vo2test
            cmd.CommandText = "INSERT INTO vo2phases_tb (Vo2_TestID, Phase, Time, Intensity, RER, Borgscale, Pulse) VALUES (@Vo2testid, @phase, @time, @intensity, @rer, @borgscale, @pulse);";

            cmd.Prepare();
            cmd.Parameters.AddWithValue("@vo2testid", VO2TestID);
            cmd.Parameters.AddWithValue("@phase", Phase);
            string TimeString = "00:" + Convert.ToString(Time.TimeOfDay.Hours) + ":" + Convert.ToString(Time.TimeOfDay.Minutes);
            cmd.Parameters.AddWithValue("@time", TimeString);
            cmd.Parameters.AddWithValue("@intensity", Intensity);
            cmd.Parameters.AddWithValue("@rer", RER);
            cmd.Parameters.AddWithValue("@borgscale", Borgscale);
            cmd.Parameters.AddWithValue("@pulse", Pulse);

            cmd.ExecuteNonQuery();

            cmd.Connection.Close();
        }
        catch (MySqlException ex)
        {
            Console.WriteLine(ex.Message);
        }

    }

2 个答案:

答案 0 :(得分:1)

您的问题是您似乎只在您的代码中期待1个Vo2phase。

虽然这在概念上甚至在执行中都不是问题,但它缺乏,因为它没有考虑提交的集合。

我会重载addToDB,以便接受List作为参数。

public void addToDB(List<Vo2phase> phases)
{
    try
    {
        //prepare for query
        var cmd = MySQL.readyQuery();

        //insert testdata to Vo2test
        cmd.CommandText = "INSERT INTO vo2phases_tb (Vo2_TestID, Phase, Time, Intensity, RER, Borgscale, Pulse) VALUES (@Vo2testid, @phase, @time, @intensity, @rer, @borgscale, @pulse);";

        cmd.Prepare();
        foreach (Vo2phase phase in phases)
        {
            cmd.Parameters.AddWithValue("@vo2testid", VO2TestID);
            cmd.Parameters.AddWithValue("@phase", Phase);
            string TimeString = "00:" + Convert.ToString(Time.TimeOfDay.Hours) + ":" + Convert.ToString(Time.TimeOfDay.Minutes);
            cmd.Parameters.AddWithValue("@time", TimeString);
            cmd.Parameters.AddWithValue("@intensity", Intensity);
            cmd.Parameters.AddWithValue("@rer", RER);
            cmd.Parameters.AddWithValue("@borgscale", Borgscale);
            cmd.Parameters.AddWithValue("@pulse", Pulse);

            cmd.ExecuteNonQuery();

            cmd.Parameters.Clear();
        }
        cmd.Connection.Close();
    }
    catch (MySqlException ex)
    {
        Console.WriteLine(ex.Message);
    }

}

现在要构建列表,你只需添加一大堆Vo2phases:

List<Vo2phase> Phases = new List<Vo2phase>
{
    new Vo2phase(VO2TestID = testId, Phase = i ...),
    new Vo2phase(VO2TestID = testId, Phase = i ...),
    new Vo2phase(VO2TestID = testId, Phase = i ...),
    new Vo2phase(VO2TestID = testId, Phase = i ...),
    new Vo2phase(VO2TestID = testId, Phase = i ...),
    new Vo2phase(VO2TestID = testId, Phase = i ...),
    new Vo2phase(VO2TestID = testId, Phase = i ...)

    // add the parameters as you normally would.
};

希望这有帮助!

答案 1 :(得分:0)

我的方法,如果我期望大量的插入,将使addToDB成为一个静态方法,它接受连接和Vo2phases的集合;当你只将它用于单个命令执行时,你几乎失去了任何好处,并且实际上从prepare产生了一些成本。

除此之外,准备好的命令可以作为静态成员变量维护,以便不会不断重新创建;然而,你可能会用一些代码来验证命令是否已经变坏了#34;因为它是最后一次使用。