hi. I try to select a colomn of names and use them to create new tables. I first try to use the Read method to put this names in an array so that i may use them afterwards to create the tables :
String* nomTitre[]=0;
String *q;
q="SELECT CQG from titres";
MySqlConnection * cntd= new MySqlConnection("User=root;Host=localhost;Port=3306;Database=base Weekly");
cntd->Open();
MySqlCommand *myCommand = new MySqlCommand(q,cntd);
MySqlDataReader *myReader=myCommand->ExecuteReader();
int i=0;
while (myReader->Read()){
nomTitre=myReader->GetString(0);
}
myReader->Close;
while (nomTitre){
q=String::Format(S"CREATE TABLE {0} (date DATE, open FLOAT, high FLOAT,low FLOAT,close FLOAT)",nomTitre);
myCommand->CommandText =q;
myCommand->ExecuteNonQuery();
i++;
}
but then i have a " non handled System.NullReferenceException", "the object reference is not defined to an instance obect" with the line
nomTitre=myReader->GetString(0);
...
Then I tried with MySqlDataTable :
q="SELECT code from titres";
MySqlDataTable *myDataTable = new MySqlDataTable(q,"User=root;Host=localhost;Port=3306;Database=base Weekly");
myDataTable->Active = true;
foreach(DataRow myRow in myDataTable->Row)
{
foreach(DataColumn myCol in myDataTable->Columns)
{
q=String::Format(S"CREATE TABLE {0} (date DATE, open FLOAT, high FLOAT,low FLOAT,close FLOAT)",myRow[myCol]);
myCommand->CommandText =q;
myCommand->ExecuteNonQuery();
}
}
myDataTable->Active =false;
but then i hace a problem with the type DataRow : " uncorrect use of this type as an expression"...
Can you help me on either of the solutions i tried ?
ExecuteReader() or MySqlDataTable
Re: ExecuteReader() or MySqlDataTable
There is no foreach construction in managed C++, use for statement instead.
Don't forget to close a connection after use.
Use pointers with managed variables.
Don't forget to close a connection after use.
Use pointers with managed variables.
Code: Select all
MySqlConnection * myConnection = new
MySqlConnection(S"User=root;Host=localhost;Port=3306;Database=base Weekly");
myConnection->Open();
__try
{
MySqlCommand * myCommand = new MySqlCommand();
myCommand->Connection = myConnection;
String * q = S"SELECT code from titres";
MySqlDataTable * myDataTable = new MySqlDataTable( q,
S"User=root;Host=localhost;Port=3306;Database=base Weekly");
myDataTable->Active = true;
for (int i = 0; i Rows->Count; ++i)
{
DataRow * myRow = myDataTable->Rows->Item[i];
for (int j = 0; j Columns->Count; ++j)
{
DataColumn * myCol = myDataTable->Columns->Item[j];
q = String::Format(S"CREATE TABLE {0} (date DATE, open FLOAT,
high FLOAT, low FLOAT, close FLOAT)",myRow->Item[myCol]);
myCommand->CommandText = q;
myCommand->ExecuteNonQuery();
}
}
myDataTable->Active = false;
}
__finally
{
myConnection->Close();
}