自写数据库操作类

#ifndef _CUSTOM_DATABASE_
#define _CUSTOM_DATABASE_
#endif




#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace \
rename("EOF","adoEOF")rename("BOF","adoBOF")


#import "C:\Program Files\Common Files\System\ado\msADOx.dll"


#import "C:\Program Files\Common Files\System\ado\msADO15.dll" no_namespace rename("EOF", "EndOfFile")






class custom_database
{
public:
custom_database(void);
~custom_database(void);


bool create_database(LPCTSTR name);//后缀为.mdb 创建数据库
bool create_table(LPCTSTR name);//创建数据表
bool ADD_field(LPCTSTR tabel_name,LPCTSTR field_name,int type = 2,int str_len = 100);//添加字段1:BOOL 类型,2:字串类型,3:事件类型,4:整形
bool DEL_field(LPCTSTR tabel_name,LPCTSTR field_name);




//设置主键
bool set_main_key(LPCTSTR table_name,LPCTSTR key_name);//1:添加主键 2:修改主键




//SQL语句执行
BOOL SQL_EXCUTE(LPCTSTR sql);




bool connect_database(LPCTSTR name,LPCTSTR user = L"",LPCTSTR pass = L"");//连接数据库
bool dis_conn(LPCTSTR name);


//字段中插入数据


bool insert_data(LPCTSTR table,LPCTSTR des,LPCTSTR src,LPCTSTR key);
bool change_data(LPCTSTR data);


private:
_ConnectionPtr m_Connection; //数据库连接对象
_RecordsetPtr m_Recordset;  //数据集指针

};





//CPP文件

#include "StdAfx.h"
#include "custom_database.h"
#include <afx.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace \
rename("EOF","adoEOF")rename("BOF","adoBOF")


#import "C:\Program Files\Common Files\System\ado\msADOx.dll"


#import "C:\Program Files\Common Files\System\ado\msADO15.dll" no_namespace rename("EOF", "EndOfFile")






custom_database::custom_database(void)
{
::CoInitialize(NULL);  //初始化COM库
}




custom_database::~custom_database(void)
{
CoUninitialize();  //卸载COM库
}




bool custom_database::create_database(LPCTSTR name)
{
//在指定路径下创建一个空的access数据库
//使用ADOX::CatalogPtr来创建mdb文件:


HRESULT hr = S_OK;
CString strcnn(_T("Provider=Microsoft.JET.OLEDB.4.0;Data source = "));
strcnn+=name;
try  

ADOX::_CatalogPtr m_pCatalog = NULL; 
hr = m_pCatalog.CreateInstance(__uuidof (ADOX::Catalog));
if (FAILED(hr))
{
_com_issue_error(hr);


}
else


{
m_pCatalog->Create(_bstr_t(strcnn)); //Create MDB


}


}


catch(_com_error &e)


{




AfxMessageBox(e.Description());


}


return true;
}




bool custom_database::create_table(LPCTSTR name)//创建数据表
{
try
{
CString  sql;
sql.Format(L"create table %s",name);
m_Connection->Execute((_bstr_t)sql,NULL,adCmdText);
return TRUE;


}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
// TODO: 在此添加控件通知处理程序代码
}






bool custom_database::connect_database(LPCTSTR name,LPCTSTR user,LPCTSTR pass)//连接数据库
{
//SQL 数据库连接方式
/*CString strname;  //尝试连接数据库
strname.Format(L"Provider=SQLOLEDB.1;Password=u_video@123;Persist Security Info=True;User ID=Uvideo123;Initial Catalog=GeKu;Data Source=uvideo.cn,8984");
m_Connection.CreateInstance("ADODB.Connection");
m_Connection->Open((_bstr_t)strname,L"",L"",adConnectUnspecified);*/






try
{
//创建连接对象实例
m_Connection.CreateInstance("ADODB.Connection");
//设置连接字符串


CString strConnect;
strConnect.Format(L"DRIVER={Microsoft Access Driver (*.mdb)};uid=%s;pwd=%s;DBQ=%s;",user,pass,name);
//使用Open方法连接数据库
m_Connection->Open((_bstr_t)strConnect,"","",adModeUnknown);
MessageBox(NULL,L"连接成功!",L"连接提示",MB_OK);
return TRUE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}




bool custom_database::ADD_field(LPCTSTR tabel_name,LPCTSTR field_name,int type,int str_len)//添加字段
{
try
{
CString  sql;
switch (type)
{
case 1:
sql.Format(L"ALTER table %s ADD %s bit;",tabel_name,field_name);
break;
case 2:
sql.Format(L"ALTER table %s ADD %s VARCHAR(%d);",tabel_name,field_name,str_len);
break;
case 3:
sql.Format(L"ALTER table %s ADD %s DATE;",tabel_name,field_name);
break;
case 4:
sql.Format(L"ALTER table %s ADD %s INT;",tabel_name,field_name);
break;
}
m_Connection->Execute((_bstr_t)sql,NULL,adCmdText);
MessageBox(NULL,L"创建字段成功!",L"字段提示",MB_OK);
return TRUE;


}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}




bool custom_database::DEL_field(LPCTSTR tabel_name,LPCTSTR field_name)//删除字段
{
try
{
CString strConnect;
strConnect.Format(L"ALTER TABLE %s DROP COLUMN %s;",tabel_name,field_name);
m_Connection->Execute((_bstr_t)strConnect,NULL,adCmdText);
MessageBox(NULL,L" 删除成功!",L"删除提示",MB_OK);
return TRUE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}




bool custom_database::dis_conn(LPCTSTR name)
{
try
{
m_Connection->Close();
return TRUE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}




BOOL custom_database::SQL_EXCUTE(LPCTSTR sql)
{
try
{
CString strConnect = sql;
m_Connection->Execute((_bstr_t)strConnect,NULL,adCmdText);
return TRUE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}




bool custom_database::set_main_key(LPCTSTR table_name,LPCTSTR key_name)//1:添加主键 2:修改主键
{
try
{
CString strConnect;
strConnect.Format(L"Alter table %s ADD Constraint pk_test_tab Primary KEY(%s);",table_name,key_name);
m_Connection->Execute((_bstr_t)strConnect,NULL,adCmdText);
return TRUE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}






bool custom_database::insert_data(LPCTSTR table,LPCTSTR des,LPCTSTR src,LPCTSTR key)
{
return 0;
}


bool custom_database::change_data(LPCTSTR data)
{
return 0;
}



©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页