コード例 #1
0
ファイル: MainWindow.cs プロジェクト: kdator/DatabaseApp
        private void requestsButton_Click(object sender, EventArgs e)
        {
            Database db = new Database();

            db.OpenConnection();
            try
            {
                var       requests = new List <String>();
                DataTable dt       = db.GetConnection().GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "VIEW" });
                foreach (DataRow row in dt.Rows)
                {
                    requests.Add(row["TABLE_NAME"].ToString());
                }

                RequestsCall requestsWindow = new RequestsCall(requests);
                DialogResult res            = requestsWindow.ShowDialog();
                if (res != DialogResult.OK)
                {
                    return;
                }
                String selectedReq = requestsWindow.GetSelected();

                RequestsResult result = new RequestsResult(selectedReq, LoadTable(db, selectedReq));
                result.Show();
            }
            finally
            {
                db.CloseConnection();
            }
        }
コード例 #2
0
ファイル: MainWindow.cs プロジェクト: kdator/DatabaseApp
        private void proceduresButton_Click(object sender, EventArgs e)
        {
            Database db = new Database();
            Dictionary <String, String> proceduresRepresentation = new Dictionary <String, String>();

            db.OpenConnection();
            try
            {
                DataTable dt = db.GetConnection().GetOleDbSchemaTable(OleDbSchemaGuid.Procedures, null);
                foreach (DataRow row in dt.Rows)
                {
                    if (row["PROCEDURE_NAME"].ToString().IndexOf("~") == 0)
                    {
                        continue;
                    }
                    proceduresRepresentation.Add(row["PROCEDURE_NAME"].ToString(), row["PROCEDURE_DEFINITION"].ToString());
                }
                RequestsCall requestsWindow = new RequestsCall(new List <String>(proceduresRepresentation.Keys));
                DialogResult res            = requestsWindow.ShowDialog();
                if (res != DialogResult.OK)
                {
                    return;
                }
                String selectedReq = requestsWindow.GetSelected();

                String query = "EXEC [" + selectedReq + "] ";

                OleDbCommand command = db.GetConnection().CreateCommand();
                int          i       = 0; // нужен будет для порядка входящих параметров.
                foreach (String param in
                         getAllParametersFromProcedure(proceduresRepresentation[selectedReq]))
                {
                    if (!tablesColumns.Contains(param))
                    {
                        InputParam inputParam = new InputParam(param);
                        inputParam.ShowDialog();
                        String data = inputParam.GetInputData();
                        query += $" \'@param{i}'";
                        command.Parameters.AddWithValue($"@param{i}", data);
                        if (inputParam.DialogResult != DialogResult.OK)
                        {
                            return;
                        }
                        i++;
                    }
                }
                command.CommandText = query.Substring(0, query.Length - 1);

                OleDbDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    DataTable schemaTable = reader.GetSchemaTable();
                    DataTable data        = new DataTable();
                    foreach (DataRow row in schemaTable.Rows)
                    {
                        string colName = row.Field <string>("ColumnName");
                        Type   t       = row.Field <Type>("DataType");
                        data.Columns.Add(colName, t);
                    }
                    while (reader.Read())
                    {
                        var newRow = data.Rows.Add();
                        foreach (DataColumn col in data.Columns)
                        {
                            newRow[col.ColumnName] = reader[col.ColumnName];
                        }
                    }
                    RequestsResult requestResult = new RequestsResult(selectedReq, data);
                    requestResult.Show();
                }
                UpdateTables(db);
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message, "Warning",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning,
                                MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
            finally
            {
                db.CloseConnection();
            }
        }