Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I am having an error while I am trying to install my project. It is a windows application. What I am trying to do is;
If the database exists the application will use the existing database on the SQL server, but if not it will create the database and use it on the application. What I have done so far;
The program can create the db successfully but if there is an existing db it wont let me install it.
It error that I am getting is on the GetSql() method. Because the db exists, it is throwing an error like 'please change the db name.It already exists.'.
On the Install() method I check it if the db exists or not but some how it is go in to the "if" statement and executes the AddDBtable() method.
BTW on the 'Sql.txt' it contains the script for the necessary db.

And I cant debug it because it wont let me do it while installation.
The code;

C#
[RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        System.Data.SqlClient.SqlConnection masterConnection = new System.Data.SqlClient.SqlConnection();
        public ProjectInstaller()
        {
            InitializeComponent();
        }
        private string GetSql(string Name)
        {

            try
            {
                masterConnection.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
                System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand("CREATE DATABASE AMAZON_PRODUCT_DB", masterConnection);
                Command.Connection.Open();
                Command.ExecuteNonQuery();
                Command.Connection.Close();
                Command.Dispose();
                // Reads the contents of the embedded file.
                StreamReader reader = new StreamReader("./SQLAuto.txt");
                return reader.ReadToEnd();

            }
            catch (Exception ex)
            {
               System.Windows.Forms.MessageBox.Show("In GetSQL: " + ex.Message);

               throw ex;
            }
        }

        private void ExecuteSql(string Sql)
        {
            masterConnection.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=AMAZON_PRODUCT_DB;Integrated Security=True";
            System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, masterConnection);
            Command.Connection.Open();
            try
            {
                Command.ExecuteNonQuery();
            }
            finally
            {
                // Closing the connection should be done in a Finally block
                Command.Connection.Close();
                Command.Dispose();
            }
        }

        protected void AddDBTable()
        {

            try
            {
                ExecuteSql(GetSql("sql.txt"));

            }
            catch (Exception ex)
            {
                // Reports any errors and abort.
                System.Windows.Forms.MessageBox.Show("In exception handler: " + ex.Message);
                throw ex;
            }
        }


        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";

            string cmdText = "select * from master.dbo.sysdatabases where name='AMAZON_PRODUCT_DB'";
            //string cmdText = "select * from sys.databases where name ='" + AMAZON_PRODUCT_DB + "'";
            bool bRet = false;

            using (SqlConnection sqlConnection = new SqlConnection(connString))
            {
                sqlConnection.Open();

                using (SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection))
                {
                    int nRet = sqlCmd.ExecuteNonQuery();
                    if (nRet <= 0)
                    {
                        bRet = false;
                    }
                    else
                    {
                        bRet = true;
                    }
                }
            }
            if (!bRet)
            {
                AddDBTable();
                masterConnection.Close();
                masterConnection.Dispose();
            }
Posted
Updated 27-Dec-10 6:08am
v2
Comments
TweakBird 27-Dec-10 12:09pm    
Edited code blocks.

1 solution

I reviewed your code and found that
sqlCmd.ExecuteNonQuery();
is returning -1. Since it does not query database therefore no outcome is created. This method is mainly used to insert values in a database.

I changed the following lines of codes
int nRet = sqlCmd.ExecuteNonQuery();
if (nRet == 0)


with the following line and it is working fine.
if (String.IsNullOrEmpty(sqlCmd.ExecuteScalar().ToString()))


Hope this may answer your question
 
Share this answer
 
Comments
Orcun Iyigun 27-Dec-10 12:40pm    
Yes that worked and now it is installing. Thanks for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900