I've been reading "Apress - Introducing Visual CSharp 2010" the last 2 weeks and wanted to write my first app which include a connection to an access DB.
i found an article explain how to do so and in this article i found something that i dont underst in the example that was given :
using System.Data.Odbc
OdbcConnection DbConnection = new OdbcConnection("DSN=SAMPLE_ISAM")DbConnection.Open() // so far so good
OdbcCommand DbCommand = DbConnection.CreateCommand();
please thanks
Sam HobbsPosted Jan 16, 2011, 2:34 PM
No, it is not a constructor. A constuctor is a special member of a class that does whatver initialization is needed for the object. In your sample, the OdbcCommand is being constructed within the OdbcConnection.CreateCommand method. The OdbcConnection.CreateCommand method calls an OdbcCommand constructor.
Note that you should call methods such as that within a try/catch block and if your book does not explain that then it is not a good book.
In fact, ODBC is older technology and most new development don't use it. You should use ADO or direct calls to the database; for now, just learn ADO. The book teaching ODBC is probably not the best book. I suppose you can go ahead and learn ODBC but just understand that you are not likely to use ODBC; it is better to use ADO. Net.
DiegoPosted Jan 16, 2011, 11:19 AM
//Here you create a way to acess the DB
OdbcConnection DbConnection = new OdbcConnection("DSN=SAMPLE_ISAM");
//Here you open the connection, if it works, else you will got a exception
DbConnection.Open();
//Here you create an object which you will use to send commands to the BD
//this object use the previous connection, but there's a method in DbConnection that //create this command for you, so you only job is create the variable and call the method
OdbcCommand DbCommand = DbConnection.CreateCommand();
I hope help you.
Jaish MathewsPosted Jan 16, 2011, 11:06 AM
Yes. You assumed correctly. It's a ODBCCommand object variable. This object variable is instantiating using CreateCommand();
Once you go to the definition of CreateCommand() in .NET framework, you will see below lines which saying this method will return a type of ODBCCommand
// Returns:
// An System.Data.Odbc.OdbcCommand object.
public OdbcCommand CreateCommand();