Tech Off Thread

13 posts

How to execute an SQL Server Stored procedure in modern C++?

Back to Forum: Tech Off
  • User profile image
    eduquint

    I am new to this forum, so, I hope this is the right place to post this issue.

    I am an experienced programmer, but I am starting to develop modern VC++ applications. Nowadays, I am programming mostly in C# 5.0.

    In an effort to learn modern C++, I have converted several small utilities, I normally use, from C# 5.0 to C++, using MFC. The new versions open faster, perform much better, and have a very small footprint. STL is impressive. Unfortunately, the user interface is much poorer when compared to the DevExpress suite I use for C#. I hope there are similar suites for C++ too. Are any of you aware of one I could try?

    Alas, I am unable to find any simple MFC C++ sample showing how to execute an SQL Server 2012 Store Procedure that takes parameters and returns one or more recordsets.

    I have been browsing the Web for days. All the samples I found are very old (VC++ 6.0), most do not even compile, some are ATL related, and other are VC++.Net related.

    I will not be able to do any serious work without easily accessing SQL Servers.

    Would any of you please kindly point me to any resource (sample, book, e-book, documentation, etc.) that could help me perform this very simple task? Microsoft's own documentation does not have many samples, and not one specifically illustrating how to do this simple operation.

    I am under the impression that Microsoft is deprecating MFC. Is this true? Besides the modern interface, which does not apply in my case because my clients mostly run Windows 7, what is the preferred way to develop native C++ Windows line-of-business applications?

    Thanks very much for any suggestion.

    Eduardo Quintana

  • User profile image
    GreyLensman

    Google "t-sql c++" returned many possible solutions.  It looks like you first have to pick how you want to talk to the server.

  • User profile image
    Maurits

    Re: the "how do I write UI in a native C++ app" question:

    I would normally suggest writing a Windows Store app, but there is a requirement that the app run on Windows 7.

    I believe MFC is still supported, but more recent UI frameworks (that still run on Windows 7) would include Windows Presentation Foundation and XAML.

  • User profile image
    Maurits

    Here's an example of how to call a stored procedure using the native C++ OLE DB APIs:

    http://technet.microsoft.com/en-us/library/ms403291.aspx

  • User profile image
    eduquint

    Thanks for your prompt reply, Maurits.

    As for WPF, as far as I know, and according to this Microsoft site Introducing Windows Presentation Foundation, WPF is a .NET technology. I know by fact that Visual Studio is written in WPF and C++, but this king of integration is not easy, so much so, that the next VS version will be entirely developed in .NET (albeit, compiled with a new technology that generates native code from C# and VB.Net).

    XAML seems interesting. I will try to learn it. But it will be something else to master in.

    As for the C++ OLE DB APIs, thanks for the hint. I am already reviewing the code, but It seems too complex for a simple task:

    1. Connect to an SQL Server, 
    2. Execute a stored procedures with two parameters and
    3. Read the resulting recordset.

     I had seen it before, but I deemed it too much code for such a simple task. Is there a simpler way of doing it?

    Again, thanks for your help.

    Eduardo Quintana

  • User profile image
    eduquint

    @GreyLensman:

    Thanks for your suggestion.

    I had tried it already: 80% of the results are for job offers, 19% are for unrelated subjects (.NET C++, etc), and the remaining 1% are for very old useless stuff.

    It is not easy to find a good article or sample on how to do this simple task in native C++:

    1. Connect to an SQL Server,
    2. Execute a stored procedures with parameters and
    3. Read the resulting recordset.

    The only way I found so far is using the  C++ OLEDB API, which is very complex for such a simple task.

    Thanks again for your help.

    I will keep trying.

    Eduardo Quintana

     

  • User profile image
    blowdart

    As for the C++ OLE DB APIs, thanks for the hint. I am already reviewing the code, but It seems too complex for a simple task:

    1. Connect to an SQL Server, 
    2. Execute a stored procedures with two parameters and
    3. Read the resulting recordset.

     I had seen it before, but I deemed it too much code for such a simple task. Is there a simpler way of doing it?

    Well you could simplify it by not taking parameters in your stored procedure and ignoring the method response, but really that is the approach to do it.

    Any examples using ODBC should be ignored, OLE/DB replaced that years ago.

  • User profile image
    eduquint

    @blowdart:

    Thanks,

     

    I will try this one: Execute a Stored Procedure (Using RPC Syntax) and Process Return Codes and Output Parameters (OLE DB) and this one: Fetch Rows from a Result Set (OLE DB).

    I need parameters because all my sp are parameters based. But I suspect I can manage this approach. 

     

    Thanks again for your help.

     

    Eduardo Quintana

  • User profile image
    eduquint

    @blowdart:

    I tried and ran the samples using OLE DB. The problem is that the SQL OLEDB Client is deprecated by Microsoft, so much so, that I had to define the following constant to have the sample program compiled:

    #define _SQLNCLI_OLEDB_IGNORE_DEPRECATION_WARNING_
    Is there another way of accessing an SQL Server that is not depreacated by Microsoft?

    Thanks,

    Eduardo Quintana
  • User profile image
    blowdart

    I didn't realise it had all moved on. Apparently Native Client is now where it's at.

    http://msdn.microsoft.com/en-us/sqlserver/aa937733.aspx

     

     

  • User profile image
    eduquint

    @Maurits:

    As is the case with WPF, XAML is a .Net technology and only works with native C++ in Windows 8/8.1 Store Apps. 

    Unfortunately, neither of them are a solution for Windows 7 or even 8/8.1 in the desktop.

    So, I still would like to find a modern desktop interface technology to use in my future native modern C++ developments.

     

  • User profile image
    eduquint

    @blowdart:

    Apparently, the currently preferred access technology is SQL Native Client + ODBC. OLEDB is being deprecated and will not be available in SQL 2014 Native Client. See SQL Server 2012 via native c++ no atl preferred access method from windows.

    Also, as stated in Data Access Technologies Road Map

    "SQLOLEDB: The Microsoft OLE DB Provider for SQL Server (SQLOLEDB), which supports access to Microsoft SQL Server, has been deprecated. Its connectivity to future versions of SQL Server may not be supported. The ability to connect to versions earlier than SQL Server 7 will be removed from the operating system after Windows 7. New applications should use the SQL Server Native Client OLEDB provider, which supports new SQL Server features."

    And :

    "After SQL Server 2012, the SQL Server Native Client OLE DB provider will no longer be included in SQL Server Native Client."

    The SQL Server Native Client ODBC provider is a C programming-language interface, not a C++, much less a modern C++ one, but it is manageable. In my case, as soon as I grab its workings, I will create a class to deal with all my store procedures and isolate myself from C style programming complexity.

    I found some samples in the Microsoft documentation. I am trying to make them compile and run in my development environment. Alas, I am having a linking errors:

    Error 1 error LNK2019: unresolved external symbol _GetUserNameExW@12 referenced in function _main c:\Users\Eduardo\documents\visual studio 2013\Projects\SqlNativeClient\SqlNativeClient\SqlNativeClient.obj SqlNativeClient.

    I will somehow figure how to solve this problem and finish my testing.

    Eduardo Quintana

  • User profile image
    eduquint

    @blowdart:

    I just had to add ;Secur32.lib to the end of the project's properties->Debug->Linker->Input->Additional Dependencies line.

    It compiled and worked perfectly. Now I will try other samples.

    Eduardo Quintana

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.