C steps to connect MySQL VC

Source: Internet
Author: User

Beginner C, see the C connection MySQL tutorial is not many, encountered a lot of problems, saw many allies of the solution, a little vague (for me this rookie), the following post specific steps to study together:

1.C connection MySQL method: C, C + +, ODBC, Java, Net ..., here to see the connection of C, first into the MySQL driver official website download CONNECTOR/C http://dev.mysql.com/downloads/ connector/Choose c version download

2.C in the VC 6 environment to connect MySQL: Below the source code (see what others write, as a reference)

#include "stdafx.h" #include <stdio.h> #include <winsock2.h> #include <mysql.h> #include <stdlib.h >/* database connection with macro */#define HOST "localhost" #define USERNAME "root" #define PASSWORD "root" #define DATABASE "test" void Query_    SQL (char* sql); int main () {char *query;    query= "SELECT * from a";    Query_sql (query); system ("pause"); return 0;} void Query_sql (char* sql) {MYSQL my_connection;/* This is a database connection */int res;/* return flag after executing SQL sentence */mysql_res *res_ptr;/* pointing to Pointer to the result of the query */Mysql_field *field; /* Field structure pointer */Mysql_row result_row; /* Query information returned by line */int row, column;    /* The number of rows and columns returned by the query */int i, J;    /* Initialize MySQL connection my_connection*/mysql_init (&my_connection);                                   /* Establish MySQL connection */if (NULL! = Mysql_real_connect (&my_connection, HOST, USERNAME, PASSWORD, Database, 0, NULL, client_found_rows) */* Connection succeeded */{printf ("DB query Query_sql connection succeeded!        \ n ");        /* Set the query code to GBK to support the Chinese */mysql_query (&my_connection, "Set names GBK"); res = MysQl_query (&my_connection, SQL);            if (res)/* Execution failed */{printf ("Error:mysql_query!\n");        /* Close Connection */Mysql_close (&my_connection); } else/* Now represents the success of the execution */{/* The results of the query to res_ptr*/res_ptr = Mysql_store_result (&my_co            Nnection); /* If the result is not empty, put the result print*/if (res_ptr) {/* Gets the number of rows and */column = Mysql_num_f                Ields (RES_PTR);                row = Mysql_num_rows (res_ptr);                printf ("Query to%d lines \ n", row); /* The field name for output results */for (i = 0; field = Mysql_fetch_field (res_ptr); i++) printf ("%10s", field-                >name);                printf ("\ n"); /* Output results by line */for (i = 1; i < row+1; i++) {Result_row = Mysql_fetch_row (                    RES_PTR);             for (j = 0; j < column; J + +) printf ("%10s", Result_row[j]);       printf ("\ n");        }}/* Do not forget to close the connection */Mysql_close (&my_connection);    }} else {printf ("Database connection failed"); }}

3. Required libraries for mysql.h required to join the header file

Steps: Tools = = "option = =" Directory (click this option) = = "Include files select Add

3. Join the Mysql.h implementation, on the win platform for the Libmysql.lib file ( as to why Lib file, see figure Understanding or own Internet understanding)

3. Add this file to the environment at compile time Libmsql.lib steps:

Project = = "Set = =" Connect (select this tab) = = "Add Libmysql.lib to the object/library module

4. At this point, the dynamic connection library Libmysql.dll copied to the Realse/debug directory to be published (because the DLL is loaded when the dynamic runtime executable file, at this time to ensure that the normal operation) must be this step, or it will be an error that can not find this file

5. Compile run: Result

Summarize:

The C Connection database has two forms. One is the official C driver package, and the other is the ODBC connection. The file mysql.h introduced in the code is a declaration of the operation of MySQL, the implementation of the method file is the Libmysqld.dll file, and the Libmysql.lib file is to be added at compile time The Libmysqld.dll function lets the compiler pass, knowing that there is an implementation of this function. But this connection method is coupled with the MySQL database, the maintainability of the code is very bad, it is recommended to use ODBC Bar (folly).

C steps to connect MySQL VC

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.