积极答复者
CString类型的时间字符串如何存入Access时间字段中?

问题
-
我从MFC编辑框获取的CSting类型的日期和时间字符串,分别定义为:CString strDate = L"2011年08月09日";CString strTime = L"18::20:33";
构造SQL插入指令,CSting strSQL; strSQL。Format(L"INSERT INTO 表(Date, Time) VALUES (#%s#, #%s#)", strDate, strTime);运行出错,警告 Access日期语法错误
我又改为CString strDate = L"format('2011年08月09日', 'yyyy-mm-dd')";CString strTime = L'format('18::20:33', 'hh:nn:ss')"; CSting strSQL; strSQL。Format(L"INSERT INTO 表(Date, Time) VALUES (%s, %s)", strDate, strTime);运行后去掉strTime就没问题,加上就出错,
您不一定按我的思路,我主要的文帝纠结于,将CStirng 的日期和时间分别插入Access的时间:长日期和长时间字段中,SQL插入语句怎么写?我用ODBC,语言VC++2010,您当vc++6.0即可。
答案
-
CDatabase db1;
db1.Open(NULL, false, false, _T("ODBC;DSN=SQL;UID=admin;"));CRecordset cd(&db1);
cd.Open(AFX_DB_USE_DEFAULT_TYPE, L"SELECT * FROM [N]");
cd.MoveFirst();
CDBVariant varID, varDate, varTime;
cd.GetFieldValue(L"MYID", varID);
cd.GetFieldValue(L"mDate", varDate);
cd.GetFieldValue(L"mTime", varTime);
if(!cd.IsEOF())
{
CString str = L"";
CTime date(varDate.m_pdate->year, varDate.m_pdate->month, varDate.m_pdate->day,
varTime.m_pdate->hour, varTime.m_pdate->minute, varTime.m_pdate->second);
str.Format(L"ID: %ld, Date:%s", varID.m_lVal, date.Format(L"%y-%m-%d %H:%M:%S"));
MessageBox(str);
}
else
{
MessageBox(L"没找到");
}cd.Close();
db1.Close();- 已标记为答案 Sunrain2011 2011年8月10日 15:55
全部回复
-
INSERT INTO 表(Date, Time) VALUES (?, ?)
之后用command->CreateParameter和command->Parameters->Append添加类型为VT_DATE的参数
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
您好,您能不能给个具体例子?我的时间变量CSrting strTime = L"08:52:55";
CString strDate = L"2011年08月10日";
要构造的SQL插入指令:CString strSQL;
strSQL.Format(L"INSERT INTO 表(Time, Date) VALUES (%s, %s,)", strTime, strDate);
command->CreateParameter和command->Parameters->Append该怎么添加进去?谢谢。
Time:在Access中为长时间,Date:为长日期
-
- 其实我找到一个解决办法:就是修改数据库将日期和时间合并在一起构造时间CString strTime = L"format('2011年08月10日 09:05:33', ‘yyyy-mm-dd hh:mm:ss’)";
CString strSQL; strSQL.Format(L"INSERT INTO 表(Time) VALUES (%s)", strTime); 这条strSQL执行后没问题;
2。只要日期:即CString strTime = L"format('2011年08月10日, ‘yyyy-mm-dd)";CString strSQL; strSQL.Format(L"INSERT INTO 表(Time) VALUES (%s)", strTime); 这样执行也没问题;
3。日期和时间分开:即CString strDate = L"format('2011年08月10日, ‘yyyy-mm-dd)";CString strTime = L'format('18::20:33', 'hh:nn:ss')";CString strSQL; strSQL.Format(L"INSERT INTO 表(Time) VALUES (%s, %s)", strTime, strDate); 这样能把时间插进数据库,但是在此运行就出现:
- 已标记为答案 Sunrain2011 2011年8月10日 1:15
- 取消答案标记 Sunrain2011 2011年8月10日 1:15
-
你上面那里只指定了一列,但却要插入两个值
INSERT INTO 表(Time) VALUES (%s, %s)
- 已标记为答案 Sunrain2011 2011年8月10日 6:00
- 取消答案标记 Sunrain2011 2011年8月10日 6:00
-
strSQL.Format(L"INSERT INTO 表(Time, Date) VALUES ('%s', '%s')", strTime, strDate);走了很多弯路,很无语啊,其他的方法我都试了,为什么这么简单的没试啊,很很感谢各位朋友。
- 已标记为答案 Sunrain2011 2011年8月10日 6:00
- 取消答案标记 Sunrain2011 2011年8月10日 6:26
-
CDatabase db1;
db1.Open(NULL, false, false, _T("ODBC;DSN=SQL;UID=admin;"));CRecordset cd(&db1);
cd.Open(AFX_DB_USE_DEFAULT_TYPE, L"SELECT * FROM [N]");
cd.MoveFirst();
CDBVariant varID, varDate, varTime;
cd.GetFieldValue(L"MYID", varID);
cd.GetFieldValue(L"mDate", varDate);
cd.GetFieldValue(L"mTime", varTime);
if(!cd.IsEOF())
{
CString str = L"";
CTime date(varDate.m_pdate->year, varDate.m_pdate->month, varDate.m_pdate->day,
varTime.m_pdate->hour, varTime.m_pdate->minute, varTime.m_pdate->second);
str.Format(L"ID: %ld, Date:%s", varID.m_lVal, date.Format(L"%y-%m-%d %H:%M:%S"));
MessageBox(str);
}
else
{
MessageBox(L"没找到");
}cd.Close();
db1.Close(); -
CDatabase db1;
db1.Open(NULL, false, false, _T("ODBC;DSN=SQL;UID=admin;"));CRecordset cd(&db1);
cd.Open(AFX_DB_USE_DEFAULT_TYPE, L"SELECT * FROM [N]");
cd.MoveFirst();
CDBVariant varID, varDate, varTime;
cd.GetFieldValue(L"MYID", varID);
cd.GetFieldValue(L"mDate", varDate);
cd.GetFieldValue(L"mTime", varTime);
if(!cd.IsEOF())
{
CString str = L"";
CTime date(varDate.m_pdate->year, varDate.m_pdate->month, varDate.m_pdate->day,
varTime.m_pdate->hour, varTime.m_pdate->minute, varTime.m_pdate->second);
str.Format(L"ID: %ld, Date:%s", varID.m_lVal, date.Format(L"%y-%m-%d %H:%M:%S"));
MessageBox(str);
}
else
{
MessageBox(L"没找到");
}cd.Close();
db1.Close();- 已标记为答案 Sunrain2011 2011年8月10日 15:55