Beispiel #1
0
        /// <summary>
        /// Set the budget to an existing database.
        /// </summary>
        public static async void SetBudget()
        {
            if (dbConnection != null && dbConnection.State != ConnectionState.Closed)
            {
                SetBudget setBudget = new SetBudget(firstSet: false);
                setBudget.ShowDialog();

                string commandString = String.Format(@"UPDATE [{0}].[dbo].[SubGet]
                                                    SET Expenses = {1}
                                                    WHERE Id = 0", dbConnection.Database, BudgetValue);
                using (SqlCommand command = new SqlCommand(commandString, dbConnection))
                {
                    await command.ExecuteNonQueryAsync();
                }
                InitializeDataGrid(dbConnection);
            }
            else
            {
                MessageBox.Show("A connection hasn't been established, connect to a database first and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a database with a given name and sets the Budget value.
        /// </summary>
        /// <param name="newDbName"></param>
        public static async void CreateDB(string newDbName)
        {
            // TODO: MAKE CODE CLEANER!!!!!!!

            ConnectionString.Append("Database=master;");
            dbConnection = new SqlConnection(ConnectionString.ToString());
            await dbConnection.OpenAsync();

            string commandString = String.Format("CREATE DATABASE {0}", newDbName);

            using (SqlCommand command = new SqlCommand(commandString, dbConnection)){
                try
                {
                    await command.ExecuteNonQueryAsync();

                    //TODO: ask for start budget
                    SetBudget setBudget = new SetBudget(firstSet: true);
                    setBudget.ShowDialog();


                    string tableCreate = String.Format(@"CREATE TABLE [{0}].[dbo].[SubGet]
                                                (
                                                    [Id] INT NOT NULL IDENTITY(0,1) PRIMARY KEY, 
                                                    [Name] NVARCHAR(50) NOT NULL,
                                                    [Expenses] SMALLMONEY NOT NULL

                                                   
                                                 )

                                                 INSERT INTO [{0}].[dbo].[Subget] (Name, Expenses) VALUES('Budget', {1})", newDbName, BudgetValue);
                    using (SqlCommand command2 = new SqlCommand(tableCreate, dbConnection))
                    {
                        try
                        {
                            await command2.ExecuteNonQueryAsync();
                        }
                        catch (SqlException)
                        {
                            MessageBox.Show("Table creation error.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                catch (SqlException)
                {
                    MessageBox.Show("This Database already exists or is invalid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }


            await dbConnection.CloseAsync();

            await dbConnection.DisposeAsync();

            ConnectionString.Replace("master", newDbName);
            dbConnection = new SqlConnection(ConnectionString.ToString());
            await dbConnection.OpenAsync();

            InitializeDataGrid(dbConnection);
            MessageBox.Show("Database and Table successfully created!");

            //throw new NotImplementedException();
            //  dbConnection = new SqlConnection(ConnectionString.ToString());
            //
        }