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
821 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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