SqlPipe.cpp
// Execute the given SQL against _serverInstanceName
Execute the given SQL against _serverInstanceName
Execute the given SQL against _serverInstanceName
DWORD executeSql(TCHAR* sql)
{
	HRESULT hr;
	_ConnectionPtr conn;
	hr = conn.CreateInstance(__uuidof(Connection));
	if (FAILED(hr))
	{
		err(L"ADODB.Connection CreateInstance failed: %s", (TCHAR*)errorMessage(hr));
		return hr;
	}
	_bstr_t serverName = "lpc:.";
	if (_serverInstanceName != NULL)
		serverName += "\\" + _bstr_t(_serverInstanceName);
	try
	{
		conn->ConnectionString = "Provider=SQLOLEDB; Data Source=" + serverName + "; Initial Catalog=master; Integrated Security=SSPI;";
		conn->ConnectionTimeout = 25;
		conn->Open("", "", "", adConnectUnspecified);
	}
	catch(_com_error e)
	{
		err(L"\nFailed to open connection to '" + serverName + L"': ");
		err(L"%s [%s]\n", (TCHAR*)e.Description(), (TCHAR*)e.Source());
		return e.Error();
	}
	try 
	{	
		variant_t recordsAffected; 
		conn->CommandTimeout = 0;
		conn->Execute(sql, &recordsAffected, adExecuteNoRecords);
		conn->Close();
	}
	catch(_com_error e)
	{
		err(L"\nQuery failed: '%s'\n\n%s [%s]\n", sql, (TCHAR*)e.Description(), (TCHAR*)e.Source());
		conn->Close();
		return e.Error();
	}
	return 0;
}
DWORD executeSql ( TCHAR * sql ) { HRESULT hr ; _ConnectionPtr conn ; hr = conn . CreateInstance ( __uuidof ( Connection ) ) ; if ( FAILED ( hr ) ) { err ( L" " , ( TCHAR * ) errorMessage ( hr ) ) ; return hr ; } _bstr_t serverName = " " ; if ( _serverInstanceName != NULL ) serverName += " \\ " + _bstr_t ( _serverInstanceName ) ; try { conn -> ConnectionString = " " + serverName + " " ; conn -> ConnectionTimeout = 25 ; conn -> Open ( " " , " " , " " , adConnectUnspecified ) ; } catch ( _com_error e ) { err ( L" \n " + serverName + L" " ) ; err ( L" \n " , ( TCHAR * ) e . Description ( ) , ( TCHAR * ) e . Source ( ) ) ; return e . Error ( ) ; } try { variant_t recordsAffected ; conn -> CommandTimeout = 0 ; conn -> Execute ( sql , & recordsAffected , adExecuteNoRecords ) ; conn -> Close ( ) ; } catch ( _com_error e ) { err ( L" \n \n \n \n " , sql , ( TCHAR * ) e . Description ( ) , ( TCHAR * ) e . Source ( ) ) ; conn -> Close ( ) ; return e . Error ( ) ; } return 0 ; }
Execute the given SQL against _serverInstanceName
Execute the given SQL against _serverInstanceName
//log(L"\nexecuteSql '%s'\n", sql); // Use '_Connection_Deprecated' interface for maximum MDAC compatibility // BTW: Windows 7 SP1 introduced _Connection_Deprecated: See: //   http://social.msdn.microsoft.com/Forums/en/windowsgeneraldevelopmentissues/thread/3a4ce946-effa-4f77-98a6-34f11c6b5a13 // and  //   http://support.microsoft.com/kb/2517589 // "lpc:..." ensures shared memory... //log(L"> Connect: %s\n", (TCHAR*)conn->ConnectionString); //log(L"> SQL: %s\n", sql); //err(L"  Errors:\n", conn->Errors->Count); //for (int i = 0; i < conn->Errors->Count; i++) //	err(L"  - %s\n\n", (TCHAR*)conn->Errors->Item[i]->Description);