各位大侠,我打算用MFC创建一个数据库,但是不知道如何才能成功?
之前看过<VC数据库编程>,很不幸按照书上例子都没创建成功。有点龊啊。
所以我想请问一下各位:
如果我要创建数据库我的机器上应该数据库的那些软件(我已经安装了MYSQL和ACCESS)?
是否要先用ACCESS创建一个表单呢,才能连接呢?
请给出一个创建的例子?
谢谢。
之前看过<VC数据库编程>,很不幸按照书上例子都没创建成功。有点龊啊。
所以我想请问一下各位:
如果我要创建数据库我的机器上应该数据库的那些软件(我已经安装了MYSQL和ACCESS)?
是否要先用ACCESS创建一个表单呢,才能连接呢?
请给出一个创建的例子?
谢谢。
并且要稍微完整一点。
CDaoDatabase myDatabase;
try
{
myDatabase.Create(文件名,dbLangGeneral,dbVersion30);
CDaoTableDef table1(&myDatabase);
table1.Create("Table111");
//定义字段属性
CDaoFieldInfo fieldinfo;
fieldinfo.m_strName=TableFieldName[i];
fieldinfo.m_nType=dbText;
fieldinfo.m_lSize=20;
fieldinfo.m_lAttributes=dbVariableField;
fieldinfo.m_bAllowZeroLength=TRUE;
table1.CreateField(fieldinfo);
table1.Append();
myDatabase.Close();
}
catch (CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription);
e->Delete();
}
#import "c:\Program Files\Common Files\system\ado\msadox.dll" no_namespace
#import "c:\Program Files\Common Files\system\ado\msado15.dll"
#define TESTHR(x) if FAILED(x) _com_issue_error(x);
#include "iostream.h"
#include "stdio.h"
#include "conio.h"
//Function declarations
void CreateDatabaseX(void);
//------------------------------------------------------------//
//Main Function
//Purpose: Test Driver
//------------------------------------------------------------//
void main()
{
HRESULT hr = S_OK;
hr = ::CoInitialize(NULL);
if(SUCCEEDED(hr))
{
CreateDatabaseX();
//Wait here for the user to see the output
printf("Press any key to continue...");
getch();
::CoUninitialize();
}
}
//------------------------------------------------------------//
//CreateDatabaseX
//Purpose: create a new Jet database with the Create method
//------------------------------------------------------------//
void CreateDatabaseX()
{
HRESULT hr = S_OK;
// Define ADOX object pointers.
// Initialize pointers on define.
// These are in the ADOX:: namespace.
_CatalogPtr m_pCatalog = NULL;
//Set ActiveConnection of Catalog to this string
_bstr_t strcnn("Provider='Microsoft.JET.OLEDB.4.0';"
"Data source = c:\\new.mdb");
try
{
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));
m_pCatalog->Create(strcnn);
printf("Database 'c:\\new.mdb' is created.\n");
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\n\tSource : %s \n\tdescription : %s \n ",
(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}
catch(...)
{
cout << "Error occured in CreateDatabaseX...."<< endl;
}
}
// EndCreateDatabaseCpp //ADO的具体操作可以参考http://www.vckbase.com/document/listdoc.asp?mclsid=11&sclsid=1105
#import "c:\program files\common files\system\ado\msado15.dll"\
no_namespace rename("EOF","ADOEOF")
然后你就可用以下代码对ADO进行初始化了。
/////////////////////////初始化数据库//////////////////////
AfxOleInit();
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance( __uuidof(Connection) );
if(SUCCEEDED(hr))
{CString strConnection
bstr_t strConnect = "Provider=SQLOLEDB; Server=(local); Database=历史数据库; uid=sa; pwd=;"; //连接串还是用你自己的,记得练到NewDatabase上
hr=m_pConnection->Open(strConnect, "", "", NULL);
if (SUCCEEDED(hr))
{
}
else
{
AfxMessageBox("连接高兴数据库出错!请检查配置或网络");
return FALSE;
}
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接错误!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return FALSE;
}
HRESULT hr_record;
//DataRecord.CreateInstance(__uuidof(Recordset));////初始化记录集
m_pcmd.CreateInstance (__uuidof(Command),NULL);
//hr_record=m_precord.CreateInstance(__uuidof(Recordset));
hr_record=DataRecord.CreateInstance(__uuidof(Recordset));////初始化记录集
if(SUCCEEDED(hr_record))
{
MessageBox("打开记录成功");
return TRUE;
}
else
{
return FALSE;
}
http://www.vckbase.com/document/viewdoc/?id=1772魔法单词记忆
——DAO访问ACCESS数据库
http://www.vckbase.com/document/viewdoc/?id=1381
如果是access的话,分为两个步骤
1.创建*.mdb文件,纯粹的文件操作,和数据库无关
2.如果要创建表的话,连接数据库,用create语句创建
你看这书是否适合你 书号是 ISBN7-115-13024-8