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
820 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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