// Open titles table, casting Connection pointer to an
// IDispatch type so converted to correct type of variant.
TESTHR(pRstTitles.CreateInstance(__uuidof(Recordset)));
pRstTitles->Open ("Titles", _variant_t((IDispatch *) pConnection,
true), adOpenStatic, adLockOptimistic, adCmdTable);
pConnection->Errors->Clear();
pCmdChange->Execute(NULL, NULL, adCmdText);
// Retrieve the current data by requerying the recordset.
pRstTitles->Requery(adCmdUnknown);
TESTHR(pRstRoySched->Open("SELECT * FROM roysched WHERE"
" royalty = 20",strCnn,adOpenStatic,adLockBatchOptimistic,
adCmdText));
// Prompt for a record to delete.
printf("Before delete there are %d titles with 20 percent "
"royalty :\n", pRstRoySched->RecordCount);
// Open an IADORecordBinding interface pointer which we'll use
// for Binding Recordset to a class.
TESTHR(pRstRoySched->QueryInterface(
__uuidof(IADORecordBinding),(LPVOID*)&picRs));
// Bind the Recordset to a C++ Class here
TESTHR(picRs->BindToRecordset(&royschrs));
// Delete the record
pRstRoySched->Delete(adAffectCurrent);
pRstRoySched->UpdateBatch(adAffectCurrent);
}
else
{
blnFound = FALSE;
printf("This Title ID not available");
}
// Show the results.
VARIANT varFilter;
varFilter.vt = VT_I2;
varFilter.iVal = adFilterNone;
pRstRoySched->PutFilter (varFilter);
pRstRoySched->Requery(-1);
// Bind the Recordset to a C++ Class here.
TESTHR(picRs->BindToRecordset(&royschrs));
printf("\nAfter delete there are %d titles with 20 percent"
" royalty: ", pRstRoySched->RecordCount);
// Restore the data because this is a demonstration.
if(blnFound)
{
// Set the final character of the destination string to NULL.
royschrs.m_sz_titleid[sizeof(royschrs.m_sz_titleid)-1] = '\0';
// The source string will get truncated if its length is
// longer than the length of the destination string minus one.
strncpy(royschrs.m_sz_titleid,strTmpTitleID,sizeof(royschrs.m_sz_titleid)-1);
royschrs.m_sz_lorange = intLoRange;
royschrs.m_sz_hirange = longHiRange;
royschrs.m_sz_royalty = intRoyalty;
TESTHR(picRs->AddNew(&royschrs));
pRstRoySched->UpdateBatch(adAffectCurrent);
}
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstRoySched->GetActiveConnection();
// GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
// Clean up objects before exit.
//Release the IADORecordset Interface here
if (picRs)
picRs->Release();
if (pRstRoySched)
if (pRstRoySched->State == adStateOpen)
pRstRoySched->Close();
}
//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='MySqlServer';"
"Initial Catalog='pubs';Integrated Security='SSPI';");
_bstr_t strId;
_bstr_t strMessage;
try
{
//Open a connection
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->Open(strCnn,"","",adConnectUnspecified);
//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: ");
myscanf(emprs.m_sz_empid, sizeof(emprs.m_sz_empid));
strId = emprs.m_sz_empid;
printf("Enter First Name: ");
myscanf(emprs.m_sz_fname, sizeof(emprs.m_sz_fname));
printf("Enter Last Name:");
myscanf(emprs.m_sz_lname, sizeof(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);
}
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;
}
}
// Clean up objects before exit.
//Release the IADORecordset Interface here
if (picRs)
picRs->Release();
if (pRstEmployees)
if (pRstEmployees->State == adStateOpen)
pRstEmployees->Close();
if (pConnection)
if (pConnection->State == adStateOpen)
pConnection->Close();
}
'添加前判断有否同类名
Dim a As String
a = "kind='" & Trim(Text1) & "'"
rs_cargo.FindFirst a
If rs_cargo.NoMatch = False Then '找到同名 类别
MsgBox "该类别已存在!", vbOKOnly + vbInformation, "注意"
Text1.SetFocus
Exit Sub
End If
'没同名类别 可以 update
'进行update操作
//////////////
水银 你的例子很长啊,但偏偏就缺了find
你说的:"列名称只能是单列的名称,不支持多列搜索"是什么意思啊?是否不能find例如 班别='1班' and 性别='男' and 成绩>80 等之类的情