CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    SQL Server ado connect

    I want to use an sql database in my application. How do I connect to an SQL server database? Are there any examples? Could someone provide an example?

    I was able to connect to an mdb (Access) file, but I do not believe Access will be able to fulfill my applications requirements for a database. I need to use SQL Server. I created a database and some tables in the database but I am not sure how to connect to it. It seems to hide the database in a server. Any ideas or examples?
    Mike B


  2. #2
    Join Date
    Apr 2001
    Location
    Turkmenistan
    Posts
    674

    Re: SQL Server ado connect


    #define CON_STR "provider=sqloledb; server=yourServer; uid=yourUID; pwd=yourPWD; database=yourDatabase;";
    HRESULT hr = m_Connection->Open(_bstr_t(CON_STR), _bstr_t(""),
    _bstr_t(""), adAsyncConnect);


    Bayram.


  3. #3
    Join Date
    Feb 2001
    Posts
    2,455

    Re: SQL Server ado connect

    I cannot seem to get this to work. Here is the code I have tried:

    BOOL CSchedulerDoc::OnNewDocument()
    {
    if (!CDocument::OnNewDocument())
    return FALSE;

    HRESULT hr;
    CString strConn = "Provider=sqloledb;server=comp_acer9;database=Project_Data.mdf";
    //make connection
    try{
    hr = m_pConnection.CreateInstance(__uuidof(Connection));
    if(SUCCEEDED(hr))
    {
    hr = m_pConnection->Open(_bstr_t(strConn), _bstr_t(L""), _bstr_t(L""), adAsyncConnect);
    if(SUCCEEDED(hr))
    {
    }
    }
    }
    catch(_com_error &e)
    {
    _bstr_t bstrError = e.Error();
    _bstr_t bstrDesc = e.Description();
    TRACE(bstrError);
    TRACE(bstrDesc);
    }
    catch(...)
    {
    }
    return TRUE;
    }



    In the line

    CString strConn = "Provider=sqloledb;server=comp_acer9;database=Project_Data.mdf";



    I have also tried

    CString strConn = "Provider=sqloledb;server=comp_acer9;uid="";pwd=""database=Project_Data.mdf";



    but that did not work.
    I am not sure if my spq database is set up properly and although the try block is not working it does not seem to be throwing an exception. I am not sure what to do from here.
    If I open SQL server 7.0 Enterprise manager the list shows something similar to the following:

    Console Root
    Microsoft SQL Servers
    SQL Server Group
    Comp_Acer9 (Windows NT)
    Databases
    .........
    .........
    Project
    Data Transformation Services
    SQL 6.5



    Anyway, this is the directory and if you see something wrong with what I am doing, please, I could use some help!
    Mike B



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured