鱼C论坛

 找回密码
 立即注册
查看: 1275|回复: 2

VS2013连接MySQL数据库

[复制链接]
发表于 2017-2-20 18:48:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
环境:VS2013,debug版本,MySQL5.7.17版本
1、安装好MySQL数据库,确保服务器中有MySQL服务,可以在资源管理器中查看
2、打开VS2013,新建一个项目,如果安装的是32位的MySQL,则不需要进行本步,如果安装的是64位的MySQL,则需要在win32控制台上新建一个64位的环境,方法是右击项目,选择属性,右上角有一个配置管理器,点击一下,会有一个活动解决方案平台,新建一个X64即可
3、然后是对MySQL中的include和lib 文件的路径加到VS中
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <WinSock2.h>
  6. #include "mysql.h"
  7. #include "winsock.h"
  8. #include <iomanip>
  9. using namespace std;
  10. int res = 0;
  11. MYSQL_RES *result = NULL;
  12. MYSQL mysql;
  13. MYSQL_ROW sql_row;
  14. MYSQL_FIELD *fd = NULL;


  15. char column[20][20] = { 0 };
  16. int selectData(char * SQL, char * Msg);


  17. int main()
  18. {
  19. mysql_init(&mysql);
  20. if (!mysql_real_connect(&mysql, "localhost", "root", "zqh18362961932", "mysql", 3306, NULL, 0))
  21. {
  22. cout << "连接数据库时发生错误!" << endl;
  23. }
  24. else
  25. {
  26. cout << "连接数据库成功!" << endl;
  27. /*
  28. //设置编码格式,否则在cmd下无法显示中文;并且插入的中文也会显示乱码
  29. mysql_query(&mysql, "SET NAMES GBK");
  30. char *msg = NULL;
  31. //查询数据
  32. char select[] = "select * from user";
  33. if (selectData(select, msg) != 0)
  34. {
  35. cout << msg << endl;
  36. }
  37. */
  38. }
  39. /*
  40. if (result != NULL)mysql_free_result(result);//释放结果资源
  41. mysql_close(&mysql); //释放数据库
  42. system("pause");
  43. return(0);
  44. */
  45. system("pause");
  46. //return(0);
  47. }
  48. /*
  49. int selectData(char *SQL, char * Msg)
  50. {
  51. res = mysql_query(&mysql, SQL);//查询
  52. if (!res)
  53. {
  54. result = mysql_store_result(&mysql);//保存查询到的数据到result
  55. if (result)
  56. {
  57. int i, j;
  58. cout << "number of result:" << mysql_num_rows(result)<<endl;
  59. //获取列名
  60. for (i = 0; fd = mysql_fetch_field(result); i++)
  61. {
  62. strcpy(column[i], fd->name);
  63. }
  64. j = mysql_num_fields(result);
  65. //输出列名
  66. for (i = 0; i<j; i++)
  67. {
  68. cout << setw(10) << setiosflags(iOS::left) << column[i];
  69. //printf("%s\t", column[i]);
  70. }
  71. cout << endl;
  72. //获取具体的数据
  73. while (sql_row = mysql_fetch_row(result))
  74. {
  75. for (i = 0; i<j; i++)
  76. {
  77. cout << setw(10) << setiosflags(ios::left) << sql_row[i];


  78. //printf("%s\t",sql_row[i]);
  79. }
  80. cout << endl;
  81. }
  82. }
  83. return 0;
  84. }
  85. else
  86. {
  87. Msg = "query sqlfailed!";
  88. }
  89. return(1);


  90. }
  91. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-2-21 10:52:02 | 显示全部楼层
嗯~楼主为什么不用sql server呢,VS不是跟sqlsever才是配套的么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-21 19:13:17 | 显示全部楼层
jackche0214 发表于 2017-2-21 10:52
嗯~楼主为什么不用sql server呢,VS不是跟sqlsever才是配套的么

现在的项目需要MySQL,所以就用它啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 15:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表