Connect to the SQL Server database using VC

Source: Internet
Author: User
Tags odbc connection
> 1. What settings do VC need to make when connecting to the SQL Server database?
-----------------------------------------------------
(1) initialize the com library and introduce the ADO library definition file.
(2) connect to the database using the connection object
(3) Use the established connection to execute SQL commands through the connection and command objects, or use the recordset object to obtain the result record set for query and processing.
(4) Close the connection and release the object after use.

> 2.ado, oledb, ODBC, Dao, and other database-related words respectively? What is the relationship?
-----------------------------------------------------
There are several different database connection methods.
Oledb is a database connection method based on component objects.
ADO is a simple and fast oledb connection method based on oledb.
ODBC connection methods developed by database vendors based on the public interfaces of databases developed by Microsoft.
Dao is a simpler data connection method developed by Microsoft. It was used more early.


> 3. Brief steps for connecting to the database with VC.
-----------------------------------------------------
There are some on hand. By the way, I checked it online. The following should be correct.
Http://www.yesky.com/413/1839913.shtml

The following code provides the connection mode of the VC + ADO database:

# Import "C: \ Program Files \ common files \ System \ ADO \ msadox. dll" rename_namespace ("ADOX ")
# Import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll "\
Rename_namespace ("Ado") Rename ("EOF", "endoffile ")
# Pragma warning (Disable: 4146)
# Include "icrsint. H"
# Include "shlwapi. H"
# Pragma comment (Lib, "shlwapi. lib ")

Using namespace ADO;

Coinitialize (null );

Using namespace ADOX;
_ Catalogptr m_pcatalog = NULL;
Cstring strconnection, strtable;
Tchar chfilepath [max_path];

Getmodulefilename (null, chfilepath, max_path );
(_ Tcsrchr (chfilepath, '\') [1] = 0;
Lstrcat (chfilepath, _ T ("test. mdb "));

Strtable = chfilepath;

Strconnection = _ T ("provider = Microsoft. Jet. oledb.4.0; Data Source =") + strtable + _ T ("; Jet oledb: Engine type = 5 ;");

If (! Pathfileexists (chfilepath ))
{
Try
{
M_pcatalog.createinstance (_ uuidof (Catalog ));
M_pcatalog-> Create (_ bstr_t (strconnection ));
}
Catch (...)
{
M_pcatalog-> release ();
M_pcatalog = NULL;
Couninitialize ();
}
M_pcatalog-> release ();

// M_pcatalog-> release ();
// M_pcatalog = NULL;
}
Couninitialize ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.