CString sql_str;
sql_str.Format(_T("delete from customer where cust_id = %d"), cust_id);
m_db_opr.ExecuteSQL(sql_str);
现在不用了,我们使用CRecordset类的这几个函数来实现这一切:
AddNew Prepares for adding a new record. Call Update to complete the addition.
Cancel Update Cancels any pending updates due to an AddNew or Edit operation.
Delete Deletes the current record from the recordset. You must explicitly scroll to another record after the deletion.
Edit Prepares for changes to the current record. Call Update to complete the edit.
Update Completes an AddNew or Edit operation by saving the new or edited data on the data source.
class Ccustomer : public CRecordset
{
public:
Ccustomer(CDatabase* pDatabase = NULL);
DECLARE_DYNAMIC(Ccustomer)
// Field/Param Data
在customer.c里面有如此一段:
#error Security Issue: The connection string may contain a password
// The connection string below may contain plain text passwords and/or
// other sensitive information. Please remove the #error after reviewing
// the connection string for any security related issues. You may want to
// store the password in some other form or use a different user authentication.
CString Ccustomer::GetDefaultConnect()
{
return _T("DSN=chh1;DESCRIPTION={\x8fd9\x4e2a\x662fchh1 database table};SERVER=127.0.0.1;UID=zhoutianzuo;PWD=000000;DATABASE=chh1;PORT=3306");
}
void Ccustomer::DoFieldExchange(CFieldExchange* pFX)
{
pFX->SetFieldType(CFieldExchange::outputColumn);
// Macros such as RFX_Text() and RFX_Int() are dependent on the
// type of the member variable, not the type of the field in the database.
// ODBC will try to automatically convert the column value to the requested type
RFX_Int(pFX, _T("[cust_id]"), db_cust_id);
RFX_Text(pFX, _T("[cust_name]"), db_cust_name);
}
void Cmy_dbDlg::OnBnClickedAddNew()
{
// TODO: Add your control notification handler code here
int cust_id = 0 ;
CString cust_name;
if(!GetCustIdFromEdit(cust_id))
{
AfxMessageBox(_T("NULL cust id, return"));
return;
}
if(!m_cust_name.GetWindowTextLengthW())
{
AfxMessageBox(_T("NULL cust name, return"));
return;
}
m_cust_name.GetWindowTextW(cust_name);
Ccustomer my_record(&m_db_opr);
try
{
my_record.Open(CRecordset::snapshot, _T("customer"));
my_record.AddNew();
my_record.db_cust_id = cust_id;
my_record.db_cust_name = cust_name;
if(!my_record.Update())
{
AfxMessageBox(_T("Add New failed!"));
}
}
catch(CDBException* pe)
{
// The error code is in pe->m_nRetCode
pe->ReportError();
pe->Delete();
}
}
还是解释一下代码,不管是增删还是修改,首先必须打开数据库先,修改和删除需要先判断条目是否存在,而增加就不用了,如果不允许重复条目,而插入了重复条目,也会抛出异常的,但是没有关系,就权当是一个提醒,没有任何危害。而上面的Open函数,就相当的需要解释了,在绑定了数据库变量后,第二个参数仅仅需要给出一个数据库名字customer,相当于是 “"select cust_id, cust_name
from customer",至于这个Open该怎么输入,具体有MSDN为参考:
The lpszSQL Parameter and the Resulting SQL String
Case
What you pass in lpszSQL
The resulting SELECT statement
1
NULL
SELECTrfx-field-listFROMtable-name CRecordset::Open callsGetDefaultSQL to get the table name. The resulting string is one of cases 2 through 5, depending on whatGetDefaultSQL returns.
2
A table name
SELECTrfx-field-listFROMtable-name
The field list is taken from the RFX statements inDoFieldExchange. Ifm_strFilter andm_strSort are not empty, adds theWHERE and/orORDER BY
clauses.
3 *
A complete SELECT statement but without aWHERE orORDER BY clause
As passed. If m_strFilter andm_strSort are not empty, adds theWHERE and/orORDER BY clauses.
4 *
A complete SELECT statement with aWHERE and/orORDER BY clause
As passed. m_strFilter and/orm_strSort must remain empty, or two filter and/or sort statements are produced.
5 *
A call to a stored procedure
As passed.
* m_nFields must be less than or equal to the number of columns specified in theSELECT statement. The data type of each column specified in theSELECT statement must
be the same as the data type of the corresponding RFX output column.