asp.net中C#连接mysql数据库程序代码

作者:袖梨 2022-06-25
 代码如下 复制代码

#include
#include
#include
 
int main(void)
{
        const char *host = "localhost";
        const char *user = "root";
        const char *pass = "yourpassword";
        const char *db   = "mysql";
        /* 定义mysql变量 */
        MYSQL mysql;
        MYSQL_RES *rs;
        MYSQL_ROW row;
        mysql_init(&mysql); /* 初始化 */
        /* 连接数据库 */
        if (!mysql_real_connect(&mysql, host, user, pass, db, 0, NULL, 0))
        {
                printf("%s", mysql_error(&mysql));
        }
        // get more result test
        /*
        char *sql = "select user,host from user";
        if (mysql_query(&mysql, sql)!=0) 
        { //查询
                printf( "%s", mysql_errno(&mysql), mysql_error(&mysql));
        }
        rs = mysql_store_result(&mysql); // 获取查询结果
        while ((row = mysql_fetch_row(rs))) 
        {      // 获取每一行记录
                printf( "%s----%sn", row[0], row[1]);
        }
        */
        // get one result test
        char *sql = "select user from user where host='127.0.0.1'";
        if (mysql_query(&mysql, sql)!=0)
        { //查询
                                                                                                                  1,7           Top
        if (mysql_query(&mysql, sql)!=0)
        { //查询
                printf( "%s", mysql_errno(&mysql), mysql_error(&mysql));
        }
        rs = mysql_store_result(&mysql); // 获取查询结果
        row=mysql_fetch_row(rs);
        printf("%sn",row[0]);
        //*/
        mysql_free_result(rs); /* 释放结果集 */
        mysql_close(&mysql); /* 关闭连接 */
        return 1;
}

相关文章

精彩推荐