Home > Software engineering >  VC with the database connection error 80040 e4d
VC with the database connection error 80040 e4d

Time:09-24

 
_ConnectionPtr pConnection;
Try
{
Retrieves the hr=pConnection. CreateInstance (" ADODB. Connection ");//create a Connection object
If (SUCCEEDED (hr))
{
Hr=pConnection - & gt; Open (" driver={SQL Server}; Server=(local); The DATABASE=master; UID=sa; The PWD=888888 ", ""," ", adModeUnknown);
}
}
Catch _com_error (e)//COM error, when perform the function of COM, if wrong, you can capture the _com_error exception
{
Cstrings strComError;
StrComError. Format (" wrong number: 08 lx \ % n error message: % s \ n error source: % s \ n error description: % s ",
E.E rror (),//error Numbers
E.E rrorMessage (),//the error information
(LPCSTR) e.S ource (),//error source
(LPCSTR) e.D escription ());//error description

: : MessageBox (NULL, strComError, "error", MB_ICONEXCLAMATION);
}
//-- -- -- -- -- - whether the database is
_RecordsetPtr pRecordset;
PRecordset. CreateInstance (" ADODB. You ");
_variant_t vCount;
Try
{
_variant_t ra;
PRecordset=pConnection - & gt; Execute (" SELECT COUNT (*) FROM sysdatabases WHERE name='HMSys' ", & amp; Ra, adCmdText);
VCount=pRecordset - & gt; GetCollect ((_variant_t) (long) (0));
}
Catch _com_error (e)
{
AfxMessageBox (" error!" );
}
CString str;
STR. The Format (" % d ", vCount. IVal);
If (0==vCount. IVal)
{
Cstrings Sql;
Sql. The Format (" USE MASTER RESTORE DATABASE HMSys FROM DISK='% s' WITH MOVE' HMSys_Data 'TO' c: \ \ HMSys. MDF 'MOVE' HMSys_Log 'TO' c: \ \ HMSys. LDF ", FindFilePath ());
Try
{
_variant_t ra;
PRecordset=pConnection - & gt; Execute ((_bstr_t) Sql, & amp; Ra, adCmdText);
PRecordset - & gt; Close ();
}
Catch _com_error (e)
{

}
}
PConnection - & gt; Close ();
///////////////
Retrieves the hr=m_pConnection. CreateInstance (" ADODB. Connection ");//create a Connection object
Try
{
If (SUCCEEDED (hr))
{
Hr=m_pConnection - & gt; Open (" driver={SQL Server}; Server=. The DATABASE=HMSys; UID=sa; The PWD=", ""," ", adModeUnknown);
}
}
Catch _com_error (e)//COM error, when perform the function of COM, if wrong, you can capture the _com_error exception
{
Cstrings strComError;
StrComError. Format (" wrong number: 08 lx \ % n error message: % s \ n error source: % s \ n error description: % s ",
E.E rror (),//error Numbers
E.E rrorMessage (),//the error information
(LPCSTR) e.S ource (),//error source
(LPCSTR) e.D escription ());//error description
: : MessageBox (NULL, strComError, "error", MB_ICONEXCLAMATION);
}
Above is a c + + to connect to the database code, c + + runtime environment is vc + + 6.0, the database is SQL sever 2008,
Runtime error always

Ignore and continue to run the program, and

Because just contact database SQL in recent days, don't understand what's going on, check on baidu, no problem, ask everybody to help, don't appreciate!!

CodePudding user response:

Build a SQL account, log in with your own SQL account login password,

CodePudding user response:

take others encapsulated SQL input parameters to connect you

CodePudding user response:

Please use the try... Catch catch the exception,

CodePudding user response:

Your SQL server database authentication is "Windows authentication mode" or "mixed mode"? If is "Windows authentication mode" c + + program is not on,

CodePudding user response:

Just tested on my computer can connect, you try
 
//ADO. H
# include & lt; Windows. H>
# import "X: \ \ Program Files \ \ Common Files \ \ System \ \ ado \ \ msado15 DLL" no_namespace rename (" EOF ", "adoEOF")//SQL installation path
# include & lt; Iostream>
using namespace std;
The class ADO
{
Public:
//the constructor to initialize the environment, connect to the database
ADO (ServerName TCHAR */* */server name,
TCHAR database name * * pDatabase/*/,
TCHAR * pUser/* username */,
TCHAR * pWorrd/* */password,
Data table name string DateName/* */);

//the destructor, disconnect database, release the environment
~ ADO ();
Public:
_RecordsetPtr GetRecordset (_bstr_t/* to execute SQL statements */);
LONG GetCount ();
TCHAR * GetVariantToChar (variant_t/* conversion value */a);
TCHAR * GetItemValue (long/* get a column data */a);
TCHAR * GetItemValue (TCHAR field name * * szName/*/);
VOID AddValue (TCHAR * szName, TCHAR * szFuntion, TCHAR * szDesc, TCHAR * szReturnV);
VOID SelectForm (TCHAR to query table * * szTable/*/,
TCHAR field name * * szWhereName/*/,
TCHAR * szWhereValue/* */);

VOID SelectForm szTable (TCHAR */* */to query table).
VOID DeleteForm (TCHAR * szTable,/* */
TCHAR field name * * szWhereNmae/*/,
TCHAR * szWhereValue/* value */);

VOID UpdateValue (TCHAR * szTable/* */table,
TCHAR field name * * szWhereNmae/*/,
TCHAR * szWhereValue/* */condition values,
TCHAR * szDataName to modify the field name */,/*
TCHAR to change the value of the * * szDataValue/*/);

//private:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
LONG loNameCount;/* the number of fields */
};

//ADO. CPP
# include "ADO. H"

ADO, ADO (ServerName TCHAR */* */server name,
TCHAR database name * * pDatabase/*/,
TCHAR * pUser,/* username */
TCHAR * pWorrd,/* */password
Data table name string DateName/* */)
{
Try {
//initialize the COM environment
: : CoInitialize (NULL);
//create a connection object
M_pConnection. CreateInstance (__uuidof (Connection));
//connect to the database
TCHAR * pOpen=new TCHAR [1024].
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related