QuakeGod
2022-01-16 326d3e312c74726814c39c9d112faab03c4a167c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "stdafx.h"
 
/*
* Access Êý¾Ý¿â½Ó¿Ú³ÌÐò
*
*
*
*
*/
 
int CreateMdb(CString MdbPathName)
{
    //µ¼Èë¶ÔÓ¦Dll
 
 
    //::CoInitialize(NULL);
    ADOX::_CatalogPtr   pCatalog(__uuidof(ADOX::Catalog));
    CString s1,s2;
//    s1=_T("Provider=Microsoft.Jet.OLEDB.4.0;data source=")+MdbPathName;
    s1=_T("Provider=Microsoft.ACE.OLEDB.12.0;data source=")+MdbPathName;
//    s1=_T("File Name=.\\A1.UDL");
    _bstr_t   str=s1;  
 
    try   
    {   s1.Format(_T("=> Try To Create Mdb %s \r\n"),MdbPathName);
        OutputDebugString(s1);        
        pCatalog->Create(str);   //´´½¨Êý¾Ý¿â   
        return 1;
    }   
    catch(_com_error   &e)   
    {   
        //´íÎó´¦Àí   
        // ::MessageBox(NULL,_T("Êý¾Ý¿â´´½¨Ê§°Ü¡£"), _T("Ìáʾ"),MB_OK | MB_ICONINFORMATION);
        _bstr_t bt=e.Description();
        s2=(LPTSTR)bt;
        s1.Format(_T("=>LOG   Catch e Êý¾Ý¿â %s ´´½¨Ê§°Ü  %s  \r\n %s \r\n"),MdbPathName,e.ErrorMessage(),s2);
//        SysLog(s1);
        //s1.AppendFormat(_T("%ws"),e.Description());
        // ::MessageBox(NULL,s2+e.Description(),e.ErrorMessage(),MB_OK|MB_ICONINFORMATION);
 
        //TRACE(s1);
        OutputDebugString(s1);
 
        return -1;
    }
    //CoUninitialize();
    return 0;
}
int OpenMdb(CString MdbPathFileName,_ConnectionPtr & pConnectionPtr)
{
    CString strConnection;
    CString strMsg;
    CString strTemp1, strTemp2,strAddField1£¬strAddField2;
    CString s1;
//    _ConnectionPtr pConnection;
 
 
    // strTemp1 = _T("ÐÂ×Ö¶Î1");
    // strTemp2 = _T("ÐÂ×Ö¶Î2");
 
    strConnection.Format(_T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s;Persist Security Info=False;"),MdbPathFileName);
    HRESULT hr = pConnectionPtr.CreateInstance( __uuidof(Connection) );
    if ( FAILED(hr) )
    {
        ::MessageBox(NULL,_T("ÄúϵͳÖеÄÊý¾Ý¿âÇý¶¯ÓÐÎÊÌâ¡£"), _T("Ìáʾ"),MB_OK | MB_ICONINFORMATION);
        //                SysLog(_T("ÄúϵͳÖеÄÊý¾Ý¿âÇý¶¯ÓÐÎÊÌâ¡£\r\n"));
        return 0;
    }
    try
    {
        pConnectionPtr->ConnectionTimeout = 18;
        pConnectionPtr->ConnectionString =_bstr_t(strConnection);
        pConnectionPtr->Open( _bstr_t(L""), _bstr_t(L""), _bstr_t(L""), -1 );
        //½¨Á¢±í
    }
    catch( _com_error& e)
    {
        _bstr_t bstrError = e.Description();
        strMsg=(LPCTSTR)bstrError;
        // ::MessageBox(NULL,strMsg, e.ErrorMessage(),MB_OK | MB_ICONINFORMATION);
        //    _bstr_t bt=e.Description();
        //    s2=(LPTSTR)bt;
        s1.Format(_T("=>LOG  Êý¾Ý¿â %s Á¬½Óʧ°Ü  %s  \r\n %s \r\n"),MdbPathFileName,e.ErrorMessage(),strMsg);
        //SysLog(s1);
        OutputDebugString(s1);
    //    pConnectionPtr->Close();
        pConnectionPtr.Release();
        return 0;
    }
//    (* pConnectionPtr)=pConnection;
//    pConnection->Close();
//    pConnection.Release();
    return 1;
    //CoUninitialize();
}
 
int CloseMdb(_ConnectionPtr & pConnection)
{
    pConnection->Close();
    pConnection.Release();
    return 0;
}
 
int RunSql1( _ConnectionPtr pConnection,CString SqlStr1)//ÔËÐÐûÓзµ»ØÖµµÄSQLÓï¾ä
{
    _variant_t RecordsAffected;
    CString s1,s2;
    CString strMsg;
    try
    {
        pConnection->Execute(_bstr_t(SqlStr1),&RecordsAffected,adCmdText);
 
    }
    catch(_com_error& e)
    {
        _bstr_t bstrError = e.Description();
        strMsg=(LPCTSTR)bstrError;
        s1.Format(_T("=>LOG  RunSQL1 %s \r\n %s \r\n"),e.ErrorMessage(),strMsg);
        OutputDebugString(s1);
        //  SysLog(s1);
    }
    return 0;
}
 
int RunSql2( _ConnectionPtr pConnection,CString sqlquery1,_RecordsetPtr & pRecordsetPtr)//ÔËÐÐÓзµ»ØÖµµÄSQLÓï¾ä
{
    _variant_t RecordsAffected;
    CString s1,s2;
    CString strMsg;
    int j;
    int RowCount,FiledCount;
    try
    {
//        pConnection->Execute(_bstr_t(SqlStr1),&RecordsAffected,adCmdText);
        pRecordsetPtr.CreateInstance(__uuidof(Recordset));
        j=pRecordsetPtr->Open(_variant_t(sqlquery1),pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
        s1.Format(_T("-mdbÊý¾Ý¿â¶ÁÈ¡³É¹¦ readok ×´Ì¬Âë %x\r\n"),j);
        //            ::SysLog(s1);
        RowCount=pRecordsetPtr->GetRecordCount();
 
        FieldsPtr fs2;
        CString name2,value2;
//        fs2.CreateInstance(__uuidof(Fields));
        fs2=pRecordsetPtr->GetFields();
        FiledCount=fs2->Count;
        s1.Format(_T("result %x  %d\r\n"),RowCount,FiledCount);
        //                SysLog(s1);
    }
    catch(_com_error& e)
    {
        _bstr_t bstrError = e.Description();
        strMsg=(LPCTSTR)bstrError;
        s1.Format(_T("=>LOG  RunSQL2 %s \r\n %s \r\n"),e.ErrorMessage(),strMsg);
        //  SysLog(s1);
        return -1;
    }
    return RowCount;
}
 
MyDataBase::MyDataBase()
{
    //µ¼Èë¶ÔÓ¦Dll
    //    ::CoInitialize(NULL);    
    IsOpened=0;
}
 
MyDataBase::~MyDataBase()
{
    if (IsOpened)
    {
        CloseMdb(m_ConnectionPtr);
        //m_ConnectionPtr;
    }
    //CoUninitialize();
}
 
int MyDataBase::CreateMdb()
{
    return ::CreateMdb(MdbPathName);
    return 1;
}
 
int MyDataBase::CreateMdbStruct()
{
    if (!IsOpened)
    {
        IsOpened=OpenMdb(MdbPathName,m_ConnectionPtr);
    }
    //½¨Á¢±í
    CString strAddField1= _T("Create Table [Products](SN COUNTER,  ProductCode Text(32), ProductType Text(32),ProductDesc Text(120), ColorNo Integer, Quality Integer, PowerNo Integer,ProductLineNo Integer,Time1 DATETIME)");
    //pConnection->Execute(_bstr_t(strAddField2),&RecordsAffected,adCmdText);
    int j=RunSql1(m_ConnectionPtr,strAddField1);
 
    CString strAddField2= _T("Create Table [Results](ID Integer, Names1 Text(50),Value1 Text(120))");
    //pConnection->Execute(_bstr_t(strAddField2),&RecordsAffected,adCmdText);
//    RunSql1(m_ConnectionPtr,strAddField2);
 
    CString strAddField3= _T("Create Table [Counter](ID1 Integer, Names1 TEXT(50),vvv VarChar(200))");
    //pConnection->Execute(_bstr_t(strAddField2),&RecordsAffected,adCmdText);
//    RunSql1(m_ConnectionPtr,strAddField3);
 
    /*
        //ÐèÒªÔö¼ÓµÄ×ֶμ°Êý¾ÝÀàÐÍ
    strAddField1 = TEXT("ALTER TABLE Covers ADD COLUMN ") + strTemp1 + TEXT(" int");
        pConnection->Execute(_bstr_t(strAddField1),&RecordsAffected,adCmdText);
 
        //ÐèÒªÔö¼ÓµÄ×ֶμ°Êý¾ÝÀàÐÍ
    CString  strAddField2 = TEXT("ALTER TABLE Covers ADD COLUMN ") + strTemp2 + TEXT(" datetime");
        pConnection->Execute(_bstr_t(strAddField2),&RecordsAffected,adCmdText);
    */
        //::MessageBox(NULL,_T("Êý¾Ý¿â¹¹Ôì¸üгɹ¦¡£"), _T("Ìáʾ"),MB_OK | MB_ICONINFORMATION);
//          SysLog(_T("Êý¾Ý¿â¹¹Ôì¸üгɹ¦¡£\r\n"));
    return j;
    //CoUninitialize();
}
 
int MyDataBase::AddToOutPutDataBase(int Index, CString ProductCode, CString ProductType, CString ProductDesc, int ColorNo,int Quality,int PowerNo,int ProductLineNo)
{
    CString sqlstr1;
    if (Index==0)
    {
        sqlstr1.Format(_T("Insert Into Products(ProductCode,ProductType,ProductDesc,ColorNo,Quality,PowerNo,ProductLineNo,time1) \
                          Values('%s','%s','%s','%d','%d','%d','%d',Now())"),
                          ProductCode,ProductType,ProductDesc,ColorNo,Quality,PowerNo,ProductLineNo);
 
    }
    else
    {
        sqlstr1.Format(_T("Insert Into Products(SN,ProductCode,ProductType,ProductDesc,ColorNo,Quality,PowerNo,ProductLineNo,time1) \
                          Values('%d','%s','%s','%s','%d','%d','%d','%d',Now())"),
                          Index,ProductCode,ProductType,ProductDesc,ColorNo,Quality,PowerNo,ProductLineNo);
    }
    int j;
    j=RunSql1(this->m_ConnectionPtr,sqlstr1);
 
    //ɾ³ý±íÖмǼ£¬Ö»±£Áô×îеÄ200Ìõ
    sqlstr1.Format(_T("DELETE FROM Products WHERE SN NOT IN (SELECT TOP 200 SN FROM Products ORDER BY SN DESC)"));
    j=RunSql1(this->m_ConnectionPtr,sqlstr1);
    return j;
}
 
int MyDataBase::AddToMdb(int channel,CString SN)
{
    //    ::CoInitialize(NULL);
 
    CString Sql1;
    Sql1.Format(_T("INSERT INTO COUNTERS(CHANNEL,SN,TIME1) values('%d','%s',TIME())"),channel,SN);
    //  SysLog(Sql1+_T("\r\n"));
    RunSql1(m_ConnectionPtr,Sql1);
    // pConnection->Execute(_bstr_t(Sql1),&RecordsAffected,adCmdText);
 
    return 1;
    //CoUninitialize();
 
}
 
int MyDataBase::CheckMdb1(CString SerialNo)
{
    int j,k;
    CString s1;
    int fieldcount;
    int readok=0;
    _variant_t v2;
    CString namestr,valuestr;
    _ConnectionPtr m_connection2;
    _RecordsetPtr m_recordset2;
    k=m_connection2.CreateInstance(__uuidof(Connection));
//    m_DBConnection.CreateInstance("ADODB.Connection");
    CString db2constr;
    db2constr.Format(_T("Driver={Microsoft Access Driver (*.mdb)};Dbq=%s;CharSet=gbk"),MdbPathName); //Uid=admin;Pwd=123;
        
//    m_connection2->ConnectionString="Provider=MSDAORA.1;password=stream;user id=stream;Data Source=stream.bly.com;Persist Security Info=True";
    m_connection2->ConnectionString=_bstr_t(db2constr);
//    Logtxt(db2constr+"\r\n");
    try 
    {
        j=m_connection2->Open(_T(""),_T(""),_T(""),-1);
//        DB2opened=1;
        s1.Format(_T("-mdbÊý¾Ý¿âÁ¬½Ó³É¹¦ DBOpened ×´Ì¬Âë %x %x\r\n"),j,k);
        //::SysLog(s1);
    //    m_DBConnection->Execute("Insert into workers(name) values('zhang')",&v1,0);
        m_recordset2.CreateInstance(__uuidof(Recordset));
        CString sqlquery;
        sqlquery.Format(_T("Select * from COUNTERS Where SN='%s'"),SerialNo);
        j=m_recordset2->Open(_variant_t(sqlquery),m_connection2.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
        readok=1;
        s1.Format(_T("-mdbÊý¾Ý¿â¶ÁÈ¡³É¹¦ readok ×´Ì¬Âë %x\r\n"),j);
//            ::SysLog(s1);
        namestr.Format(_T("serial"));
        valuestr.Format(_T("'%s'"),SerialNo);
        int rowcount;
        FieldsPtr fs2;
        CString name2,value2;
        fs2.CreateInstance(__uuidof(Fields));
        fs2=m_recordset2->GetFields();
        fieldcount=fs2->Count;
        j=fieldcount;
        k=m_recordset2->GetRecordCount();
 
 
        s1.Format(_T("result %x  %d\r\n"),j,k);
//                SysLog(s1);
 
        FieldPtr field2;
        field2.CreateInstance(__uuidof(Field));
        rowcount=0;
/*
        for (i=0;i<fieldcount;i++)
        {
            field2=fs2->GetItem(_variant_t(long(i)));
    //        v1=field->GetValue();
            name2=(LPCTSTR)field2->GetName();
//                m_list1.InsertColumn(i,name2,LVCFMT_LEFT,80);
        }
*/            
/*
        while (!m_recordset2->adoEOF)
        {
//                m_list1.InsertItem(rowcount,value2,0);
            (*resultstr).Empty();
            for (i=0;i<fieldcount;i++)
            {
                field2=fs2->GetItem(_variant_t(long(i)));
        //        v1=field->GetValue();
                name2=(LPCTSTR)field2->GetName();
                v2=    m_recordset2->GetCollect(_variant_t(name2));
 
                if (v2.vt==VT_NULL)
                {
                    value2.Empty();
                    l=0;
                }
*/                    /*
                else if (v2.vt==VT_R8||v2.vt==VT_R4)
                {
                    double dval1;
                    v2.ChangeType(VT_R8);
                    dval1=v2.dblVal;
                    value2.Format(_T("%f"),dval1);
                    l=value2.GetLength();
                }
                */
/*                    else
                {
                    v2.ChangeType(VT_BSTR);
                    value2=(char *)_bstr_t(v2.bstrVal);
                    l=_bstr_t(v2.bstrVal).length();
                }
//                        m_list1.SetItemText(rowcount,i,value2);
                    if (name2==_T("Serial")) {continue;}
                    if (name2==_T("DateTime")) 
                {
                    namestr+=_T(",")+name2;
                    valuestr+=_T(",to_date('")+value2+_T("','YYYY-mm-dd hh24:MI:ss')");
                    continue;
                }
                    namestr+=_T(",")+name2;
                    valuestr+=_T(",'")+value2+_T("'");
                    s1.Format(_T("%s=%s;"),name2,value2);
                    (*resultstr)+=s1;
            }
//                if (m_pRecordset->GetadoEof
            s1.Format(_T("%s %d \r\n"),(*resultstr),(*resultstr).GetLength());
//                Logtxt(s1);
            m_recordset2->MoveNext();
            rowcount++;
        }
*/
        m_recordset2->Close();
        m_connection2->Close();    
        return k;
    }
    catch (_com_error e1)
    {    
//        DB2opened=0; 
        s1.Format(_T("-mdbÊý¾Ý¿âÁ¬½Óʧ°Ü ¾ßÌåÐÅÏ¢ÈçÏÂ\r\n  %s\r\n"),(char *)(e1.Description()));
//            ::SysLog(s1);
        return -1;
    }
    return 0;
 
}