Key to accessing the database using the ODBC API under Windows

Source: Internet
Author: User

This article uses the SQL2005 + VS2008 environment to successfully connect to the database in ODBC API mode.

1.SQL Installation and Environment configuration

This part of the Web-based random search has a lot of information, and it is easy to configure the success.

Note the key points: sa account configuration, remote connection configuration.

After the configuration succeeds, you can successfully connect to the remote database as shown in:

  

2. Build the console program, using the ODBC API, the language C + + is available.

On the ODBC test code, do not do too much in this show, all because the online random search has a lot.

The main explanation here is the SQLDriverConnect () execution failure return-1 problem.

The problem is that many people who have just come into contact with ODBC are prone to it, and there is often no useful solution on the web.

Before this, I would like to introduce a function processlogmessages ().

  This function is from the MS document and is used to display an error detail that you can use directly in your program.

The specific code for this function can be found in the ODBC API development tutorial.

  Examples of Use:

1 Char* szconnstrin="Driver=sql Server; server=192.168.1.128,1433; Uid=sa; Pwd=sa;";2R = SQLDriverConnect (Phdbc,null, (sqlchar*) Szconnstrin,sql_nts,sqlconout,sizeof(sqlconout),&conoutlen,sql_driver_complete);3 if(r! = sql_success && r! =sql_success_with_info) {4     //Connection Failed! 5Processlogmessages (SQL_HANDLE_DBC,PHDBC,"yyyyyy",1);6     return-1;7}

3. Connecting database key issues and solutions <->driver problem:

  

We can check if the PC has such a database driver, the location is:

      Control Panel \ All Control Panel items \ Administrative Tools \ Data Sources (ODBC)

  

I only installed the MSSQL database, if you want to use other databases, such as MySQL, ORACLE, you need to first install the database driver.

Two drivers can connect to the SQL Server database, like SQL Native client driver can also modify the code as follows:

Char* szconnstrin="driver=sql Native Client; server=192.168.1.128,1433; Uid=sa; Pwd=sa; ";

<->server or authentication issues:

  

  

  Usually is the database address, port number, user name password This kind of problem causes, examines is good.

<-> Execution statement failed:

  Example of execution:

1 //*) execute the specific SQL2R = SQLExecDirect (stmt, (sqlchar*)"SELECT * FROM dbo. Mytab", sql_nts);3     if(r! = sql_success && r! =sql_success_with_info) {4cout<<"Execution failed! "<<r<<Endl;5Processlogmessages (SQL_HANDLE_STMT,STMT,"yyyyyy",0);6         return-1;7}

  

This is because we did not select the database at connect, so the execution failed.

We can modify the options when connect, specify database, as follows:

Char* szconnstrin="driver=sql Native Client; server=192.168.1.128,1433; Uid=sa; Pwd=sa;database=chroma; ";

4. Summary

Success lies in trying!

These are my experience in some of the attempts, although the database involved is only fur;

But to try more is the only way to a more advanced field.

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.