Solved

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

Posted on 2011-09-23
6
416 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 82

Accepted Solution

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

Author Comment

by:sydneyguy
Comment Utility
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
Comment Utility
its not the connection string thats workign fine
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 82

Assisted Solution

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

myConnection.Open()
0
 

Author Closing Comment

by:sydneyguy
Comment Utility
thanks for the input have it all working now, understanding it a lot better now thanks for the help
0
 
LVL 82

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 new code templates in NetBeans IDE 8.0 for Windows.

743 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

18 Experts available now in Live!

Get 1:1 Help Now