|
Haha, your second problem happened to be solved by me, here is the solution:
The database I use is ACCESS. After checking a lot of information, I realized that ACCESS uses JET SQL, while SQL SERVER uses T-SQL. There is a big difference. JET SQL is quite bad, and many do not support it. I originally wanted to use declare Come to declare the cursor to solve, who knows that ACCESS does not recognize
In the end, I had no choice but to choose the method of generating a temporary table. JET SQL does not support generating a temporary table, so I can only generate a table, and deleting it later is the same.
I use VS + MFC
The main methods are:
1. First determine whether the table tempTable to be generated already exists, if it exists, delete it first (this step is mainly to prevent abnormalities)
2. Then generate a temporary table tempTable, and store the query results in it
3. Add an ID column to this temporary table and set its attribute to "automatic numbering"
4. Finally, this temporary table is passed to the DATAGRID control to display
The code and description are as follows:
m_ptrRecordset-> CursorLocation = adUseClient; //Very important, if not, the data cannot be displayed
m_ptrConnection-> CursorLocation = adUseClient;
CString temp_s=L"";
long l=0;
_variant_t NO;
temp_s.Format(L"SELECT Count(*) AS RTab FROM MSysObjects WHERE (((MSysObjects.Name) Like\"%s\"));",L"tempTable");
//This SQL statement is used to determine whether the tempTable table already exists, and the following is to delete it if it exists
m_ptrRecordset=m_ptrConnection->Execute(_bstr_t(temp_s),COleVariant(l),adCmdText);
NO=m_ptrRecordset->GetCollect(COleVariant(long(0)));
if(NO.vt==VT_I4) //First judge whether there is a tempTable table, if it exists, delete it first
{
if(NO.lVal>0)
{
m_ptrConnection->Execute(_bstr_t(L"drop table tempTable"),COleVariant(l),adCmdText);
}
}
temp_Str.Format(L"select Main.FileName ,Main.InningsNum ,Main.Date ,Main.ItemAttribute INTO tempTable from Main where Main.Date between #%s# and #%s# ORDER BY Main.Id",startTime,endTime );
m_ptrConnection->Execute(_bstr_t(temp_Str),COleVariant(l),adCmdText);
//This SQL statement is to generate a new table tempTable from the query results and save it in the newly generated table
m_ptrConnection->Execute(_bstr_t(L"alter table tempTable add id Counter "),COleVariant(l),adCmdText);
//This statement modifies the newly generated tempTable table and adds a new column named ID to it. The attribute is automatic numbering, namely Counter
//The following article contains the attribute values of all columns in ACCESS
//http://hi.baidu.com/djiznew/item/0faef5eb8ddbede3fa42ba8c
if(m_ptrRecordset->GetState()==adStateOpen) //Determine whether the data set is open, if open, close
{
m_ptrRecordset->Close();
}
HRESULT hr = m_ptrRecordset->Open(_bstr_t(L"select id as [serial number],FileName as [file name],InningsNum as [round time],Date as [match time],ItemAttribute as [item attribute] from tempTable order by id"), _variant_t((IDispatch*)m_ptrConnection, TRUE),adOpenStatic, adLockBatchOptimistic, adCmdUnknown);
if(hr != S_OK)
{
AfxMessageBox(L"create recordset error!");
return;
}
m_dataGrid.putref_DataSource((LPUNKNOWN)m_ptrRecordset); //Realize the connection of CRecordset with our DATAGRID control
m_dataGrid.Refresh(); // The m_dataGrid variable is the variable associated with the DATAGRID control
this->m_ptrConnection->Execute(_bstr_t(L"drop table tempTable"),COleVariant(l),adCmdText);
//After connecting to DATAGRID, delete the tempTable table
There are problems:
Since the msysobjects object is not open to the outside world by default, that is, it cannot be modified, so we must modify some options before we can achieve its operation, otherwise it will be executed
temp_s.Format(L"SELECT Count(*) AS RTab FROM MSysObjects WHERE (((MSysObjects.Name) Like\"%s\"));",L"tempTable");
This statement gives an error
Solution:
Just add the update operation to msysobjects in the admin's authority.
The default situation is that admin has no authority to msysobjects! Include query
under access 2000
Tools -> Options -> View -> Hidden Objects, System Objects
Check the previous one.
Tools -> Security -> User and group permissions, select "Table" for object type
Select MSysObjects in the object name, and set its permissions in Permissions. |
|