m stuck wid a prob wherein i hv a table named book already in database created programatically from an xml file.now if sum1 cums wid another xml file havin a table book then the prog gives an error 'the table already exists in database'...
can i hv a method by which i cn chek if da table exists already by an sql query nd if it does can i drop it ..... i tried dis sql steps...
string strComand = "IF EXISTS (SELECT * FROM "+table.TableName+")";
SqlConnection cn;
cn=conn();
cn.Open();
SqlCommand mycommand= cn.CreateCommand();
mycommand.CommandText = strComand.ToString();
try
{
string strCommand;
if(table.Rows.Count>=0)
{
strCommand = "DROP TABLE"+table.TableName;
mycommand.CommandText = strCommand.ToString();
mycommand.ExecuteNonQuery();
cn sum1 help me out wid da sql query written in c# for da above prob, will it wrk 4 multiple tables in an xml file...lik book nd author already existing and it drops dem and creates new table....1 mr thing ..how 2 knw dat da existing table mathces wid da new table by da help of sum conditional statements...
Jan MontanoPosted Sep 6, 2007, 9:56 PM
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Pay_Type]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Pay_Type]
GO
As long as the user executing that query has the appropriate permission, then there'll be no problem executing it.
By the way, I noticed you're call mycommand.CommandText = strComand.ToString();. You could just put mycommand.CommandText = strComand; instead because strComand is already a string and you don't need to call it's ToString() method anymore.
Cheers!