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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore 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.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now