[英]How to convert c# Datetime into Foxpro Date
I've converted FOX PRO tables using OLEDB and I am using tables in c# ,when I am inserting data into tables it's working fine but when i am inserting date field(present day)date it's showing error data type mismatch. 我已经使用OLEDB转换了FOX PRO表,并且正在c#中使用表,当我向表中插入数据时,它工作正常,但是当我插入日期字段(现在)date时,它显示错误数据类型不匹配。
dateformat dd/MM/yyyy only date no time. dateformat仅dd / MM / yyyy没有日期。
string tdate = datetime.now.tostring("dd/MM/yyyy");
when I am inserting into tables I am passing value ctod('"+tdate+"'); 当我插入表时,我传递值ctod('“ + tdate +”');
string query = "select * from table1 where ordcust='" + account + "'";
OpenConnection();
OleDbCommand cmd = new OleDbCommand(query, con);
if (ds.Tables[0].Rows.Count > 0)
{
string ordcust = dr[0].ToString();
string ordnum = dr[1].ToString();
DateTime orddate = DateTime.Now;
string vara=orddate.ToString("dd/MM/yyyy");
string cs = "insert into resulttable(ordercustomer,ordnumber,orddate) values ('" + ordcust + "','" + ordnum + "', ctod('" + vara + "') )";
}
I strongly suspect you are a victim of choosing the wrong data type . 我强烈怀疑您是选择错误数据类型的受害者。 You should never store your
DateTime
values with their string
representations. 您永远不要将
DateTime
值及其string
表示形式存储在一起。 Just pass their values directly in a parameterized query . 只需直接在参数化查询中传递它们的值即可 。
My suggestions; 我的建议;
DBTYPE_DATE
on for OleDb Type which is mapped System.DateTime
in .NET side which is mapped OleDbType.Date
in parameter type. System.DateTime
OleDb Type定义DBTYPE_DATE
,在参数类型映射为OleDbType.Date
.NET端。 DateTime.Now
directly to your parameterized query. DateTime.Now
直接传递给参数化查询。 Also be aware of: The case against DateTime.Now
另请注意: 针对
DateTime.Now
的案例
What does it mean that you "converted" FoxPro tables using OLEDB? 您使用OLEDB“转换”了FoxPro表是什么意思? Your code doesn't look right, not only for FoxPro but for any SQL database.
您的代码看起来不正确,不仅对于FoxPro,而且对于任何SQL数据库。 You should use parameters anyway.
无论如何,您都应该使用参数。 It would look like:
它看起来像:
using (var con = new OleDbConnection(@"Provider=VFPOLEDB;Data Source=c:\Path To Your Data\"))
{
string query = "select * from table1 where ordcust=?";
con.Open();
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Parameters.AddWithValue("p1", account);
var reader = cmd.ExecuteReader();
if (reader.Read())
{
string ordcust = (string)dr[0];
string ordnum = (string)dr[1];
DateTime orddate = DateTime.Now;
string vara = orddate.ToString("dd/MM/yyyy");
string cs = "insert into resulttable (ordercustomer,ordnumber,orddate) values (?, ?, ?)";
var insertCommand = new OleDbCommand(cs, con);
insertCommand.Parameters.AddWithValue("p1", ordcust);
insertCommand.Parameters.AddWithValue("p2", ordnum);
insertCommand.Parameters.AddWithValue("p3", orddate);
insertCommand.ExecuteNonQuery();
}
con.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.