Solved

C# access a mysql database using ODBC have code running but not returning any records

Posted on 2011-09-23
6
421 Views
Last Modified: 2013-12-17
Hi Experts, easy on for you, i need to read a table from a mysql data base, have got the code going below, it runs does not throw up any errors but does not return any records, there are records there, but new are for me and am looking for help to get it going and some good reference material to read so i can underderstand  which ODBC connection comes in what order and what dataset needs what reference to what table ect.basicaly how to do it , but having a running code that works to play with.
any help would be appresiated, thanks

private System.Data.Odbc.OdbcConnection myConnection;
        private System.Data.Odbc.OdbcCommand cmd;
        private System.Data.Odbc.OdbcDataReader OdbcDR;
        private string connectionString;

        private void Readrec_Click(object sender, EventArgs e)
        {
           
            connectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" + txtIP.Text + ";PORT=" + txtPort.Text + ";DATABASE=" + txtDatabase.Text + ";UID=" + txtUsername.Text + ";PWD=" + txtPassword.Text + ";OPTION=3";
           // OdbcCon = new System.Data.Odbc.OdbcConnection(ConStr);
 
            OdbcConnection myConnection = new OdbcConnection(connectionString);
            DataSet ds = new DataSet("vtiger_account");
            OdbcDataAdapter da = new OdbcDataAdapter();
            OdbcCommand cmd = new OdbcCommand();
            cmd.CommandText = "SELECT vtiger_account.accountid, vtiger_account.account_no FROM vtiger_account ORDER BY vtiger_account.accountid DESC";
            da.SelectCommand = cmd;
            da.SelectCommand.Connection = myConnection;
            da.Fill(ds, "vtiger_account");
            DataTable tb = new DataTable("vtiger_account");
      //      DataTable tb = ds.Tables("accountid");

            foreach (DataRow row in tb.Rows)
            {

                string somename = (string)row["accountid"];
                //lstNames.Items.Add(somename);

            }  
0
Comment
Question by:sydneyguy
  • 3
  • 3
6 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 36590767
According to this page http://support.microsoft.com/kb/310988 , it looks like you're not opening the connection.
0
 

Author Comment

by:sydneyguy
ID: 36591523
connection seems to be opening ok as it does not throw up any errors it think its more to do with the loading of the data to the actual table, but could be wrong i have no workign code to say either way thus my question, but not sure how to check this
0
 

Author Comment

by:sydneyguy
ID: 36591955
its not the connection string thats workign fine
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 500 total points
ID: 36593142
Some where after the OdbcConnection is defined, you should have:

myConnection.Open()
0
 

Author Closing Comment

by:sydneyguy
ID: 36593221
thanks for the input have it all working now, understanding it a lot better now thanks for the help
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36593241
You're welcome, glad you got it working.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS 2017 18 105
Input parameteres to DragOver 2 37
what is the best Integrated development environment 2 39
parse string in c# 5 29
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question