vc+ado动态创建access数据库

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 mynote 的 blog

vc+ado动态创建access数据库

ado技术目前已经成为连接数据库的主流技术,下面我来介绍如何使用ado来动态创建access数据库。

为了使用ado,必须引入微软的两个动态连接库msadox.dll和msado15.dll:

#pragma warning (disable: 4146)
#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")
#pragma warning (default: 4146)

将上述代码加入到stdafx.h文件中,由于ado是com组件,因此使用ado之前还要初始化com环境:

coinitialize(null);



下面是一个在access数据库中创建表的sql语句的例子:

create table test
(
nametext(20) with compression not null,

scoredecimal(12,4) not null default 0,

id smallint not null default 0,

birthday date,

sex char(1),

constraint ck_ch_sex check(sex in ('n','v')),

constraint pk_id primary key (id)

);



使用adox::catalogptr来创建mdb文件:

hresult hr = s_ok;

//set activeconnection of catalog to this string
cstring strcnn(_t("provider=microsoft.jet.oledb.4.0;data source = d:\\test.mdb"));


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)
{
 // notify the user of errors if any.
 afxmessagebox(_t(“error“));
}



创建一个连接对象打开刚刚建立的mdb文件:

_connectionptr g_pconn;

g_pconn.createinstance(__uuidof(connection));

g_pconn->open("provider=microsoft.jet.oledb.4.0;data source=d:\\test.mdb;",
"", "", adconnectunspecified);



下面函数用来执行sql文件中的sql语句:

// function name : runsqlscript
// description: 执行sql脚本, peckermen@163.com, 2003-09-15
// return type: bool 成功返回true
// argument : lpcstr sqlscriptfile sql脚本文件名
// argument : char separator sql 分割符号, 缺省';'
// argument : char remarksql 注释符号, 缺省'-'
bool runsqlscript(lpcstr sqlscriptfile, char separator = ';', char remark = '-')
{
bool bret = false;
cfilefind finder;
cstring errlong;
if (finder.findfile(sqlscriptfile) == true)
{
cfile fsql;
tchar *buffer, *psql, *p;

fsql.open(sqlscriptfile,cfile&::moderead);
uint nfilelength = fsql.getlength();

buffer = (tchar *)malloc((nfilelength + 1) * sizeof(tchar));
_tcsnset(buffer, tchar('\0'), nfilelength + 1);

uint nbytesread = fsql.read(buffer,nfilelength); //把sql文件内容读入内存缓冲区
fsql.close();

p = psql = buffer;
bool brunok = false;
bool binnote = false;
bool bskip = false;
cstring strsql;
tchar ch;
errlog = _t("");
while (p < (buffer + nfilelength)){
//判断是否是注释行
 if (binnote)
 {
if (*p == tchar('\x0a'))
 binnote = false;
 }
 else
 {
if ((*p == remark) && (*(p+1) == remark))
{
 binnote = true;
 p++;
}
else
{
//判断是否是sql语句结束标志
 if (*p == separator){
strsql = _t("");
bskip = false;
while (psql < p)
{
 if (bskip == false){
if ((*(psql) == remark) &&(*(psql+1) == remark))
{
 bskip = true;
 psql++;
}
else
{
 ch = *psql;
 strsql = strsql+ch;
}
 }
 else{
if (*psql = tchar('\x0a')){
 bskip = false;
 ch = tchar(' ');
 strsql = strsql + ch;
}
 }

 psql++;
}
psql = p + 1;
errlog = errlog + _t("-- sql running ...\n");
errlog = errlog + strsql;

_variant_t vrecords;

m_nrecordsaffected = 0;

try
{
 g_pconn->cursorlocation = aduseclient;
 g_pconn->execute(_bstr_t((lpctstr)strsql), &vrecords, adexecutenorecords);
 m_nrecordsaffected = vrecords.ival;
 brunok = true;
}
catch(_com_error &e)
{
 brunok = false;
}

if (brunok)
 errlog = errlog + _t("\n-- successed!\n");
else
{
 errlog = errlog + _t("\n-- failed!\n");
 break;
}

 }
}
 }
 p++;
}
free(buffer);
bret = brunok;
}
return bret;
}



调用runsqlscript创建access数据库中的表:

runsqlscript(_t("d:\\test.sql"));



关闭数据库连接:

g_pconn->close();

::couninitialize();



以上代码演示了怎样在vc中利用ado根据sql文件动态创建一个access数据库,欢迎指正。

本文关键:vc+ado动态创建access数据库
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top