Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Hi,

I have created a webpate with a textbox and a button.

If we enter the tablename and press button it should show whether the table exists or not.

I want code to check whether the table exists in the databe or not.....
Posted
Updated 22-Nov-11 0:42am
v2
Comments
Smithers-Jones 22-Nov-11 6:40am    
Do not shout, that's rude.


Removed all-caps.

Hi ravi,
code to get all the tables in your database is as follows.
USE YourDBName
GO 
SELECT *
FROM sys.Tables
GO

Then you can implement your logic.
If you unable to get, send me a query.
 
Share this answer
 
You need to fire query and check if table is exist in database

SQL
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE name = 'tablename' AND xtype = 'U')
 
Share this answer
 
Use this on Button Click

SqlConnection conn = new SqlConnection();
       conn.ConnectionString = "Ur Connection String";
       conn.Open();
       string strCommand = "SELECT * FROM INFORMATION_SCHEMA.TABLES where table_type!='View' and table_name!='sysdiagrams'";
       SqlCommand cmd = new SqlCommand(strCommand, conn);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataTable dt = new DataTable();
       da.Fill(dt);

       foreach (DataRow dr in dt.Rows)
       {
           if (dr[2] != YourTextBox.Text)
           {
               Response.Write("Table not exist");
           }
       }

       cmd.Dispose();
       conn.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900