/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL)
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"
//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"
_ConnectionPtr m_pConnection(_uuidof(Connection))
m_pConnection->Open(varSource,"","",adModeUnknow)
_RecordsetPtr m_pSet(_uuid(Recordset))
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr()
adOpenDynamic,adLockPessimistic,adCmdText)
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage())
return
}
_variant_t var
CString %%2=""
long fldc=m_pSet->GetFields()->GetCount()
long i=0
try {
m_pSet->MoveFirst()
if(!m_pSet->adoEOF)
{
for(i=0i<fldci++)
{
var=m_pSet->GetCollect((long)i)
var.ChangeType(VT_BSTR)
%%2+=var.bstrVal
%%2+=" "
}
//m_pSet->MoveNext()
}
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage())
delete e
}
//m_pSet->MoveFirst()
CoUninitialize(NULL)
2.单值比较
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL)
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"
//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"
_ConnectionPtr m_pConnection(_uuidof(Connection))
m_pConnection->Open(varSource,"","",adModeUnknow)
_RecordsetPtr m_pSet(_uuid(Recordset))
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText)
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage())
return
}
_variant_t var
try {
m_pSet->MoveFirst()
if(!m_pSet->adoEOF)
{
var=m_pSet->GetCollect((long)0)
var.ChangeType(VT_I2)
int %%3=var.intVal
if(%%3==%%4)
{
%%5
}
//m_pSet->MoveNext()
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage())
delete e
}
//m_pSet->MoveFirst()
CoUninitialize(NULL)
3.显示表格
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL)
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"
//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"
_ConnectionPtr m_pConnection(_uuidof(Connection))
m_pConnection->Open(varSource,"","",adModeUnknow)
//打开属性为默认(adModeRead(只读),adModeWrite(可写),adModeReadWrite(可读写)等)
_RecordsetPtr m_pSet(_uuid(Recordset))
try {
HRESULT hr=m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText)
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage())
}
if(SUCCESSED(hr))
{
//表打开成功
}
FieldsPtr p_fields=m_pSet->Fields
FieldPtr p_field
_variant_t var_index
LPCSTR field_name
int index=0
_bstr_t bstr_field_name
int countfields=p_fields->GetCount()
CString *Column=new CString[countfields]
CListCtrl *pList=(CListCtrl*)GetDlgItem(%%1)//IDC_LIST_TABLEDATA
VERIFY(pList)
pList->DeleteAllItems()
for(index=0index<countfieldsindex++)
{
var_index.vt=VT_I4
var_index.IVal=index
p_field=p_fields->Item[var_index]
bstr_field_name=p_field->GetName()
field_name=(LPCSTR)bstr_field_name
Column[index]=field_name
int ColumnWidth=Column[index].GetLength()*15
pList->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth)
}
int i=0
_bstr_t vCol
//pList->SetTextBkColor(RGB(122,200,122))
//pList->SetTextColor(RGB(0,0,200))
while(!m_pSet->adoEOF)
{
pList->Insert(i,atoi(i))
for(int j=0j<countfieldsj++)
{
vCol=m_pSet->GetCollect((long)j)
pList->SetItemText(i,j,vCol)
}
m_pSet->MoveNext()
i++
}
CoUninitialize(NULL)
22.批量执行SQL和存储过程
CDatabase * pDatabase = new CDatabase
TRY
{
pDatabase->OpenEx( _T("DSN=ODBCNameUID=%%1PWD=%%2"), CDatabase::noOdbcDialog)
}
CATCH (CDBException, e)
{
delete pDatabase
return
}END_CATCH
SQL.Format("exec sp_Name")//有参数的话直接写再后面
pDatabase->ExecuteSQL(SQL)
pDatabase->Close()
delete pDatabase
22.2、用ADO调用存储过程
_ConnectionPtr Conn = NULL
_RecordsetPtr Rs = NULL
_CommandPtr Cmd = NULL
CoInitialize(NULL)
Conn.CreateInstance ( __uuidof(Connection))
Rs.CreateInstance (__uuidof(Recordset))
Cmd.CreateInstance (__uuidof(Command))
Conn->Open(L"db", L"sa", L"", adOpenUnspecified)//打开正常
//如下为_CommandPtr对象参数的赋值和调用
Cmd->ActiveConnection = Conn
Cmd->CommandText = "SP_TEST"
//数据库中实际存在这个测试存储过程,select * from atable,不传递参数
Cmd->CommandType = adCmdStoredProc
Cmd->Parameters->Refresh()
Rs = Cmd->Execute( NULL,NULL, adCmdUnknown ) //COM出错。
//注释掉的代码为直接的SQL语句提交,运行正确。
//Cmd->ActiveConnection=Conn
//Cmd->CommandText = "select * from atable"
//Cmd->CommandType = adCmdText
//Cmd->Parameters->Refresh()
//Rs = Cmd->Execute(NULL,NULL,adCmdUnknown)
Rs->Close()
Conn->Close()
CoUninitialize()
数据库设计的基本步骤如下:
1、安装并打开MySQL WorkBench软件以后,在软件的左侧边栏有三个选项,分别是对应“连接数据库”、“设计数据库”、“迁移数据库”的功能。这类选择第二项,设计数据库,点击右边的“+”号,创建models。
2、进入MySQL Model界面后,点击“Add Diagram"。
3、然后就进入了EER Diagram的设计页面。可以从左侧的图标中选择要创建的资源。鼠标停留在图标上3秒后,会提示资源的类型。选择资源后,可以在设计页面上方选择属性。
4、这里添加了一张数据库表,添加后,双击,在页面的底部会出现数据库表的编辑框。
5、这里创建一张user表,设置了id、name、age三列。
6、还可以创建外键,左侧提供了四种外键类型。点击后,只需要用鼠标分别选中要关联的两个表即可。Workbench会自动生成对应的外键。
7、在文件、导出中,可以选择导出为sql脚本。
8、下图是导出过程sql脚本的过程。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)