Boblysan

Boblysan

  • NA
  • 66
  • 0

DataReader and ODBC, help please...

Feb 14 2005 11:22 AM
Good morning, I am trying to create a very simple program which pulls information from a DB and populates the information in to a datagrid / table / listview, etc. (I haven not decided which one I am going to use yet) I am able to do this but am getting an odd error which I would like some help with. I can successfully connect to the data source, run a query, etc. The problem is that once I read the information I then attempt to close the reader and it failes. The code!!! private void Form1_Load(object sender, System.EventArgs e) { // Build a connection and SQL strings string connectionString = "DSN=MMB"; string SQL = "select mb_agent_id, mb_agent_name, status, reason, status_change from mb.t_mb_agent where status <> 'DISCONNECTED'"; // Create a connection object OdbcConnection conn = new OdbcConnection(connectionString); // Create a command object OdbcCommand cmd = new OdbcCommand(SQL); OdbcDataReader reader = cmd.ExecuteReader(); cmd.Connection = conn; // Open connection conn.Open(); try { while (reader.Read()) { listView1.Items.Add(reader.GetInt32(0) } } catch (OdbcException err) { MessageBox.Show("ERROR: " + err.Message); } reader.Close(); conn.Close(); } } } When getting to the line reader.Close(); I get a compile time error indicating that the ODBC connection / data source does not support this function. I am using the ODBC.NET component for my project. So I guess I have 2 questions. Is it bad if I do not close the reader but just close the connection instead? Secondly, if my goal is to go grab information from this db every 5 to 10 seconds, should you open the connection once when starting the application and then close it when done? Or should you open and close the connection for every interval I retrieve data? Please advise.. Oh, if you are wondering, the database type I am using is a memory resident db called TimesTen. Thank you,

Answers (2)