Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
822 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 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