do we need to call coInitialize() per instance or per database connection while connecting sql server?

parvathy priya 1 Reputation point
2020-11-30T12:33:38.783+00:00

I am testing with MSSQL server 2008. I have configured multiple database in my application. When I try to connect the second database it throws HR error "0x800401f0" which says CO_E_NOTINITIALIZED and it was thrown when trying to create co-create instance for the second database connection. But already we have coInitialized when we connect default/first database. So do we need to Coinitialize per database connection or per instance?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,765 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 48,581 Reputation points
    2020-11-30T15:23:04.91+00:00

    CoInitialize sets up the COM infrastructure on a thread. Therefore you call it per-thread, not per database or instance. Ideally though call CoInitializeEx instead.

    Note that if you call it again it will return a successful failure (S_FALSE).

    2 people found this answer helpful.
    0 comments No comments

  2. Cris Zhan-MSFT 6,606 Reputation points
    2020-12-01T07:59:20.983+00:00

    Hi @parvathy priya ,

    This is more a programming problem than a SQL Server database connection problem, and perhaps more effective help can be obtained in the appropriate forum.
    Check the Remarks of the document: CoInitialize function

    And please check the following posts if help:
    https://stackoverflow.com/questions/49481853/cocreateinstance-in-the-mmdevice-api-returns-error-code-0x800401f0
    https://stackoverflow.com/questions/9286600/when-do-i-need-to-call-coinitialize-in-this-scenario