ExecuteReader() or MySqlDataTable

ExecuteReader() or MySqlDataTable

Postby theud » Mon 27 Dec 2004 17:28

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[i]=myReader->GetString(0);
}
myReader->Close;
while (nomTitre[i]){
q=String::Format(S"CREATE TABLE {0} (date DATE, open FLOAT, high FLOAT,low FLOAT,close FLOAT)",nomTitre[i]);
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[i]=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 ?
theud
 

error

Postby theud » Tue 28 Dec 2004 11:27

ok minor error, just write rather :

String* nomTitre[]=new String*[1000];
theud
 

Re: ExecuteReader() or MySqlDataTable

Postby Oleg » Wed 29 Dec 2004 11:02

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.

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 < myDataTable->Rows->Count; ++i)
        {
          DataRow * myRow = myDataTable->Rows->Item[i];
          for (int j = 0; j < myDataTable->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();
      }
Oleg
Devart Team
 
Posts: 264
Joined: Thu 28 Oct 2004 13:56


Return to dotConnect for MySQL