我正在尝试将字符串转换为TimeSpan并将其保存到我的MS Access数据库中。但我似乎无法正确转换。
如果您可以在下面查看我的代码,我们将不胜感激。谢谢!
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
CultureInfo culture;
culture = CultureInfo.CurrentCulture;
string pNum = patientNum.Text;
string pFname = Firstname.Text;
string pLname = Lastname.Text;
string cpNum = contactNum.Text;
DateTime aDate = DateTime.ParseExact(appDate.Text, "MM/dd/yyyy", culture);
TimeSpan aTime = TimeSpan.ParseExact(appTime.Text, "hh:mm tt", culture);
string aTreat = treat.Text;
string aCost = treatCost.Text;
string aRemark = appRemarks.Text;
OleDbCommand con = new OleDbCommand("Insert into appointments1(patientNo,firstName,lastName,contactNo,date,time,treatment,cost,remarks)" + "Values (@patientNo,@firstName,@lastName,@contactNo,@date,@time,@treatment,@cost,@remarks)");
con.Connection = connection;
connection.Open();
con.Parameters.Add("@patientNo", OleDbType.Integer).Value = pNum;
con.Parameters.Add("@firstName", OleDbType.VarChar).Value = pFname;
con.Parameters.Add("@lastName", OleDbType.VarChar).Value = pFname;
con.Parameters.Add("@conatctNum", OleDbType.VarChar).Value = cpNum;
con.Parameters.Add("@date", OleDbType.Date).Value = aDate;
con.Parameters.Add("@time", OleDbType.DBTime).Value = aTime;
con.Parameters.Add("@treatment", OleDbType.VarChar).Value = aTreat;
con.Parameters.Add("@cost", OleDbType.Currency).Value = aCost;
con.Parameters.Add("@remarks", OleDbType.VarChar).Value = aRemark;
try
{
con.ExecuteNonQuery();
MessageBox.Show("Appointment Added!");
raiseUpdate();
connection.Close();
this.Dispose();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
}
答案 0 :(得分:0)
您应该像这样使用它:
TimeSpan aTime = TimeSpan.ParseExact(appTime.Text, "hh\\:mm\\ tt", culture);
有关更多信息,请参见文档:Custom TimeSpan format strings
答案 1 :(得分:0)
尝试
$ jq '.[] | split("=") | select(.[0]=="bucketName")[1]' <<< '["bucketName=myBucket", "a=b"]
"myBucket"
答案 2 :(得分:0)
您无法以这种方式解析时间跨度。尝试使用 Parse 和匹配的区域性:
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
DateTime aDate = DateTime.Parse(appDate.Text, culture);
TimeSpan aTime = DateTime.Parse(appTime.Text).TimeOfDay;
此外,Access没有特殊的时间,因此请尝试(尽管我目前无法对此进行测试):
con.Parameters.Add("@time", OleDbType.Date).Value = aTime;
如果这不起作用,则可能必须使用VBA的DateTime的 epoch (数字零日期)进行暴力破解:
DateTime vbaEpoch = new DateTime(1899, 12, 30);
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
DateTime aDate = DateTime.Parse(appDate.Text, culture);
DateTime aTime = vbaEpoch.AddTicks(DateTime.Parse(appTime.Text).TimeOfDay.Ticks);
和:
con.Parameters.Add("@time", OleDbType.Date).Value = aTime;
或者,您可以仅使用一个日期时间字段将其全部删除:
DateTime aDateTime = DateTime.Parse(appDate.Text + " " + appTime.Text, culture);
答案 3 :(得分:0)
谢谢大家的回答,我能够弄清楚。
访问确实没有“特殊时间”(?),不确定是否是您的意思,但是我将时间更改为“日期”。
con.Parameters.Add("@date", OleDbType.Date).Value = aDate;
con.Parameters.Add("@time", OleDbType.Date).Value = aTime;
即使我将值另存为字符串,它也能正常工作
string aDate = appDate.Text;
string aTime = appTime.Text;
我开始了解到“日期”和“时间”是保留字。不知道C#是否区分大小写,但比遗憾更安全。因此,我必须添加方括号来包装我的列名。 See info here。
OleDbCommand con = new OleDbCommand("INSERT INTO [appointments1]([patientNo], [firstName], [lastName], [contactNo], [date], [time], [treatment], [cost], [remarks])" +
"VALUES(@patientNo, @firstName, @lastName, @contactNo, @date, @time, @treatment, @cost, @remarks)");
现在,我的代码一切正常。谢谢!!