Click here to Skip to main content
15,921,276 members
Home / Discussions / Database
   

Database

 
GeneralSQL code and datagrid Pin
ASGill10-Mar-04 13:24
ASGill10-Mar-04 13:24 
GeneralRe: SQL code and datagrid Pin
MasudM10-Mar-04 22:23
MasudM10-Mar-04 22:23 
GeneralRe: SQL code and datagrid Pin
ASGill11-Mar-04 2:04
ASGill11-Mar-04 2:04 
GeneralRe: SQL code and datagrid Pin
MasudM11-Mar-04 5:14
MasudM11-Mar-04 5:14 
GeneralRe: SQL code and datagrid Pin
Mike Dimmick11-Mar-04 2:56
Mike Dimmick11-Mar-04 2:56 
GeneralRe: SQL code and datagrid Pin
ASGill11-Mar-04 15:39
ASGill11-Mar-04 15:39 
GeneralDataView from DataTable Pin
Le centriste10-Mar-04 10:33
Le centriste10-Mar-04 10:33 
GeneralADO Disconnected Recordset Pin
cmacgowan10-Mar-04 6:41
cmacgowan10-Mar-04 6:41 
Hi ...

I am trying to use ADO disconnected recordset to insert data into a sql table. I am using AddNew(vField, vValue) with UpdateBatch(). The code below does not throw any exceptions ... but does not add data to the table.

Any comments are appreciated,
Thanks,
Chris

<br />
<br />
void CTestApp::TestDatabaseUpdateBatch1a(void) <br />
{<br />
    int nDataCount = 0; <br />
    long nIndex = 0; <br />
    long nIndex2 = 0; <br />
<br />
    CString csMessage; <br />
    CString csErrorMessage;<br />
    CString csTemp; <br />
    CString csSQL; <br />
<br />
    BOOL    bIsOpen; <br />
    BOOL    bIsEmpty; <br />
<br />
    long    nCount = 0; <br />
    int     nTemp = 0; <br />
    int     nLimit = 0; <br />
<br />
    int     nTempInt = 0; <br />
    long    nTempLong = 0; <br />
    double  nTempDouble = 0; <br />
<br />
    HRESULT hResult; <br />
<br />
    SYSTEMTIME  st;<br />
<br />
<br />
    int     i = 0; <br />
<br />
    string  strTemp; <br />
<br />
    _variant_t sval;<br />
<br />
    _variant_t  vNull;<br />
    vNull.vt    = VT_ERROR;<br />
    vNull.scode = DISP_E_PARAMNOTFOUND;<br />
<br />
    COleSafeArray colesaFieldList;<br />
    COleSafeArray colesaDataList;<br />
<br />
    vector<COleSafeArray>  *pvecDataList;<br />
    <br />
    pvecDataList = new vector<COleSafeArray>; <br />
<br />
<br />
    COleDateTime oledtCurrentDate = COleDateTime::GetCurrentTime();<br />
<br />
    // Convert the OleDateTime to the varient<br />
    // COleVariant vCurrentDateTime(oledtCurrentDate);<br />
    COleVariant vCurrentDateTime; <br />
<br />
    CMxTextParse *pMxTextParse = NULL; <br />
<br />
    CMainFrame *pMainFrame = (CMainFrame *)AfxGetMainWnd(); <br />
	CFrameWnd* pChild = pMainFrame->GetActiveFrame();<br />
    CTestMeteorlogixView* pView  = (CTestMeteorlogixView*)pChild->GetActiveView();  <br />
<br />
    pView->WriteLog("Start TestDatabaseUpdateBatch1a."); <br />
    pView->WriteLog("Load table using AddNew() and UpdateBatch()."); <br />
<br />
<br />
    // Define ADO connection pointers<br />
    _ConnectionPtr pConnection = NULL;<br />
    _RecordsetPtr  pRecordset = NULL;<br />
<br />
    try<br />
    {<br />
        // When we open the application we will open the ADO connection<br />
        pConnection.CreateInstance(__uuidof(Connection));<br />
<br />
        // Replace Data Source value with your server name.<br />
        bstr_t bstrConnect("Provider='sqloledb';Data Source='SQLDEV';"<br />
                           "Initial Catalog='AlphaNumericData';"<br />
                           "User Id=cmacgowan;Password=cmacgowan");<br />
<br />
        // Open the ado connection<br />
        pConnection->Open(bstrConnect,"","",adConnectUnspecified);<br />
<br />
        // Create an instance of the database<br />
        pRecordset.CreateInstance(__uuidof(Recordset));<br />
        <br />
        // Select the correct sql string.  Note that we are creating an<br />
        // empty string by doing a select on the primary key.  We are only<br />
        // doing inserts and we do not want to bring data back from the<br />
        // server<br />
<br />
        csSQL = "SELECT * FROM dbo.AAMacgowanTest WHERE RecordId IS NULL";<br />
        // csSQL = "SELECT * FROM dbo.DICastRaw1Hr";<br />
<br />
<br />
        pRecordset->PutRefActiveConnection(pConnection);<br />
        pRecordset->CursorLocation = adUseClient;<br />
        <br />
<br />
        pRecordset->Open(csSQL.AllocSysString(), vNull, adOpenStatic, adLockOptimistic, -1);<br />
        <br />
        // Test to see if the recordset is connected<br />
        if(pRecordset->GetState() != adStateClosed)<br />
        {<br />
            // The recordset is connected, we will see if we are <br />
            // at the end<br />
<br />
            if((pRecordset->BOF) && (pRecordset->GetadoEOF()))<br />
            {<br />
                // The recordset is empty<br />
                bIsEmpty = false;<br />
            }<br />
<br />
<br />
            if(pRecordset->GetadoEOF())<br />
            {<br />
                bIsOpen = false;<br />
            }<br />
            else<br />
            {                <br />
                // disconnect the database                    <br />
                pRecordset->PutRefActiveConnection(NULL);<br />
            }<br />
        }<br />
<br />
<br />
        // disconnect the database                    <br />
        // pRecordset->PutRefActiveConnection(NULL);<br />
<br />
        // Disassociate the connection from the recordset.<br />
        pRecordset->PutRefActiveConnection(NULL);<br />
<br />
        // Set the count<br />
        nCount = 1; <br />
    <br />
        // now we will scroll through the file<br />
        while(nCount > 0) <br />
        { <br />
            nCount--; <br />
<br />
            nDataCount = 10; <br />
        <br />
            // test that we got some data<br />
            if (nDataCount >= 0) <br />
            { <br />
                // Start the insert process<br />
                // m_pRecordset->AddNew();<br />
<br />
                COleSafeArray warningList;<br />
                //int index, listIndex = -1, bitIndex; // indexing variables<br />
                // long lowIndex, highIndex, arrayIndex[2];<br />
                <br />
                VARIANT vFieldList[25]; <br />
                VARIANT vValueList[25]; <br />
<br />
                int nFieldIndex = 0; <br />
                int nValueIndex = 0; <br />
<br />
<br />
                // Setup the fields<br />
                vFieldList[nFieldIndex].vt = VT_BSTR;<br />
                vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"Name");<br />
                nFieldIndex++;<br />
<br />
                vFieldList[nFieldIndex].vt = VT_BSTR;<br />
                vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"Section");<br />
                nFieldIndex++;<br />
<br />
                vFieldList[nFieldIndex].vt = VT_BSTR;<br />
                vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"Code");<br />
                nFieldIndex++;<br />
<br />
                vFieldList[nFieldIndex].vt = VT_BSTR;<br />
                vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"Latitude");<br />
                nFieldIndex++;<br />
<br />
                vFieldList[nFieldIndex].vt = VT_BSTR;<br />
                vFieldList[nFieldIndex].bstrVal = ::SysAllocString(L"Longitude");<br />
                nFieldIndex++;<br />
<br />
<br />
                pView->WriteLog("Set data using AddNew() ..."); <br />
<br />
                // COleDateTime is a wrapper for VARIANT's DATE type. COleVariant is <br />
                // a wrapper for VARIANTs themselves. If you need to create a <br />
                // variant, you can say:<br />
                COleDateTime oledtCurrentDate2 = COleDateTime::GetCurrentTime();<br />
<br />
                // Convert the OleDateTime to the varient<br />
                COleVariant vCurrentDateTime2(oledtCurrentDate2);<br />
<br />
                //Set the DATE variant data type.<br />
                memset(&st, 0, sizeof(SYSTEMTIME));<br />
                st.wYear = 2000;<br />
                st.wMonth = 1;<br />
                st.wDay = 1;<br />
                st.wHour = 12;<br />
<br />
                // vect is a vector of COleSafeArrays containing the records <br />
                for(i = 0; i < 10; i++)<br />
                {<br />
<br />
                    // Setup the data<br />
                    nValueIndex = 0; <br />
                    vValueList[nValueIndex].vt = VT_BSTR;<br />
                    vValueList[nValueIndex].bstrVal = ::SysAllocString(L"BLUE");<br />
                    nValueIndex++;<br />
                    <br />
                    vValueList[nValueIndex].vt = VT_BSTR;<br />
                    vValueList[nValueIndex].bstrVal = ::SysAllocString(L"KSTP");<br />
                    nValueIndex++;<br />
<br />
                    vValueList[nValueIndex].vt = VT_I4;<br />
                    vValueList[nValueIndex].dblVal = 100 + nFieldIndex; <br />
                    nValueIndex++;<br />
                    <br />
                    vValueList[nValueIndex].vt = VT_R8;<br />
                    vValueList[nValueIndex].dblVal = 11.11 + nFieldIndex; <br />
                    nValueIndex++;<br />
<br />
                    vValueList[nValueIndex].vt = VT_R8;<br />
                    vValueList[nValueIndex].dblVal = 22.22 + nFieldIndex; <br />
                    nValueIndex++;<br />
<br />
                    // Add the record to the recordset<br />
                    pRecordset->AddNew(vFieldList, vValueList);<br />
                }<br />
<br />
<br />
<br />
                pView->WriteLog("Call UpdateBatch()."); <br />
<br />
                // Re-connect.<br />
                pRecordset->PutRefActiveConnection(pConnection);<br />
                <br />
                // Send updates.<br />
                pRecordset->UpdateBatch(adAffectAll);<br />
                <br />
                // Close the recordset and the connection<br />
                pRecordset->Close();<br />
                pConnection->Close();<br />
<br />
            }                                        <br />
        }<br />
	}<br />
	catch(_com_error *e)<br />
	{<br />
		CString Error = e->ErrorMessage();<br />
		AfxMessageBox(e->ErrorMessage());<br />
        pView->WriteLog("Error processing TestDatabase()."); <br />
	}<br />
	catch(...)<br />
	{<br />
        csMessage = "Undefined exception handled.  Error message details \n\n"; <br />
<br />
        hResult = GetAdoErrorMessage(m_pConnection, <br />
                                     &csErrorMessage);<br />
<br />
        csMessage += csErrorMessage; <br />
        csMessage += "\nmethod: CTestMeteorlogixApp::OnTestDatabaseAdoBulkload()"; <br />
<br />
		AfxMessageBox(csMessage);<br />
<br />
	}<br />
<br />
    csTemp.Format("Last Row %03d  DIcastId = %s ", nIndex, strTemp.c_str()); <br />
    pView->WriteLog(csTemp); <br />
<br />
    pView->WriteLog("End TestDatabaseUpdateBatch1."); <br />
<br />
}<br />
<br />
<br />
<br />
<br />
<br />



Thanks,
Chris
GeneralAbout CrystalReports ... Pin
DustInTheWind9-Mar-04 10:18
DustInTheWind9-Mar-04 10:18 
GeneralRe: About CrystalReports ... Pin
jscorales11-Mar-04 22:52
jscorales11-Mar-04 22:52 
GeneralI need suggestions - datatabase structure Pin
zzaa9-Mar-04 4:26
zzaa9-Mar-04 4:26 
GeneralRe: I need suggestions - datatabase structure Pin
Andy Harman9-Mar-04 10:17
Andy Harman9-Mar-04 10:17 
QuestionHow to create an NTEXT from string data? Pin
kumaichi9-Mar-04 3:22
kumaichi9-Mar-04 3:22 
GeneralRunning SQL script in .NET GUI Pin
xsigroup9-Mar-04 3:21
xsigroup9-Mar-04 3:21 
GeneralProblems with CREATE DATABASE Pin
Frederick S Jones9-Mar-04 3:12
Frederick S Jones9-Mar-04 3:12 
GeneralRe: Problems with CREATE DATABASE Pin
Colin Angus Mackay9-Mar-04 5:39
Colin Angus Mackay9-Mar-04 5:39 
GeneralRe: Problems with CREATE DATABASE Pin
Frederick S Jones11-Mar-04 2:08
Frederick S Jones11-Mar-04 2:08 
GeneralGenerating SQL script Pin
Asad Hussain9-Mar-04 2:51
Asad Hussain9-Mar-04 2:51 
GeneralRe: Generating SQL script Pin
Colin Angus Mackay9-Mar-04 5:34
Colin Angus Mackay9-Mar-04 5:34 
QuestionOracle Notification Services?? Pin
Chen Venkataraman8-Mar-04 9:43
Chen Venkataraman8-Mar-04 9:43 
GeneralFind right Sql Collation Problem !! Pin
MasudM8-Mar-04 5:49
MasudM8-Mar-04 5:49 
GeneralHi, everyone. I need help about SQL 2000 server. Pin
jlizardo8-Mar-04 4:01
jlizardo8-Mar-04 4:01 
GeneralRe: Hi, everyone. I need help about SQL 2000 server. Pin
Rob Graham8-Mar-04 8:56
Rob Graham8-Mar-04 8:56 
GeneralConnection Pooling Pin
Mazdak7-Mar-04 21:03
Mazdak7-Mar-04 21:03 
GeneralRe: Connection Pooling Pin
Rob Graham8-Mar-04 3:00
Rob Graham8-Mar-04 3:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.