Hi, i'm doing a small program that communicates with my first SQL Server, but I'm having some troubles with the C++ (I'm usued to C only, and use it for developing microcontrolers firmwares).

What Im trying to do here is it:

Write the USER and PASSWORD in a form, then send the values to my "database class", it will evaluate the user/pass and login in the mysql server and the program say "connection OK".

I've done it all EXCEPT the form thing, here is the problem I'm having:

I extract the string from the "userBox->Text" to another string and I pass that string to my class ( mySqlDatabase(char *serverName, string& user, string& password, char *dbName) ) but it gives out two diferent erros which seens to have no relation (and I'm quite newbie in C++ so both are misteries to me).

1>e:\users\user\documents\projetos\testes\project1\test form db last\test form db last\mydb.h(29) : error C2061: syntax error : identifier 'string'

1>e:\users\user\documents\projetos\testes\project1\test form db last\test form db last\Form1.h(153) : error C2661: 'mySqlDatabase::mySqlDatabase' : no overloaded function takes 4 arguments

here the codes that are giving me the most headache.

private: System::Void connectButton_Click(System::Object^  sender, System::EventArgs^  e) {
	String^ user = userBox->Text;
	String^ pwd = passBox->Text;

	mySqlDatabase conexao1("localhost", user, pwd, "cpp_data");

	if(conexao1.connection(1)){
		textBox1->Text = L"Connection OK";
	}
	Delete conexao1; //test purposes
}

the mySqlDatabase class

class mySqlDatabase{
private:
	char TABLE_OF_INTEREST[25];
	char SERVER_NAME[25];
	char DB_USER[25];
	char DB_USERPASS[25];
	char DB_NAME[25];

public:
mySqlDatabase(){}

	// /*
mySqlDatabase(char *serverName, string& user, string& pwd, char *dbName){
	strcpy( TABLE_OF_INTEREST, "users");
	strcpy( SERVER_NAME, "localhost");
	strcpy( DB_USER, user);
	strcpy( DB_USERPASS, pwd);
	strcpy( DB_NAME, "cpp_data");
}

int connection(int arg){
	MYSQL *hnd=NULL; // mysql connection handle
	const char *sinf=NULL; // mysql server information
	hnd = mysql_init(NULL);
	if (NULL == mysql_real_connect(hnd,SERVER_NAME,DB_USER,DB_USERPASS,DB_NAME,0,NULL,0)){
	<...>
}

Recommended Answers

All 6 Replies

You should note that String^ and std::string& are NOT the same thing. You can not mix the two (String is managed c++, std::string is unmanaged c++).

Also, strcpy() will not work with either String^ or std::string without some sort of conversion, such as using std::string's c_str() method.

Since you are writing managed c++ it might be better to change that class to use String^ instead of those character arrays as well as in all the functions.

using the String^ user/pass worked out just fine, thanks, but as you said, there is a need for conversion, and I'm having quite a hard time to make it works :\

what namespaces or .h are dispendables and how is the correct semantic of the c_str(); for it to work? I tried out a few times but had only error messages. :\

Here are the ones I tried:
std::DB_USER = user.c_str();
DB_USER = std::user.c_str();
DB_USER = user.c_str();

all turns out the same error:

1>e:\users\user\documents\projetos\testes\project1\test form db last\test form db last\mydb.h(35) : error C2228: left of '.c_str' must have class/struct/union

So, this is the basic body of my "mydb.h"

//_MYDB.H_

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#define W32_LEAN_AND_MEAN
#include <winsock2.h>
#include "mysql.h"

	using namespace std;
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

// change these to suit your setup
// prototypes
//void showTables(MYSQL*);
//void showContents(MYSQL*,const char*);

class mySqlDatabase{
private:
	char TABLE_OF_INTEREST[25];
	char SERVER_NAME[25];
	char DB_USER[25];
	char DB_USERPASS[25];
	char DB_NAME[25];

public:
	// /*
	mySqlDatabase( char *serverName, String^ user, String^ password, char *dbName){
			strcpy( TABLE_OF_INTEREST, "users");
			strcpy( SERVER_NAME, "localhost");

			strcpy( DB_USER, user);
			strcpy( DB_USERPASS, password);
			strcpy( DB_NAME, "cpp_data");
	}
// */
	~mySqlDatabase(){
		MYSQL *hnd=NULL; // mysql connection handle
		hnd = mysql_init(NULL);
		mysql_real_connect( hnd,SERVER_NAME,DB_USER,DB_USERPASS,DB_NAME,0,NULL,0);
		mysql_close(hnd);
	}

	int connection( void){
		MYSQL *hnd=NULL; // mysql connection handle
		const char *sinf=NULL; // mysql server information
		hnd = mysql_init(NULL);
		if (NULL == mysql_real_connect(hnd,SERVER_NAME,DB_USER,DB_USERPASS,DB_NAME,0,NULL,0))
				{
				//fprintf(stderr,"Problema encontrado ao conectar no banco de dados %s do servidor %s.\n",DB_NAME,SERVER_NAME);
				}
				else
				{
				//fprintf(stdout,"Conectado ao banco de dados %s com usaário %s '%s'.\n",DB_NAME,SERVER_NAME,DB_USER);
				sinf = mysql_get_server_info(hnd);

				if (sinf != NULL)
				{
				//fprintf(stdout,"Got server information: '%s'\n",sinf);
				//showTables(hnd);
				//showContents(hnd,TABLE_OF_INTEREST);
					return 1;
				}
				else
				{
				//fprintf(stderr,"Failed to retrieve the server information string.\n");
				return 0;
				}

				}
			return 1;
	}
}

See this old article -- scroll down to the section Converting Managed Strings to Character Arrays

Thanks for the article, it helped alot, BUT problems never stops.

Now, why can't I just declare that the CHAR[25] = *CHAR; ? char* should be just the same as a char[] shouldn't?

>>can't I just declare that the CHAR[25] = *CHAR; ?

No -- char str[25];

solved, I should use

char str[25];
*char str2;

strcpy(str, str2);

Thanks for all help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.