我有以下代码插入一行,但我不知道如何一次接近多行。我需要在表格中输入最多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);
}
}
答案 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;因为它是最后一次使用。