MFC->SQL Server
2017-11-04 本文已影响0人
Dorrrris
//按钮响应函数
::CoInitialize(NULL);//初始化OLE/COM库环境,为访问ADO接口做准备
_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
CString ss("select * from 生产记录表");
_bstr_t bstrSQL;
bstrSQL = (_bstr_t)ss;
try
{
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect = "Provider=SQLOLEDB;Server=(local);Database=test;uid=sa;pwd=123654;";
m_pConnection->Open(strConnect, "", "", adModeUnknown);
//创建记录集
m_pRecordset.CreateInstance(_uuidof(Recordset));
//取得表中的记录
m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
_variant_t vSerialNumber, vPTime, vAmount, vSize, vClass, vOK, vNG, vPIC; //对应库中的SerialNumber,PTime,Amount,Size,Class
while (!m_pRecordset->EndOfFile)
{
vSerialNumber = m_pRecordset->GetCollect(_variant_t((long)0));//这儿给字段编号和字段名都可以
vPTime = m_pRecordset->GetCollect("PTime");
vAmount = m_pRecordset->GetCollect("Amount");
vSize = m_pRecordset->GetCollect("Size");
vClass = m_pRecordset->GetCollect("Class");
if (vSerialNumber.vt != VT_NULL && vPTime.vt != VT_NULL && vAmount.vt != VT_NULL
&& vSize.vt != VT_NULL && vClass.vt != VT_NULL)
{
UpdateData(TRUE);
SerialNumber = vSerialNumber;
CurrentTime = (char*)(_bstr_t)vPTime;
Amount = vAmount.lVal;
Size = (char*)(_bstr_t)vSize;
Class = (char*)(_bstr_t)vClass;
UpdateData(FALSE);
}
m_pRecordset->MoveNext(); ///移到下一条记录
}
m_pRecordset->Close(); // 关闭记录集
要求是一个月新建一张表。
如何得到系统时间?
以前抄过一段代码,在VC++6.0中可以用,VS不可以。
#include<time.h>
void CRealtimeTemperatureMeasurementDlg::gettime()
{
time_t nowtime;
struct tm *timeinfo;
time( &nowtime );
timeinfo = localtime( &nowtime );
m_year = timeinfo->tm_year + 1900;
m_month = timeinfo->tm_mon + 1;
m_day = timeinfo->tm_mday;
m_hour = timeinfo->tm_hour;
m_minute = timeinfo->tm_min;
m_second = timeinfo->tm_sec;
}
刷新时间的话:
void CRealtimeTemperatureMeasurementDlg::OnBUTTONrefresh()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
gettime();
UpdateData(FALSE);
}
百度到另外一种,已经验证可以用:https://zhidao.baidu.com/question/1989448216231845747.html
#include <ctime>
void CTRYnDlg::OnBnClickedRefreshtime()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
struct tm t;
time_t now;
time(&now);
localtime_s(&t, &now);
m_Year = t.tm_year + 1900;
m_Month = t.tm_mon + 1;
m_Day = t.tm_mday;
UpdateData(FALSE);
}
下面这种 还没试
SYSTEMTIME st;
CString strDate,strTime;
GetLocalTime(&st);
strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);
strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);