Solved

check data table, update data and add data table to sql 2005 database if not exist in c#

Posted on 2007-03-22
3
819 Views
Last Modified: 2011-09-20
i am using vc2005 and sdl 2005.  
i need to update and add a table to an sql database only if a the Data Table name does not exist in the database.
I want to loop all the Data Table of an existing sql db and verify that the name exits. If the name does not exist i want to add a new data table.
If the Data table exist i will also want to add data to the table champ.
i  need  the code in c#
for now my code is like that

///////////////////////////////////////////
else if (rbUpdate.Checked==true)
                        {
                    try
                    {
                    string namedb = cmbDataName.Text;
                    SqlConnection con1 = new SqlConnection("Data Source="+this.txtServName.Text.Trim()+";Initial Catalog="+namedb+";Integrated Security=True");
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con1;
                    cmd.CommandType = CommandType.Text;
                    con1.Open();

                    // need to write the Data Table test here and to add data if the table exist
                    // my db name is : named
                    // i open the server, how do i open the database ?


                    con1.Close();
                                           
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
}


///////////////////////////////

thanks                              
0
Comment
Question by:selvephilippe
3 Comments
 
LVL 9

Accepted Solution

by:
dbeneit earned 500 total points
ID: 18776143
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Table]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
select 1 as respuesta
else
select 0  as respuesta

if respuesta=1 not exist
if respuesta = 0 exist
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question