作为刚开始进行编程的新手,我急切需要知道很多关于编程方面的知识,所以一个一个向大家请教,麻烦各位不吝赐教,也希望大家能帮我这个忙,谢谢在C++中,不知道有哪些连接数据库的方法,好像有ADO、ODBC等,大家能不能用个例子说明一下这两种方法是怎么用的,或者怎么实现的,数据库用MYSQL,并说明一下哪些地方需要进行手动设置,小弟在这不胜感激。。

解决方案 »

  1.   

    搜索“C++ ADO 连接mysql”,“C++ ODBC 连接mysql”,估计不少
      

  2.   

    首先你要看看MYSQL的HELP;
    其次熟悉你要使用的语言
    最后自己动手做一下,不懂的问题再问
      

  3.   

    顺便也看看jdbc回来讲讲public Connection getConn(){
    String user = "test";
    String password = "test";
    String driver = "com.mysql.jdbc.Driver";
    String uri = "jdbc:mysql://10.10.1.102:3306/db_tigerstone?useUnicode=true&characterEncoding=UTF8";
    try {
    Class.forName(driver);
    conn = DriverManager.getConnection(uri, user, password);
    return conn;
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    }
    }
      

  4.   

    MySQL 中最快的则是C API。通过ADO速度会慢上很多,比API差一个数量级,但显然比API的可移植性要强上许多。
      

  5.   

    谢谢各位光临,主要的是我在自学,许多东西不清楚,下面就使用ODBC进行数据库连接提出几个问题,麻烦大家帮忙详细解答一下,谢谢
    1. 在使用ODBC连接数据库时,为什么要在控制面板/管理工具/ODBC数据源中进行设置?所起的作用是什么?这个步骤在使用ODBC连接时是不是必须的?麻烦能比较详细的讲下,谢谢
    2. 在C++程序(控制台程序)中,是怎样实现连接的,打开,操作,关闭等等是如何实现?
      

  6.   

    1. 在使用ODBC连接数据库时,为什么要在控制面板/管理工具/ODBC数据源中进行设置?所起的作用是什么?这个步骤在使用ODBC连接时是不是必须的?麻烦能比较详细的讲下,谢谢这个解释起来比较多,建议你可以先在GOOGLE中搜索一下ODBC / ADO 等相关的介绍。
    2. 在C++程序(控制台程序)中,是怎样实现连接的,打开,操作,关闭等等是如何实现?
    这个你可以直接在MYSQL的C API文档中找到例子。
    =======================================================================
    Application programs should use this general outline for interacting with MySQL: Initialize the MySQL library by calling mysql_library_init(). This function exists in both the mysqlclient C client library and the mysqld embedded server library, so it is used whether you build a regular client program by linking with the -libmysqlclient flag, or an embedded server application by linking with the -libmysqld flag. Initialize a connection handler by calling mysql_init() and connect to the server by calling mysql_real_connect(). Issue SQL statements and process their results. (The following discussion provides more information about how to do this.) Close the connection to the MySQL server by calling mysql_close(). End use of the MySQL library by calling mysql_library_end(). 
    While a connection is active, the client may send SQL statements to the server using mysql_query() or mysql_real_query(). The difference between the two is that mysql_query() expects the query to be specified as a null-terminated string whereas mysql_real_query() expects a counted string. If the string contains binary data (which may include null bytes), you must use mysql_real_query(). For each non-SELECT query (for example, INSERT, UPDATE, DELETE), you can find out how many rows were changed (affected) by calling mysql_affected_rows(). 
    In both cases, you access rows by calling mysql_fetch_row(). With mysql_store_result(), mysql_fetch_row() accesses rows that have previously been fetched from the server. With mysql_use_result(), mysql_fetch_row() actually retrieves the row from the server. Information about the size of the data in each row is available by calling mysql_fetch_lengths(). After you are done with a result set, call mysql_free_result() to free the memory used for it. 
    MYSQL_ROW row;
    unsigned int num_fields;
    unsigned int i;num_fields = mysql_num_fields(result);
    while ((row = mysql_fetch_row(result)))
    {
       unsigned long *lengths;
       lengths = mysql_fetch_lengths(result);
       for(i = 0; i < num_fields; i++)
       {
           printf("[%.*s] ", (int) lengths[i], 
                  row[i] ? row[i] : "NULL");
       }
       printf("\n");
    }
    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
      

  7.   

    请问下,是否用ODBC连接数据库就必须创建ODBC数据源,必须在控制面板中设置吗?
      

  8.   

    ===楼主发表于:2009-09-22 16:37:1409的的贴子!
    vicozo (冰露花语)
      '截至2010-08-23 14:58:38  用户结帖率60.53%当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖