以前测试过vbs脚本新增和删除数据库的脚本,今天是用c脚本做一次。假设sql server平台已经启用了sa用户,我们先判断一个数据库NewDB是否存在,如果不存在就创建它。在按钮的点击事件中添加以下脚本:
#include "apdefap.h"void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)
{
__object *cn, *rs;
char *connstr="Provider = SQLOLEDB.1;password = sa;user id = sa;Initial Catalog =master;Data Source=ES\\wincc";
char *sql="select name from master.dbo.sysdatabases where name ='NewDB'";
char *sql1="Create Database NewDB";
cn=__object_create("ADODB.Connection");
rs=__object_create("ADODB.RecordSet");
cn->open(connstr);
if (cn->State == 0)// 为0表示连接数据库不成功
MessageBox(NULL,"连接数据库失败!","警告",MB_OK);
else
{
rs->Open(sql,cn,1,3);
if (rs->RecordCount()>0)
{
MessageBox(NULL,"NewDB数据库已经存在!","注意",MB_OK);
rs->Close;
}
else
{
rs->Close;
rs->Open(sql1,cn);
rs->Close;
}
}
if(cn->State != 0)
cn->Close;
}
如果是需要删除数据库,那么在按钮的点击事件中添加以下脚本:
#include "apdefap.h"void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)
{
__object *cn, *rs;
char *connstr="Provider = SQLOLEDB.1;password = sa;user id = sa;Initial Catalog =master;Data Source=ES\\wincc";
char *sql="select name from master.dbo.sysdatabases where name ='NewDB'";
char *sql1="drop Database NewDB";
cn=__object_create("ADODB.Connection");
rs=__object_create("ADODB.RecordSet");
cn->open(connstr);
if (cn->State == 0)// 为0表示连接数据库不成功
MessageBox(NULL,"连接数据库失败!","警告",MB_OK);
else
{
rs->Open(sql,cn,1,3);
if (rs->RecordCount()>0)
{
rs->Close;
rs->Open(sql1,cn);
rs->Close;
}
else
{
MessageBox(NULL,"NewDB数据库不存在!","注意",MB_OK);
rs->Close;
}
}
if(cn->State != 0)
cn->Close;
}