dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Simple beginners problem

  1. #1
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Simple beginners problem

    Hi,

    I'm new to ADO. NET and I want to create a complete new Access DB (mdb)
    insert few tables and some columns in each table.
    Further on I want add or update existing data.

    But unfortunately I have trouble to find information in the web. This is the code
    I have so far.

    Code:
    using System;
    using ADOX;
    using ADODB;
    using System.IO;
    
    public class Program
    {
        static int Main()
        {
            if (File.Exists("C:\\Exercise1.md")) File.Delete("C:\\Exercise1.mdb");
    
    
            // CREATE FILE
            
            ADOX.CatalogClass catDatabase;
            string strDatabase = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                               "Data Source='C:\\Exercise1.mdb'";
    
            catDatabase = new ADOX.CatalogClass();
            catDatabase.Create(strDatabase);
            Console.WriteLine("A new Microsoft JET database named " +
                              "Exercise1.mdb has been created\n");
           
    
    
    
            // CREATE DB with a table  
            ConnectionClass conDatabase = new ConnectionClass();
            try
            {
                object objAffected;
                string strStatement = "CREATE TABLE Customers(FirstName char, " +
                                                              "MiddleName String, " +
                                                              "LastName varchar);";
                string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                       "Data Source='C:\\Exercise1.mdb';";
    
                conDatabase.Open(strConnection, "", "", 0);
                conDatabase.Execute(strStatement, out objAffected, 0);
            }
            finally
            {
                conDatabase.Close();
            }
          
    
    
    
            // ADD column
            ADODB.Connection conADO = new ADODB.Connection();
            object obj = new object();
            string stStatement;
    
            conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" +
                        "Data Source='C:\\Exercise1.mdb'", "", "", 0);
            stStatement = "ALTER TABLE Customers ADD COLUMN CellPhone string;";
            conADO.Execute(stStatement, out obj, 0);
    
            conADO.Execute("INSERT INTO Customers(FirstName, MiddleName, LastName) " +
                                  "VALUES('James', 'Carlton', 'Male');", out obj, 0);
    
           
    
           
            Console.WriteLine("A new column named CellPhone has been added " +
                              "to the Employees table.");
    
            conADO.Close();
    
    
    
    
    
            // UPDATING OF A COLUMN???
    
    
    
    
    
            return 0;
        }
    }

    Could some explain, how to extend that lower part, in order to update table contents, please?
    And how can I search for a specific value on a table?



    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    303

    Re: Simple beginners problem

    For update

    Code:
    UPDATE Table SET Field1 = 'value' WHERE Field2 = 'criteria'
    For select

    Code:
    SELECT * FROM Table WHERE Field2 = 'criteria'

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Simple beginners problem

    Sounds like fill in the blank homework. We usually don't do others work for them.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Simple beginners problem

    @dglienna
    It cannot be some homework. I'm 39 years old. Just think of it for a few seconds :-/


    I found a better way throurgh OleDB,
    but thanks to dee-u dee-u anyway.

  5. #5
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    303

    Re: Simple beginners problem

    Quote Originally Posted by MNovy View Post
    I found a better way throurgh OleDB,
    but thanks to dee-u dee-u anyway.
    May we know what would be that better way through OleDB?

  6. #6
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Simple beginners problem

    Quote Originally Posted by dee-u View Post
    May we know what would be that better way through OleDB?
    I'll post my code later, after I could finish my little tool :-D
    I have to manage some documents, where Excel is not fast enough.
    But Access is new to me. I need a bit time to get along with, so please excuse my beginner's questions. I'm not a student or sth like this.

  7. #7
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Simple beginners problem

    there is no SQL command that lets you create an empty database in MS Acess, unlike with the other databases. so I always include a blank MDB whenever my application needs to created a new Access database on the fly (File.Copy ). By the way, the code posted above is ADO.
    Busy

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)