SQL Server CE : How to create a database from C++ with ADO
-
I have a demo application that uses SQL Server via ADO to create and populate a database. I connect to the master database and then execute a "CREATE DATABASE ..." SQL statement. Here is the connect: bool CMsSqlDemoView::Connect(LPCTSTR szDatabase) { Disconnect(); m_DBConnection.CreateInstance(__uuidof(Connection)); if (!m_DBConnection) { m_listErrors.AddString("Failed to create ADO::Connection instance"); return(false); } m_DBConnection->CursorLocation = adUseClient; // Note: The "sqloledb" provider does not fully support XML. // For XML support, the Provider and DataTypeCompatibility values must be set as indicated. CString strConnection; strConnection.Format( "Provider=SQLNCLI10;DataTypeCompatibility=80;Integrated Security=SSPI;Data Source=%s;Database=%s;Trusted_Connection=Yes;", m_strDataSource.GetString(), szDatabase); HRESULT hr = E_FAIL; bool bResult = true; try { hr = m_DBConnection->Open(strConnection.GetString(), "", "", adConnectUnspecified); } catch(_com_error &e) { CString strText; strText.Format("Error code '%d' : '%s'", e.Error(), (LPCTSTR)e.Description()); m_listErrors.AddString(strText); bResult = false; } return(bResult); } So far so good. I have now created a second demo application that uses SQL Server CE 3.5. I set the provider as "Microsoft.SQLSERVER.CE.OLEDB.3.5" but I am unable to create a connection object, and I have no idea how to create a database since I cannot connect to a master database. Anyone know how to do this (C++)? Thanks. BY the way I don't want to use managed C++, or OLEDB.
modified on Tuesday, October 26, 2010 8:52 AM
-
I have a demo application that uses SQL Server via ADO to create and populate a database. I connect to the master database and then execute a "CREATE DATABASE ..." SQL statement. Here is the connect: bool CMsSqlDemoView::Connect(LPCTSTR szDatabase) { Disconnect(); m_DBConnection.CreateInstance(__uuidof(Connection)); if (!m_DBConnection) { m_listErrors.AddString("Failed to create ADO::Connection instance"); return(false); } m_DBConnection->CursorLocation = adUseClient; // Note: The "sqloledb" provider does not fully support XML. // For XML support, the Provider and DataTypeCompatibility values must be set as indicated. CString strConnection; strConnection.Format( "Provider=SQLNCLI10;DataTypeCompatibility=80;Integrated Security=SSPI;Data Source=%s;Database=%s;Trusted_Connection=Yes;", m_strDataSource.GetString(), szDatabase); HRESULT hr = E_FAIL; bool bResult = true; try { hr = m_DBConnection->Open(strConnection.GetString(), "", "", adConnectUnspecified); } catch(_com_error &e) { CString strText; strText.Format("Error code '%d' : '%s'", e.Error(), (LPCTSTR)e.Description()); m_listErrors.AddString(strText); bResult = false; } return(bResult); } So far so good. I have now created a second demo application that uses SQL Server CE 3.5. I set the provider as "Microsoft.SQLSERVER.CE.OLEDB.3.5" but I am unable to create a connection object, and I have no idea how to create a database since I cannot connect to a master database. Anyone know how to do this (C++)? Thanks. BY the way I don't want to use managed C++, or OLEDB.
modified on Tuesday, October 26, 2010 8:52 AM
In case anyone else needs the answer, I had to use the MS_ADOX::_Catalog COM interface which is imported from msadox.dll. For example: MS_ADOX::_CatalogPtr catalogPtr; catalogPtr.CreateInstance(__uuidof(MS_ADOX::Catalog)); catalogPtr->Create("Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=C:\\Temp\\sqlcedemo.sdf;"); The above crap non error checking code creates the C:\\Temp\\sqlcedemo.sdf database, which can then be opened and populated using ADO in the normal fashion.