Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# alternative for MFC CTables ?

Status
Not open for further replies.

DGRFL

Programmer
Dec 28, 2000
36
BE
Hello,

In an old MFC tool, I used following code to determine all tables in an ODBC connection.
// Get a list of the tables
CTables rs(&m_db);
rs.Open(NULL, NULL, NULL, "TABLE");

// Cycle through all the tables
CString strTableRef;
while (!rs.IsEOF())
{
}

How can I achieve this in C# (either with ODBC or OLE-DB)?

More generally spoken, is there some 'migration guide' with a reference table between MFC classes and the C# alternative ?

Regards

Luc
 
>> is there some 'migration guide' with a reference table
>> between MFC classes and the C# alternative ?

Maybe someone has written a book on that subject, C# from a MFC perspective, but I am not aware of it. Generally you just dig into the .NET Class Library Reference documentation to find out what classes are there. The naming convention is pretty straight forward with all the namespaces etc.

There are not many similarities between MFC and the new .NET Class Library. That is mostly a good thing. The .NET Class Library is completely new so it does not resemble anything really. There are some small similarities here and there with Java but that is about it.


-pete
 
I would suggest picking up something on ADO.NET which covers this area. The mechanism of accessing ODBC objects has been simplified dramatically although it may not seem that way on first glance (The framework introduces its own nomenclature).

Not knowing MFC, I would guess that there will be the equivalent of:

foreach (DataTable dt in DataSet ds.Tables)
{
...
}

where the dataset was pulled from an ODBC source or some such thing that will accomplish what you are looking for.

d-
 
"Generally you just dig into the .NET Class Library Reference documentation" is what I tried, but with no luck so far...

And concerning the 'DataSet' : According to my first examinations, the main strategy is :
- connect
- create a command for a specific SQL statement
- create a DataAdapter
- create a DataSet
Now DataSet.Tables contains all tables related in the command.

But I have not SQL statement !!!!

I want to access the 'meta'-information of the database. Which tables are present in this database-schema?
And I don't want to work with DB-vendor related views like USER_TABLES etc.

Regards
Luc
 
There is a method called GetSchemaTable for a variety of readers (ODBCDataReader, SQLDataReader, OleDBDataReader, OracleDataReader) but I have not personally used this. Search msdn ( for GetSchemaTable and there are a few examples on how to use this such as:

Code:
DataTable schemaTable = myReader.GetSchemaTable();

foreach (DataRow myRow in schemaTable.Rows)
{
  foreach (DataColumn myCol in schemaTable.Columns)
    Console.WriteLine(myCol.ColumnName + " = " + myRow[myCol]);
  Console.WriteLine();
}

They do mention that the results are highly dependent on the database driver.

Hope that helps.

Dean
 
If Deans method does not pan out, this works against a SQL Server Database.
Code:
OleDbConnection cn = new OleDbConnection();
DataTable dt;

// use a correct connection string for your environment
cn.ConnectionString = 
	"Provider=SQLOLEDB;Data Source=MyServer;User ID=sa;Password=sapwd;Initial Catalog=Rnd";
cn.Open();

// query the list of tables
dt = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, 
										new Object[] {null, null, null, "TABLE"});

// crude display of table names ;-)
for( int n=0; n<dt.Rows.Count; n++)
{
	System.Windows.Forms.MessageBox.Show(dt.Rows[n].ItemArray[2].ToString());
}
cs.Close();

-pete
 
Hello,

It seems that the GetOleDBSchemaTable() is the method I was looking for.

Indeed, digging deep in the Class Library is what I need to do more often...

Thanks to both of you
Luc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top