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

Thread: Table Existing

  1. #1
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179

    Table Existing

    Hi,
    Could any body tell me the proper way to find a table
    whether it exists or not.
    I use code:

    OleDbConnection dbConnection = new OleDbConnection(ConnectionString);
    string InsertQuery = "CREATE TABLE Processed (Cod INTEGER, ProcessDate DATETIME)";
    OleDbCommand dbCreateCommand = new OleDbCommand(InsertQuery, dbConnection);
    dbDeggyConnection.Open();
    dbCreateCommand.ExecuteNonQuery();

    But, if table already exist I get an error.
    Thx in advance.

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    the easiest sol. would be keeping try catch blocks

    OleDbConnection dbConnection = new OleDbConnection(ConnectionString);
    string InsertQuery = "CREATE TABLE Processed (Cod INTEGER, ProcessDate DATETIME)";
    OleDbCommand dbCreateCommand ;
    try
    {
    dbCreateCommand = new OleDbCommand(InsertQuery, dbConnection);
    }catch
    {
    }
    dbDeggyConnection.Open();
    dbCreateCommand.ExecuteNonQuery();

    yes, there are other sol. also.
    Paresh

  3. #3
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179
    Thanks a lot
    But I think it's not very good solution. May be you can get me another hints?
    Maksim.

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    you can access the table info from
    a metatable
    like
    execute the query

    SELECT * FROM sysobjects WHERE name = 'TableName'

    and check for the results...

    rest u know...



    enjoy,
    Paresh

  5. #5
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    forgot to give an example (i tested it on my machine)

    SELECT * FROM sysobjects WHERE name = 'pro'

    if exists will return with
    pro 965578478 U 1 1 1610612736 0 0 0 2003-02-10 10:38:35.190 0 0 0 U 1 67 0 2003-02-10 10:38:35.190 0 0 0 0 0 0 0

    these details in SQLDataReader r;

    int i=0;
    while(r.Read() )
    {
    i++
    }
    if( i >=1 )
    {
    // don't create table
    }

    else
    {
    //create table
    }




    Paresh

  6. #6
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179
    Thanks a lot
    But I think it's not very good solution. May be you can get me another hints?
    Maksim.

  7. #7
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179
    Thanks a lot
    But I think it's not very good solution. May be you can get me another hints?
    Maksim.

  8. #8
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179
    I'm sorry! I have problem with inet connection.

    Thx a million!!!

  9. #9
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    why ???
    what happened...

    SELECT * FROM sysobjects WHERE name = 'TableName'

    should work... it gives the existance of a table.
    if you are using SQL Server

    did you got that..
    Paresh

  10. #10
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179
    Hi!
    I use Microsoft Access.
    I think the problem in that.

  11. #11
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    yeah, look I gave you example with respect to SQL Server,
    I don't know that you are having MS Access. so I think for timebeing you should go with try...catch untill 'I'/'you'/someone
    finds the correct solution from access table..

    rgds,
    Paresh

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

Featured