Results 1 to 5 of 5

Thread: [RESOLVED] help connecting to DB

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] help connecting to DB

    Hi all,


    Im not sure where im going wrong but here is my source code

    its a single access2003 DB with one table called WinAudi2

    Code:
    BindingSource CA_bindingsource = new BindingSource();
            
    
            private void button1_Click(object sender, EventArgs e)
            {
            string MyString;    
            System.Data.Odbc.OdbcConnection cn;
            System.Data.Odbc.OdbcCommand cmd;
            MyString = "Select * from WinAudi2"; 
            cn = new System.Data.Odbc.OdbcConnection(@"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\Sweep.mdb;Uid=Admin;Pwd=;");
            cmd = new System.Data.Odbc.OdbcCommand(MyString, cn);
    
            try
            {
                cn.Open();
    
    
                CA_bindingsource.DataSource = cmd;
                dataGridView1.DataSource = CA_bindingsource;
                MessageBox.Show("Connected");
            }
    
            catch (Exception ex)
            {
                //MessageBox.Show("Error with CSV backup" + Environment.NewLine + ex.Message);
                Console.WriteLine(ex.ToString());
                MessageBox.Show("Problem" + Environment.NewLine + ex.Message);
            }
    
    
            finally
            {
                cn.Close();
            }

    and when i go to run the code i get this



    Am i not structuring something right?
    Last edited by Crash893; Feb 7th, 2007 at 07:33 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help connecting to DB

    You don't bind your BindingSource to the command. You execute the command, store the result set in an object and then bind that to the BindingSource. Given the fact that you want to bind the data to a grid you would execute the command using a OleDbDataAdapter to Fill a DataTable. It's the DataTable you'd bind to the BindingSource.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: help connecting to DB

    Im not sure exactly what you mean?

    i should be storing cmd in a datatable?, then move it to the binding source?



    Also i thought that the odbc adapter was better for some reason?
    Last edited by Crash893; Feb 7th, 2007 at 09:26 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help connecting to DB

    If you're using Access then OleDb is preferable to Odbc.

    As for the mechanics, a Command doesn't have any data. A Command basically contains some SQL code and a reference to a Connection. The Connection is obviously used to connect to the database. The SQL code is what gets executed to perform some action. When the Command is executed with a query then a result set is created, which you can then read with a DataReader. If you only want read-only, forward-only access to the data then you should use a DataReader explicitly.

    In your case you random, read/write access to the result set so you should use a DataAdapter. It will wrap your Command and hide the DataReader to make your life easier. You just use its Fill method to populate a DataTable.

    Firstly, while Odbc will work I suggest switching to OleDb. Either way, read about the DataAdapter class for the namespace you're using. Pay special attention to the constructors, the SelectCommand property and the Fill method. Also, there are numerous ADO.NET tutorials available on the Web because ADO.NET is a pivotal technology. Here's just one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: help connecting to DB

    Last edited by Crash893; Jul 17th, 2007 at 06:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width