예제 #1
0
        public string readFile(string file)
        {
            string fileText = "";

            try
            {
                fileText = File.ReadAllText(file);
                if (fileText.Equals(""))
                {
                    MyMessageBox.display("plik " + file + " jest pusty");
                    return("");
                }
            }
            catch (DirectoryNotFoundException exc)
            {
                MyMessageBox.display(exc.Message, MessageBoxType.Error);
                return("");
            }
            catch (FileNotFoundException exc)
            {
                MyMessageBox.display(exc.Message, MessageBoxType.Error);
                return("");
            }
            return(fileText);
        }
예제 #2
0
        private void PomocToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string pomoc = "(*) pierwsza kolumna wyników kwerendy MUSI zawierać ID (lub inny klucz główny)" +
                           "\r\nidentyfikujący jednoznacznie wiersz wyników kwerendy zasilającej datagrid";

            MyMessageBox.display(pomoc, MessageBoxType.Information);
        }
예제 #3
0
 private bool handleBit(object cellValue)
 {
     try
     {
         int value = int.Parse(cellValue.ToString());
         if (value == 1 || value == 0)
         {
             return(true);
         }
         else
         {
             MyMessageBox.display("\r\nNależy wprowadzić liczbę 0 lub 1", MessageBoxType.Error);
             return(false);
         }
     }
     catch (FormatException e)
     {
         MyMessageBox.display(e.Message + "\r\nNależy wprowadzić liczbę 0 lub 1", MessageBoxType.Error);
     }
     catch (OverflowException e)
     {
         MyMessageBox.display(e.Message + "\r\nNależy wprowadzić liczbę 0 lub 1", MessageBoxType.Error);
     }
     return(false);
 }
예제 #4
0
        //button, którego kliknięcie wypełnia danymi z kwerendy główny datagrid
        //jest to pierwszy przycisk, który użytkownik może nacisnąć po wpisaniu kwerendy w pole tekstowe
        private void displayButton_Click(object sender, EventArgs e)
        {
            //przekazuję kwerendę do DBConnectora w celu utworzenia połaczenia, wyciągam od razu nazwę bazy danych, jest potrzebna później

            if (configFileValidated)
            {
                sqlQuery = sqlQueryTextBox.Text;

                //sql nie widzi różnicy pomiędzy lower i upper case a ma to znaczenie przy wyszukiwaniu słow kluczowych w kwerendzie
                tableName    = connector.getTableName(sqlQuery);
                dbConnection = connector.getDBConnection(ConnectionSources.serverNameInFile, ConnectionTypes.sqlAuthorisation);

                if (dg1Handler.checkChangesExist())
                {
                    if (MyMessageBox.display("Czy zapisać zmiany?", MessageBoxType.YesNo) == MessageBoxResults.Yes)
                    {
                        //zaimplementować
                    }
                }
                else
                {
                    dg1Handler.Dispose();               //likwiduję starą instancję utworzoną w konstruktorze, bo jest to de facto wyświetlenie od zera i operacje na datagridzie od zera
                    dg1Handler = new DataGridHandler(); //każdy datagrid musi mieć swoją instancję DataGridHandlera
                    dataGridView1.Rows.Clear();
                    dataGridView1.Refresh();
                    datagridRowIndex       = 0;
                    loadNextButton.Visible = false;
                    setUpDatagrid();
                }
            }
        }
예제 #5
0
        //wyciąga nazwę db z kwerendy wpisanej przez użytkownika
        private string extractTableName()
        {
            string          tableName = "";
            TextManipulator tm        = new TextManipulator();
            //znajduję położenie wyrazu kluczowego "from" w kwerendzie
            List <int> keyWordFromPosition = tm.getSubstringStartPositions(sqlQuery, "from");

            try
            {
                //wywala bład gdy kwerenda jest na tyle bezsensowna, że nie potrafi wyłuskać sensownego wyrazu, który mógłby być nazwą bazy danych
                string textAfterFrom      = sqlQuery.Substring(keyWordFromPosition[0] + 5); //dodaję długość wyrazu from i jedną spację
                int    firstSpacePosition = textAfterFrom.IndexOf(" ");
                if (firstSpacePosition == -1)                                               //brak spacji
                {
                    tableName = textAfterFrom;
                }
                else
                {
                    tableName = textAfterFrom.Substring(0, firstSpacePosition);
                }
            }
            catch (System.ArgumentOutOfRangeException e)
            {
                MyMessageBox.display("Błąd w kwerendzie", MessageBoxType.Error);
                tableName = "";
            }
            return(restoreCase(tableName));  //powracam do oryginalnej pisowni, bo ma to znaczenie gdy collation bazy jest ustawiony na case-sensitive
        }
예제 #6
0
 private bool handleBigint(object cellValue)
 {
     try
     {
         long value = long.Parse(cellValue.ToString());
         return(true);
     }
     catch (FormatException e)
     {
         MyMessageBox.display(e.Message + "\r\nNależy wprowadzić liczbę całkowitą", MessageBoxType.Error);
     }
     return(false);
 }
예제 #7
0
 private bool handleDatetime(object cellValue)
 {
     try
     {
         DateTime value = DateTime.Parse(cellValue.ToString());
         return(true);
     }
     catch (FormatException e)
     {
         MyMessageBox.display(e.Message + "\r\nNależy wprowadzić datę", MessageBoxType.Error);
     }
     return(false);
 }
예제 #8
0
 private bool testConnection()
 {
     try
     {
         dbConnection.Open();
         dbConnection.Close();
         return(true);
     }
     catch (System.Data.SqlClient.SqlException e)
     {
         MyMessageBox.display(e.Message, MessageBoxType.Error);
     }
     return(false);
 }
예제 #9
0
 private bool handleInt(object cellValue)
 {
     try
     {
         int value = int.Parse(cellValue.ToString());    //zwykły parse zapisany jako  (int)cellValue  nie działa
         return(true);
     }
     catch (FormatException e)
     {
         MyMessageBox.display(e.Message + "\r\nNależy wprowadzić liczbę całkowitą", MessageBoxType.Error);
     }
     catch (OverflowException e)
     {
         MyMessageBox.display(e.Message + "\r\nNależy wprowadzić liczbę całkowitą od -32,768 do 32,767 ", MessageBoxType.Error);
     }
     return(false);
 }
예제 #10
0
 public bool assertFileExists(string file)
 {
     try
     {
         return(File.Exists(file));
     }
     catch (DirectoryNotFoundException exc)
     {
         MyMessageBox.display(exc.Message, MessageBoxType.Error);
         return(false);
     }
     catch (FileNotFoundException exc)
     {
         MyMessageBox.display(exc.Message, MessageBoxType.Error);
         return(false);
     }
 }
예제 #11
0
        //podana musi być pełna ścieżka do obu plików
        //jeżeli istnieje już plik docelowy, zostaje nadpisany
        //oryginał zostaje zachowany

        public bool fileCopyAs(string file1, string file2)
        {
            if (File.Exists(file1))
            {
                if (File.Exists(file2))
                {
                    File.Delete(file2);
                }
                File.Copy(file1, file2);
                return(true);
            }
            else
            {
                MyMessageBox.display("brak pliku " + file1, MessageBoxType.Error);
                return(false);
            }
        }
예제 #12
0
        private bool handleDouble(object cellValue)
        {
            string stringCellValue = cellValue.ToString();

            try
            {
                double value = Double.Parse(stringCellValue);
                return(true);
            }
            catch (InvalidCastException e)
            {
                MyMessageBox.display(e.Message + "\r\nNależy wprowadzić liczbę", MessageBoxType.Error);
            }
            catch (FormatException ex)
            {
                MyMessageBox.display(ex.Message + "\r\nZnak separatora dziesiętnego musi być taki jaki jest ustawiony w systemie");
            }
            return(false);
        }
예제 #13
0
 public void runProgram(string program)
 {
     try
     {
         System.Diagnostics.Process.Start(program);
     }
     catch (System.ComponentModel.Win32Exception exc)
     {
         //displayMessage(exc.Message);
     }
     catch (DirectoryNotFoundException exc)
     {
         MyMessageBox.display(exc.Message, MessageBoxType.Error);
     }
     catch (FileNotFoundException exc)
     {
         MyMessageBox.display(exc.Message, MessageBoxType.Error);
     }
 }
예제 #14
0
        public void writeToDB(List <string> queries)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();

            dbConnection.Open();
            foreach (string query in queries)
            {
                try
                {
                    SqlCommand command = new SqlCommand(query, dbConnection);
                    adapter.InsertCommand = command;
                    adapter.InsertCommand.ExecuteNonQuery();
                    command.Dispose();
                }
                catch (System.Data.SqlClient.SqlException e)
                {
                    MyMessageBox.display(e.Message, MessageBoxType.Error);
                }
            }
            dbConnection.Close();
        }
예제 #15
0
 public string saveTextToFile(string file, string text)
 {
     try
     {
         if (File.Exists(file))
         {
             File.WriteAllText(file, text);
             return("zmieniono plik konfiguracyjny " + file + "\r\n");
         }
         else
         {
             MyMessageBox.display("plik " + file + " nie został znaleziony", MessageBoxType.Warning);
             return("plik " + file + " nie został znaleziony\r\n");
         }
     }
     catch (DirectoryNotFoundException exc)
     {
         MyMessageBox.display(exc.Message, MessageBoxType.Error);
         return("plik " + file + " nie został znaleziony\r\n");
     }
 }
예제 #16
0
        public bool verifyCellDataType(ref DataGridCell cell)
        {
            // rozważam bazodanowe typy danych:  bit, int, bigint, oraz  w grupach: (char, varchar), (float, decimal, numeric), (datetime), (geometry)
            string typeName  = cell.DataTypeName;
            object cellValue = cell.getCellValue(cellValueTypes.newValue);

            if (cellValue != null)
            {
                if (typeName.Contains("bigint"))
                {
                    return(handleBigint(cellValue));
                }
                else if (typeName.Contains("bit"))
                {
                    return(handleBit(cellValue));
                }
                else if (typeName.Contains("int"))
                {
                    return(handleInt(cellValue));
                }
                else if (typeName.Contains("char"))
                {
                    return(true);
                }
                else if (typeName.Contains("float") || typeName.Contains("decimal") || typeName.Contains("numeric"))
                {
                    return(handleDouble(cellValue));
                }
                else if (typeName.Contains("datetime"))
                {
                    return(handleDatetime(cellValue));
                }
                else if (typeName.Contains("geometry"))
                {
                    MyMessageBox.display("Nie można edytować pól typu geometry", MessageBoxType.Error);
                }
                return(false);
            }
            return(true);
        }
예제 #17
0
        public QueryData readFromDB(string sqlQuery)
        {
            queryData = new QueryData();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sqlQuery, dbConnection);
                dbConnection.Open();
                SqlDataReader sqlReader = sqlCommand.ExecuteReader();

                int numberOfColumns = sqlReader.FieldCount;

                while (sqlReader.Read())
                {
                    object[] rowData = new object[numberOfColumns];
                    for (int i = 0; i < numberOfColumns; i++)
                    {
                        rowData[i] = sqlReader.GetValue(i).ToString();
                    }
                    queryData.addQueryData(rowData);
                }

                for (int i = 0; i < sqlReader.FieldCount; i++)
                {
                    queryData.addHeader(sqlReader.GetName(i));
                    queryData.addDataType(sqlReader.GetDataTypeName(i));
                }
                sqlReader.Close();
                sqlCommand.Dispose();
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                MyMessageBox.display(e.Message + "\r\n" + dbConnection.ConnectionString, MessageBoxType.Error);
            }

            dbConnection.Close();
            return(queryData);
        }
예제 #18
0
        public bool validateConfigFile()
        {
            string currentPath = Application.StartupPath;       //katalog z którego uruchamiany jest program

            if (ProgramSettings.configFilePath.Equals(""))      //nie zdefiniowano alternatywnej ścieżki dla pliku konfiguracyjnego
            {
                configFilePath = currentPath;                   //plik konfiguracyjny jest w tym samym katalogu co program
            }
            else
            {
                configFilePath = ProgramSettings.configFilePath;
            }

            FileManipulator fm         = new FileManipulator();
            string          configFile = configFilePath + @"\" + ProgramSettings.configFileName;

            configFileText = fm.readFile(configFile);
            if (!configFileText.Equals(""))                     //plik konfiguracyjny istnieje i nie jest pusty
            {
                TextManipulator tm      = new TextManipulator();
                List <int>      indexes = tm.getSubstringStartPositions(configFileText, ProgramSettings.connectionStringDelimiter);

                //jeżeli w pliku jest błąd i jest za dużo lub za mało znaczników
                if (indexes.Count != 2)
                {
                    MyMessageBox.display("błąd pliku konfiguracyjnego " + configFile + " dla znacznika " + ProgramSettings.connectionStringDelimiter, MessageBoxType.Error);
                    configFileValidated = false;
                }
            }
            else
            {
                configFileValidated = false;       //plik jest pusty lub go nie ma
            }
            configFileValidationWasDone = true;
            return(configFileValidated);             //domyślnie jest true
        }