终于鼓捣好了……
void CtrynottoabortDlg::OnBnClickedstart()
{
// TODO: 在此添加控件通知处理程序代码
//refreshtime部分-----------------------------------------------------------
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;
m_Hour = t.tm_hour;
m_Minute = t.tm_min;
UpdateData(FALSE);
//connectCString部分-------------------------------------------------------
UpdateData(TRUE);
CString str1("Image");
CString str2, str3;
str2.Format(_T("%d"), m_Year);
str3.Format(_T("%d"), m_Minute);
//test_connect将作为表名,形如:Image201711
test_connect = str1 + str2 + str3;
UpdateData(FALSE);
//showTable部分--------------------------------------------------------------------------------------------------------
::CoInitialize(NULL);//初始化OLE/COM库环境,为访问ADO接口做准备
_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect = "Provider=SQLOLEDB;Server=(local);Database=test;uid=sa;pwd=123654;";
m_pConnection->Open(strConnect, "", "", adModeUnknown);
_bstr_t bstrSql = "select name from sysobjects where xtype='U' ";
//创建记录集
m_pRecordset->Open(bstrSql, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
while (!m_pRecordset->EndOfFile)
{
// UpdateData(TRUE);
TableName = m_pRecordset->GetCollect("name"); // 此处获得名称
TableList.push_back(TableName);
m_pRecordset->MoveNext();
// UpdateData(FALSE);
}
m_pRecordset->Close();
m_pConnection->Close();
::CoUninitialize();
//查找第1张表,验证vector容器正常
TableName = TableList[1];
UpdateData(FALSE);
//SearchTable部分---------------------------------------------------------------------------
auto index = find(TableList.begin(), TableList.end(), test_connect);
if (index != TableList.end())
{
// SearchTable = "找到了!";
// outputtest = "创建成功!";
outputtest = "表已经存在,不创建!";
}
else
{
// SearchTable = "没找到。";
outputtest = "表不存在,即将创建!";
//CreateTable部分----------------------------------------------------------------------
//开始连接字符串
CString ss("select * into ");
CString connect1, createtable;
connect1 = ss + test_connect;
ss = " from Image where 1=2";
createtable = connect1 + ss;
//类型转换
_bstr_t bstrSQL;
bstrSQL = (_bstr_t)createtable;
m_pConnection->Open(strConnect, "", "", adModeUnknown);
m_pConnection->Execute(bstrSQL, NULL, 1); //用Execute执行sql语句来新建
m_pConnection->Close();
::CoUninitialize();
}
UpdateData(FALSE);
vector<CString>().swap(TableList);
}
本来是分块做的,最后集合在一起,调了一些地方,就可以了。
遇到了一些问题,也做一些备忘:
- 连接字符串 √
我把表的名字设成Image201706这种类型 年+月。
CString str1("Image");
CString str2,str3;
str2.Format(_T("%d"), m_Year);
str3.Format(_T("%d"), m_Month);
test_connect = str1 + str2 + str3;
2.尝试create table
不能单纯用create table吧,因为这张新表的结构是要和之前的结构完全一样的。
SELECT * INTO 表2 FROM 表1
//上语句会同时实现全表备份,如果不需要数据,仅需要结构,就
SELECT * INTO 表2 FROM 表1 WHERE 1=2
//后面的where 1=2 只要不成立就行了,比如 where 1=0一样OK
判断表是否存在
IF EXISTS(Select * From sysobjects where xtype='U' AND Object_ID(N'YourTableName')=id)
Print 'Exists'
Else
Print 'Not Exists'
在CreateTable函数中其实我并没有用到m_pRecordset,所以以下这一语句会报错
// m_pRecordset->Close(); 只要有这个我就会中止!!!虽然表仍然可以生成
连接
void CTRYnDlg::OnBnClickedConnectcstring()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
CString str1("Image");
CString str2,str3;
str2.Format(_T("%d"), m_Year);
str3.Format(_T("%d"), m_Minute);
test_connect = str1 + str2 + str3;
UpdateData(FALSE);
}
时间更新
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;
m_Hour = t.tm_hour;
m_Minute = t.tm_min;
// CString cyear;
UpdateData(FALSE);
}