| | |
How to check if table exists, if not create it (exceptions)...
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
I need my program to connect to a database, check if a table exists, and if not, create it.
I intend to check the presence of a table by simply running a "select * from table" query via an SqlCommand. I am going to put this in a try block. My problem is, how/where do I actually create the table? In the catch block? What if the SqlException was not due to the table not existing, but for instance due to an incorrect password? What is the best way to do this?
Thanks!
I intend to check the presence of a table by simply running a "select * from table" query via an SqlCommand. I am going to put this in a try block. My problem is, how/where do I actually create the table? In the catch block? What if the SqlException was not due to the table not existing, but for instance due to an incorrect password? What is the best way to do this?
Thanks!
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
A better approach is to see if it exists through sysObjects, and create it there, catch any errors on that.
Then in c# run an Execute non query. Place that into a try catch
C# Syntax (Toggle Plain Text)
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Country_Master]') AND type in (N'U')) CREATE TABLE [dbo].[Country_Master]( [County_id] [int] IDENTITY(1,1) NOT NULL, [CountryName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL )
Then in c# run an Execute non query. Place that into a try catch
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: identify current user asp.net c# project
- Next Thread: AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap()
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update upload usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml





