⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addnewx.cpp

📁 visual c++ 很全的ado的sample
💻 CPP
字号:
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
    no_namespace rename("EOF", "EndOfFile")


#include <ole2.h>
#include <stdio.h>
#include "conio.h"
#include "AddNewX.h"

//Function declaration
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void AddNewX(VOID);
void PrintProviderError(_ConnectionPtr pConnection);

///////////////////////////////////////////////////////////
//                                                       //
//      Main Function                                    //
//                                                       //
///////////////////////////////////////////////////////////
void main()
{
	HRESULT  hr = S_OK;

	if(FAILED(::CoInitialize(NULL))) 
		return; 

	if (SUCCEEDED(hr))
	{
 		AddNewX();

		//Wait here for the user to see the output
		printf("Press any key to continue...");
		getch();

    	::CoUninitialize();
	}
}


///////////////////////////////////////////////////////////
//                                                       //
//      AddNewX Function                                 //
//                                                       //
///////////////////////////////////////////////////////////
VOID  AddNewX(VOID)
{

	// Define ADO object pointers.
    // Initialize pointers on define.
	_RecordsetPtr pRstEmployees = NULL;
	_ConnectionPtr pConnection  = NULL;

	//Define Other variables
	IADORecordBinding   *picRs = NULL;  //Interface Pointer declared.(VC++ Extensions)   
	CEmployeeRs emprs;    //C++ class object   

	HRESULT hr = S_OK;

	                        //Replace Data Source value with your server name.
	_bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;Initial Catalog=pubs;User Id=sa;Password=;");
	_bstr_t strId;
 	_bstr_t strMessage;
	
	try
	{
		//Open a connection
		TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
		hr = pConnection->Open(strCnn,"","",NULL);
		

		//Open employee table 
		TESTHR(pRstEmployees.CreateInstance(__uuidof(Recordset)));

		//You have to explicitly pass the Cursor type and LockType to the Recordset here
		pRstEmployees->Open("employee",_variant_t((IDispatch *) pConnection, true),adOpenKeyset,adLockOptimistic,adCmdTable);
		

		//Open an IADORecordBinding interface pointer which we'll use for Binding Recordset to a class    
		TESTHR(pRstEmployees->QueryInterface(__uuidof(IADORecordBinding),(LPVOID*)&picRs));


	    //Bind the Recordset to a C++ Class here   
		TESTHR(picRs->BindToRecordset(&emprs));		 

		// Get data from the user.The employee id must be formatted as
		// first,middle and last initial,five numbers,then M or F to
		// signify the gender.For example,the employee id for 
		// Bill A. Sorensen would be "BAS55555M". 
		 	
		printf("Enter Employee Id: ");scanf("%s",emprs.m_sz_empid);
		strId = emprs.m_sz_empid;
		printf("Enter First Name: ");scanf("%s",emprs.m_sz_fname);
		printf("Enter Last Name:");scanf("%s",emprs.m_sz_lname);

		//Proceed if the user actually entered some thing  
		//for the id, the first and the last name. 
		 
		if(strcmp(emprs.m_sz_empid,"")  && strcmp(emprs.m_sz_fname,"") && strcmp(emprs.m_sz_lname,""))
		{
			//This adds a new record to the table   
		 		//if (FAILED(hr = picRs->AddNew(&emprs)))
				//_com_issue_error(hr);
				TESTHR(picRs->AddNew(&emprs));
		
				//Show the newly added data
		 		printf("New Record: %s  %s  %s \n",\
					emprs.lemp_empidStatus == adFldOK ? emprs.m_sz_empid : "<NULL>",\
					emprs.lemp_fnameStatus == adFldOK ? emprs.m_sz_fname : "<NULL>",\
					emprs.lemp_lnameStatus == adFldOK ? emprs.m_sz_lname : "<NULL>");
		}
		else
			printf("Please enter an employee id, first name and last name.\n");	
		 
			
		
		//Delete the new record because this is a demonstration. 
		pConnection->Execute("DELETE FROM EMPLOYEE WHERE emp_id = '"+strId+"'",NULL,adCmdText);

		
		//Release the IADORecordset Interface here   
		if (picRs)
		picRs->Release();

		// Clean up objects before exit.
		pRstEmployees->Close();
		pConnection->Close(); 

	}


	catch(_com_error &e)
	{
		// Notify the user of errors if any.
		_variant_t vtConnect = pRstEmployees->GetActiveConnection();

		// GetActiveConnection returns connect string if connection
		// is not open, else returns Connection object.
		switch(vtConnect.vt)
		{
		case VT_BSTR:
				printf("Error:\n");
				printf("Code = %08lx\n", e.Error());
				printf("Message = %s\n", e.ErrorMessage());
				printf("Source = %s\n", (LPCSTR) e.Source());
				printf("Description = %s\n", (LPCSTR) e.Description());
				break;
		case VT_DISPATCH:
				PrintProviderError(vtConnect);
				break;
		default:
				printf("Errors occured.");
				break;
		}

	}


}


///////////////////////////////////////////////////////////
//                                                       //
//      PrintProviderError Function                      //
//                                                       //
///////////////////////////////////////////////////////////

VOID PrintProviderError(_ConnectionPtr pConnection)
{
	// Print Provider Errors from Connection object.
	// pErr is a record object in the Connection's Error collection.
	ErrorPtr  pErr = NULL;
	long      nCount = 0;    
	long      i = 0;

	if( (pConnection->Errors->Count) > 0)
	{
		nCount = pConnection->Errors->Count;
		// Collection ranges from 0 to nCount -1.
		for(i = 0; i < nCount; i++)
		{
			pErr = pConnection->Errors->GetItem(i);
			printf("\n\t Error number: %x\t%s", pErr->Number, (LPCSTR)pErr->Description);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -