我使用的API每5分钟分析一次视频并将数据写入数据库。问题是,在数据库中,时间显示为秒(300000、600000、900000 ...),而不是分钟。我是否可以将计时转换为分钟,然后再将其插入数据库?
getFrameTimeStamp =视频的特定分钟数
//the code below means it will only write in every 5 minutes (300000)
if (has.getFrameTimeStamp() % 300000 == 0) {
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\sit\Desktop\(NEW) LEA WINFORMS\28June_LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonAnalysis.mdf;Integrated Security=True")) {
conn.Open();
using (SqlCommand cmd1 = new SqlCommand("INSERT INTO TBL_VIDEO(TIMESTAMP,JOY_SCORE,SURPRISE_SCORE,ANGER_SCORE,FEAR_SCORE,SADNESS_SCORE,DISGUST_SCORE) VALUES('" + has.getFrameTimeStamp().ToString() + "','" + people.get(i).impression.emotion_response.joy_score.ToString() + "','" + people.get(i).impression.emotion_response.surprise_score.ToString() + "','" + people.get(i).impression.emotion_response.anger_score.ToString() + "','" + people.get(i).impression.emotion_response.fear_score.ToString() + "','" + people.get(i).impression.emotion_response.sadness_score.ToString() + "','" + people.get(i).impression.emotion_response.disgust_score.ToString() + "')", conn)) {
using (SqlDataReader dr = cmd1.ExecuteReader()) {
}
}
}
}
答案 0 :(得分:1)
要回答您的问题,请将该值除以60000(60秒为毫秒)以得到所需的结果。
但是,最好将值存储为数字毫秒,并在显示给用户时根据需要执行转换。
// Constants
const int INTERVAL_1_MINUTES_AS_MICROSECONDS = 1 * 60 * 1000; // Or 60000
const int INTERVAL_5_MINUTES_AS_MICROSECONDS = 5 * 60 * 1000; // Or 300000
if (has.getFrameTimeStamp() % INTERVAL_5_MINUTES_AS_MICROSECONDS == 0)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd1 = new SqlCommand())
{
cmd1.CommandText = @"INSERT INTO TBL_VIDEO(TIMESTAMP,JOY_SCORE,SURPRISE_SCORE,ANGER_SCORE,FEAR_SCORE,SADNESS_SCORE,DISGUST_SCORE)";
cmd1.CommandText += " VALUES (@FrameTimeStamp, @emotion_response_joy, @emotion_response_surprise, @emotion_response_anger, @emotion_response_fear, @emotion_response_sadness, @emotion_response_disgust);";
cmd1.CommandType = CommandType.Text;
// Potentially adding a numeric value as string
//cmd1.Parameters.Add(new SqlParameter("@FrameTimeStamp", (Math.Floor(has.getFrameTimeStamp() / INTERVAL_1_MINUTE_AS_MICROSECONDS)).ToString())); // Need to type check
cmd1.Parameters.Add(new SqlParameter("@FrameTimeStamp", (has.getFrameTimeStamp()).ToString()));
cmd1.Parameters.Add(new SqlParameter("@emotion_response_joy", people.get(i).impression.emotion_response.joy_score.ToString()));
cmd1.Parameters.Add(new SqlParameter("@emotion_response_surprise", people.get(i).impression.emotion_response.surprise_score.ToString()));
cmd1.Parameters.Add(new SqlParameter("@emotion_response_anger", people.get(i).impression.emotion_response.anger_score.ToString()));
cmd1.Parameters.Add(new SqlParameter("@emotion_response_fear", people.get(i).impression.emotion_response.fear_score.ToString()));
cmd1.Parameters.Add(new SqlParameter("@emotion_response_sadness", people.get(i).impression.emotion_response.sadness_score.ToString()));
cmd1.Parameters.Add(new SqlParameter("@emotion_response_disgust", people.get(i).impression.emotion_response.disgust_score.ToString()));
int rowsAffected = cmd1.ExecuteNonQuery();
}
}
}
点与代码示例的耦合。您正在将时间戳转换为字符串以存储值。结果,很难对值执行算术运算,因为每次都必须转换为数字形式。将值存储为数字(最好是Long或其他合适的类型)。
此外,您可能希望使用参数来提供值,而不是使用字符串串联。
最后,执行插入操作时无需使用SqlDataReader,因为不会返回任何记录。使用ExecuteNonQuery是更好的选择。
TlDr; 除以60000。如果您有明确的要求,将值存储为分钟或毫秒,并在显示代码中执行转换。使用SQL参数提供数据。