BOOLADOConn::ExecuteSQL(_bstr_tbstrSQL){//variant_tRecordsetAffected;try{//是否已经连接数据库if(m_pConnection==NULL)OnInitADOConn();m_pConnection->Execute(bstrSQL,NULL,ad...
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
// variant_t RecordsetAffected;
try
{
//是否已经连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
void CEmployees::sql_insert()
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置INSERT语句
CString strWage;
strWage.Format("%f",Wage);
_bstr_t vSQL;
vSQL="INSERT INTO Employees(Emp_name,Sex,Title,Wage,Dep_id)VALUES('"+Emp_name+"','"+Sex+"','"+Title+"',"+strWage+",1)";
//执行INSERT语句
m_AdoConn.ExecuteSQL(vSQL);
//断开与数据库的链接
m_AdoConn.ExitConnect();
}
看看有什么问题么,为什么连不上,数据不能插入进去~~
void ADOConn::OnInitADOConn()
{
//初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
//创建Connection对象
m_pConnection.CreateInstance("ADODB.Recordset");
//设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect="Provider=SQLOLEDB;Server=SJ-5E2898207121;Database=HrMan;uid=sa;pwd=123456;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
//捕捉异常
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
这是数据库的链接~~~
展开