Solved

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

Posted on 2011-09-23
6
417 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now