gpt4 book ai didi

c++ - 在 Visual Studio 中使用 C++/C# 连接 MySQL

转载 作者:可可西里 更新时间:2023-11-01 07:56:25 30 4
gpt4 key购买 nike

我知道 C++ 和(MySQL 和 PHP)。但现在我需要将 MySQL 与 C++ 连接起来。我需要有关 MySQL 服务器、MySQL 连接器设置和 Code::Blocks 配置以及连接代码的帮助。请帮我。我只知道 Code::blocks 或 Visual Studio。对于 C++ 连接,我不知道关于 MySQL 服务器和连接器连接器的任何事情。请帮助我。

最佳答案

对于 Visual Studio 2010 设置如下:

  • MySQL 服务器 5.5.38 (mysql-5.5.38-win32.msi)
  • MySQL 连接器网络 6.8.3 (mysql-connector-net-6.8.3.msi)

在 Visual Studio 中 =>> 文件 =>> 新建 =>> 项目 =>> Visual C++ =>> CLR =>> CLR 空项目

enter image description here

打开项目后,View =>> Solution Explorer =>> On Project Name Right Click =>> Properties =>> C/C++ Or Configuration Properties =>> General =>> Additional Include Directories =>> Click to编辑和编写:

C:\Program Files\MySQL\include (or that directory where you had installed MySQL and Mention its include folder).

例如( C:\Program Files\MySQL\MySQL Server 5.5\include )

enter image description here

现在在相同的属性对话框中导航到链接器 =>> 常规 =>>“添加其他库目录”=>>(写入包含 .dll 或 .lib 文件的文件夹路径,例如 C:\Program Files\MySQL\连接器 ODBC)

例如( C:\Program Files\MySQL\MySQL Connector Net 6.3.0 )

enter image description here

在相同的属性对话框中,导航到链接器 =>> 输入 =>> 附加依赖项 =>>(写下您要使用的 .lib 文件的名称,例如 libmysql.lib 或您要使用的任何其他库,它位于在“附加库目录”之上)。例如。 ( libmysql.lib )

enter image description here

最后,如果它不起作用,请转到 C:\Program Files\MySQL\include(或您安装 MySQL 的目录并提及其包含文件夹)。

然后在 Windows 搜索选项中搜索写入“libmysql.lib”。然后将此 lib 文件复制到您的项目 bin debug,release 文件夹中。此外,有时它可能会在错误消息中指出某些其他 lib 或 DLL 文件的要求。

在 MySQL 目录中执行相同的进程搜索并复制粘贴它们。如果这不起作用,则将上面提到的 DLL、lib 文件复制到项目的每个文件夹中,然后在其中必须起作用。我是这样做的。

数据库连接的MySQL代码:

#include "my_global.h"
#include "mysql.h"
#include<time.h>
#include<stdio.h>
#define SERVER "localhost"
#define USER "root"
#define PASSWORD ""
#define DATABASE "database"
int main()
{
char ch;
clock_t begin, end;
//MYSQL *connect=mysql_init(NULL);
//connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0);
//if( ! connect) { printf("MySQL Initialization or Connection Failed!\n"); return 0; }
//mysql_query(connect,"insert into test values(2)");

//mysql_query(connect,"INSERT INTO student VALUES('111','aaaa','bbbb')");
begin = clock();
//printf("Application SN\tMerit\tAdmission Roll\n-------------- ------- ----------------\n");
MYSQL_RES *res_set; MYSQL_ROW row;
mysql_query(connect,"SELECT * FROM database");
res_set = mysql_store_result(connect); //unsigned int numrows = mysql_num_rows(res_set);
// while ( row = mysql_fetch_row(res_set) )
//printf(" res %s\t",row[0]); /* Print the row data */
end = clock();
printf("Execution Time: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
mysql_close(connect);
scanf("%c",&ch);
//getch();
//cin>>ch;
return 0;
}

关于c++ - 在 Visual Studio 中使用 C++/C# 连接 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287335/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com