简体   繁体   English

C ++ MFC对话框-如何将连接应用于应用程序的所有对话框?

[英]C++ MFC Dialog - How do I apply a connection to all the dialogs of my application?

I have this code to establish a connection with a MySQL database. 我有这段代码可以与MySQL数据库建立连接。 Everytime I want my dialogs to connect to the database, I have to type the whole thing. 每当我希望我的对话框连接到数据库时,我都必须输入整个内容。 Is there any other ways to apply it to all dialog pages? 还有其他方法可以将其应用于所有对话框页面吗? I tried to add EXTERN in front, but it says the code is being multiplied from another dialog. 我试图在前面添加EXTERN,但是它说代码正在从另一个对话框中复制。

unsigned short Port = 3306;
char *IPAddress = "127.0.0.1";
char *UserName = "root";
char *Password = "Root";
char *DBName = "inomatic";

MYSQL *ssock;
MYSQL_RES   *res;
MYSQL_ROW   row;
//char execsql[500];
ssock = (MYSQL *)malloc(sizeof(MYSQL));
mysql_init(ssock);
if(ssock == NULL)
{
    MessageBox("EROR: MySQL ssock init error. \n");
}
ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0);
if(!ssock)
{
    MessageBox("conn fail... \n");
    mysql_errno(ssock);
}

if(mysql_select_db(ssock, DBName) != 0)
{
    MessageBox("select db error. \n");
}

Create a "connection object" that holds the required Information. 创建一个包含所需信息的“连接对象”。 Make it global to your application or place it inside your CWinApp object. 将其全局化到您的应用程序中或将其放置在CWinApp对象中。 Initialiaze this object only once and use the global variable / singleton / CWinApp Object where you want and Need it. 仅初始化一次此对象,然后在需要和需要的地方使用全局变量/ singleton / CWinApp对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM