private void metroButtonCreateTable_Click(object sender, EventArgs e)
        {
            myDB = dbe.OpenDatabase(saveFileDialog1.FileName);

            if (metroTextBoxTableName.Text == "")
            {
                MessageBox.Show("You must give a name to your Table", "Important Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                myTB            = myDB.CreateTableDef(metroTextBoxTableName.Text);
                myFL            = myTB.CreateField("Ref" + metroTextBoxTableName.Text, DAO.DataTypeEnum.dbLong);
                myFL.Attributes = (int)DAO.FieldAttributeEnum.dbAutoIncrField;
                myTB.Fields.Append(myFL);
                myInd = myTB.CreateIndex("primaryKey");
                myFL  = myInd.CreateField("Ref" + metroTextBoxTableName.Text);
                ((IndexFields)(myInd.Fields)).Append(myFL);
                myInd.Primary = true;
                myTB.Indexes.Append(myInd);
                int n = metroGrid1.Rows.Add();
                metroGrid1.Rows[n].Cells[0].Value = "Ref" + metroTextBoxTableName.Text;
                metroGrid1.Rows[n].Cells[1].Value = "AutoNumber";
                metroButtonCreateTable.Enabled    = false;
                metroButtonRelation.Enabled       = false;
            }
        }
Пример #2
0
 private void bttnSaveTable_Click(object sender, EventArgs e)
 {
     if (txtTables.Text == "")
     {
     }
     else
     {
         myDB            = dbe.OpenDatabase(svflDialog.FileName);
         myTB            = myDB.CreateTableDef(txtTables.Text.ToString());
         myFL            = myTB.CreateField("Ref" + txtTables.Text, DAO.DataTypeEnum.dbLong);
         myFL.Attributes = (int)DAO.FieldAttributeEnum.dbAutoIncrField;
         myTB.Fields.Append(myFL);
         Index myInd = myTB.CreateIndex("primaryKey");
         myFL = myInd.CreateField("Ref" + txtTables.Text);
         ((IndexFields)(myInd.Fields)).Append(myFL);
         myInd.Primary = true;
         myTB.Indexes.Append(myInd);
         myDB.TableDefs.Append(myTB);
         MessageBox.Show(myTB.Name + " was created with success.", "Important Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
         this.Height = 280;
         txtTables.Clear();
         txtTables.Enabled     = false;
         bttnSaveTable.Enabled = false;
         txtfields.Focus();
         txtfields.Clear();
     }
 }
Пример #3
0
        private TableDef CreateFields()
        {
            Field MyFL;
            if (CHKPrimaryKey.Checked && !CHKAutoIncrement.Checked)
            {
                MyFL = MyTb.CreateField(txtFieldsName.Text, CBOType.SelectedItem, txtLength.Text);
                MyTb.Fields.Append(MyFL);
                Index MyInd = MyTb.CreateIndex("PK" + txtFieldsName.Text);
                MyFL = MyInd.CreateField(txtFieldsName.Text);
                ((IndexFields)(MyInd.Fields)).Append(MyFL);
                MyInd.Primary = true;
                MyTb.Indexes.Append(MyInd);
                MessageBox.Show("The field & the index have been created !", "Successful Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (CHKPrimaryKey.Checked && CHKAutoIncrement.Checked)
            {
                MyFL = MyTb.CreateField(txtFieldsName.Text, FieldAttributeEnum.dbAutoIncrField);
                MyTb.Fields.Append(MyFL);
                Index MyInd = MyTb.CreateIndex("PK" + txtFieldsName.Text);
                MyFL = MyInd.CreateField(txtFieldsName.Text);
                ((IndexFields)(MyInd.Fields)).Append(MyFL);
                MyInd.Primary = true;
                MyTb.Indexes.Append(MyInd);
                MessageBox.Show("The field & the index have been created !", "Successful Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (CHKAutoIncrement.Checked && !CHKPrimaryKey.Checked)
            {
                MyFL = MyTb.CreateField(txtFieldsName.Text, FieldAttributeEnum.dbAutoIncrField);
                MyTb.Fields.Append(MyFL);
                MessageBox.Show("The field has been created !", "Successful Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MyFL = MyTb.CreateField(txtFieldsName.Text, CBOType.SelectedItem, txtLength.Text);
                MyTb.Fields.Append(MyFL);
                MessageBox.Show("The field has been created !", "Successful Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


            return MyTb;
        }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            myInd = myTB.CreateIndex("PrimaryKey");
            myFL  = myInd.CreateField(lvFieldList.SelectedItems[0].SubItems[0].Text);
            ((IndexFields)(myInd.Fields)).Append(myFL);
            myInd.Primary = true;
            myTB.Indexes.Append(myInd);
            MessageBox.Show("Primary key added successfully!", "Add PK", MessageBoxButtons.OK, MessageBoxIcon.Information);
            lvFieldList.SelectedItems[0].SubItems[0].ForeColor = Color.Red;
            ButtonControl(false, false, true, false, true);

            isPK = true;
        }
Пример #5
0
        private void btn_createIndex_Click(object sender, EventArgs e)
        {
            if (!validateFormFields())
            {
                return;
            }

            try
            {
                // Retrieving the table.
                string tableName = cbo_table.Text;
                myTb = clsGlobal.myDB.TableDefs[tableName];

                // Retrieving the name of the new index.
                string indexName = txt_indexName.Text.Trim();

                // Creating the new index.
                Index idx = myTb.CreateIndex(indexName);

                // Adding the chosen fields to the new index.
                foreach (object itemChecked in chklst_fields.CheckedItems)
                {
                    string fieldName = itemChecked.ToString();
                    Field  fld       = idx.CreateField(fieldName);
                    ((IndexFields)idx.Fields).Append(fld);
                }

                // Setting the properties of the new index.
                idx.Unique      = chk_unique.Checked;
                idx.Primary     = chk_primary.Checked;
                idx.IgnoreNulls = chk_ignoreNulls.Checked;

                // Appending the new index to the table.
                myTb.Indexes.Append(idx);

                loadTableIndexes();
                clearFormFields();
            }
            catch (Exception exc)
            {
                string message = "The following error occurred while creating the new index:\n\n";
                message += exc.Message;

                MessageBox.Show(message, this.MdiParent.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Пример #6
0
        private void tleIndex_Click(object sender, EventArgs e)
        {
            //Loop to check if name is alrady in use

            for (int i = 0; i < myTB.Indexes.Count; i++)
            {
                if (txtIndex.Text == myTB.Indexes[i].Name.ToString())
                {
                    MetroMessageBox.Show(this, " Index name already in use, please enter a new one!", "Create Index", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtIndex.Clear();
                    txtIndex.Focus();
                    return;
                }
            }

            //Creating index
            myInd = myTB.CreateIndex(txtIndex.Text);
            myFL  = myInd.CreateField(txtSelIndex.Text);
            ((IndexFields)myInd.Fields).Append(myFL);
            myInd.Primary = tglPrim.Checked;

            //Create icon to be displayed in case index is primary key
            ImageList ilist  = new ImageList();
            Image     pkIcon = Image.FromFile("data/Key_10px.png");

            ilist.Images.Add(pkIcon);
            lvwTable.SmallImageList = ilist;

            ListViewItem indListItem = new ListViewItem(txtIndex.Text);

            indListItem.SubItems.Add(txtSelIndex.Text);

            //If is primary, changes the other index to keep only one primary key

            if (myInd.Primary == true)
            {
                for (int i = 0; i < myTB.Indexes.Count; i++)
                {
                    if (myTB.Indexes[i].Name != myInd.Name)
                    {
                        myTB.Indexes[i].Primary = false;
                    }
                }
                foreach (ListViewItem fieldItem in lvwTable.Items)
                {
                    fieldItem.ImageIndex = -1;
                }
                foreach (ListViewItem indexItem in lvwIndex.Items)
                {
                    indexItem.SubItems[2].Text = "No";
                }
                lvwTable.SelectedItems[0].ImageIndex = 0;
                indListItem.SubItems.Add("Yes");
            }
            else
            {
                indListItem.SubItems.Add("No");
            }
            lvwIndex.Items.Add(indListItem);
            myTB.Indexes.Append(myInd);
            clearAftCreate();
        }
Пример #7
0
        private void btnCreateTables_Click(object sender, EventArgs e)
        {
            TableDef     mytdef = null;
            Field        myfield = null, indexfield;
            Index        myInd = null;
            int          j     = 0;
            List <Field> flist = new List <Field>();

            try
            {
                // Delete all the tables from DB to avoid Table's conflicts
                //DeleteAllTables(mydb);

                foreach (TabPage tbtemp in tabcontFields.TabPages)
                {
                    // check if Table exist in DB
                    if (tableExist(mydb, tbtemp.Text) == false)
                    {
                        ////////////////////////////
                        // Table and Fields Creation
                        ////////////////////////////

                        mytdef = mydb.CreateTableDef(tbtemp.Text);

                        for (int i = 0; i < dgridList[j].RowCount - 1; i++)
                        {
                            myfield = mytdef.CreateField(dgridList[j].Rows[i].Cells["colField"].Value, (DataTypeEnum)Enum.Parse(typeof(DataTypeEnum), dgridList[j].Rows[i].Cells["colDataType"].Value.ToString()));

                            if (i == 0)
                            {
                                // Add autoincrement attribute to first field "ID"
                                myfield.Attributes = (Int32)DAO.FieldAttributeEnum.dbAutoIncrField;
                            }

                            // Add Field to TableDef
                            mytdef.Fields.Append(myfield);
                            flist.Add(myfield);
                        }

                        /////////////////////////////////////
                        ////Create an Index and Primary Key
                        /////////////////////////////////////
                        myInd         = mytdef.CreateIndex(dgridList[j].Rows[0].Cells["colField"].Value.ToString());
                        myInd.Primary = true;

                        //////////////////////////////////
                        // Add field ID to index as ass Primary Key by default
                        //Creation of indexed fields
                        indexfield = myInd.CreateField(dgridList[j].Rows[0].Cells["colField"].Value);
                        //Add field in index
                        ((IndexFields)(myInd.Fields)).Append(indexfield);

                        // Creation of associated PK and indexes defined by the user
                        for (int i = 1; i < dgridList[j].RowCount - 1; i++)
                        {
                            // Verify if PK check Box is checked to create an index and Primary Key
                            if ((string)(dgridList[j].Rows[i].Cells["Key"]).Value == "true")
                            {
                                //Creation of indexed fields
                                indexfield = myInd.CreateField(dgridList[j].Rows[i].Cells["colField"].Value);

                                //Add field in index
                                ((IndexFields)(myInd.Fields)).Append(indexfield);
                            }
                        }
                        //Add index in the table
                        mytdef.Indexes.Append(myInd);

                        //Add table into DB
                        mydb.TableDefs.Append(mytdef);


                        // Add the Field's description to each field adding a new property to each cell
                        Property prt = null;
                        int      rindex;
                        rindex = 0;
                        foreach (Field ftemp in flist)
                        {
                            if (dgridList[j].Rows[rindex].Cells["colDescription"].Value != null)
                            {
                                //Cretion of new porperty called Description in each Cell
                                prt = ftemp.CreateProperty("Description", DataTypeEnum.dbText, dgridList[j].Rows[rindex].Cells["colDescription"].Value);
                                ftemp.Properties.Append(prt);
                            }
                            rindex++;
                        }

                        flist.Clear();
                        j++;
                    }
                    else
                    {
                        j++;
                    }
                }
                MessageBox.Show("Tables have been Created in DB " + mydb.Name, "Tables Created", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnGoRelations.Show();
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("All fields must have a Data type selected in the table " + mytdef.Name, "Required Fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Runtime.InteropServices.COMException daoErr)
            {
                if (daoErr.ErrorCode == FieldExistErrorCode) // Field exist Error Code
                {
                    MessageBox.Show("There are repeated fields in table " + mytdef.Name + "\nField's name must be unique", "Repeated Field", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (daoErr.ErrorCode == FieldUnamedErrorCode) // Field unamed Error Code
                {
                    MessageBox.Show("All Fields must have a name in table " + mytdef.Name, "Unnamed Field", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else // Other Errors related to Interpot Services
                {
                    MessageBox.Show(daoErr.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)  // General Exceptions
            {
                MessageBox.Show(ex.ToString(), "Table Creation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #8
0
        private void btnCreate_Click(object sender, EventArgs e) //triggers the creation of a new field
        {
            bool error = false;

            foreach (TableDef oneTable in clsDataStorage.db.TableDefs) //loops through the tabledefinitions
            {
                if (oneTable.Attributes == 0)
                {
                    foreach (Field abcd in oneTable.Fields)
                    {
                        if (abcd.Name == txtFieldName.Text) //check if table already contains a field with the same name
                        {
                            MetroMessageBox.Show(this, "This table already contains a field with the name " + abcd.Name + ".\nField names have to be unique!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            error = true;
                        }
                    }
                    if (cmbFieldProperty.Text == "Primary" || cmbFieldProperty.Text == "Unique") //check if table contains primary or unique elements
                    {
                        foreach (Index inx in oneTable.Indexes)
                        {
                            if (inx.Primary == true && !error)
                            {
                                MetroMessageBox.Show(this, "This table already contains a field with the property INDEX.\nA table can only contain one index.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                error = true;
                                break;
                            }
                            if (inx.Unique == true && !error)
                            {
                                MetroMessageBox.Show(this, "This table already contains a field with the property UNIQUE.\nA table can only contain one UNIQUE.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                error = true;
                                break;
                            }
                        }
                    }
                }
            }
            if (!error) //if no primary or unique, then create a new field
            {
                table = clsDataStorage.db.TableDefs[cmbListTable.Text];
                string name;
                name = txtFieldName.Text;
                int   size;
                Field field = new Field();
                //creates the field according to the type chosen by the user
                if (cmbFieldType.Text == "Long")
                {
                    size  = Convert.ToInt32(txtFieldSize.Text);
                    field = table.CreateField(name, DAO.DataTypeEnum.dbLong, size);
                    if (chkAuto.Checked)
                    {
                        field.Attributes = (int)DAO.FieldAttributeEnum.dbAutoIncrField;
                    }
                }

                else if (cmbFieldType.Text == "Double")
                {
                    size  = Convert.ToInt32(txtFieldSize.Text);
                    field = table.CreateField(name, DAO.DataTypeEnum.dbDouble, size);
                }

                else if (cmbFieldType.Text == "Text")
                {
                    size  = Convert.ToInt32(txtFieldSize.Text);
                    field = table.CreateField(name, DAO.DataTypeEnum.dbText, size);
                }
                else if (cmbFieldType.Text == "Currency")
                {
                    size  = Convert.ToInt32(txtFieldSize.Text);
                    field = table.CreateField(name, DAO.DataTypeEnum.dbCurrency, size);
                }
                else if (cmbFieldType.Text == "Boolean")
                {
                    size  = Convert.ToInt32(txtFieldSize.Text);
                    field = table.CreateField(name, DAO.DataTypeEnum.dbBoolean, size);
                }
                else
                {
                    field = table.CreateField(name, DAO.DataTypeEnum.dbDate);
                }

                table.Fields.Append(field);             //appends the field to the table

                if (cmbFieldProperty.Text == "Primary") //check if field is primary, then append the property
                {
                    Index index = table.CreateIndex("pk_" + name);
                    field             = index.CreateField(name);
                    index.Primary     = true;
                    index.Required    = true;
                    index.IgnoreNulls = false;
                    ((IndexFields)index.Fields).Append(field);
                    table.Indexes.Append(index);
                }

                else if (cmbFieldProperty.Text == "Unique") //check if field is unique, then append the property
                {
                    Index index = table.CreateIndex("unq_" + name);
                    field             = index.CreateField(name);
                    index.Required    = true;
                    index.Unique      = true;
                    index.IgnoreNulls = false;
                    ((IndexFields)index.Fields).Append(field);
                    table.Indexes.Append(index);
                }
                else if (cmbFieldProperty.Text == "Index") //check if field is index, then append the property
                {
                    Index index = table.CreateIndex("idx_" + name);
                    field             = index.CreateField(name);
                    index.Required    = true;
                    index.Unique      = true;
                    index.IgnoreNulls = false;
                    ((IndexFields)index.Fields).Append(field);
                    table.Indexes.Append(index);
                }
                MetroMessageBox.Show(this, "The new field was included successfully.", "New field", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close(); //after the field being added, closes the form
            }
        }
Пример #9
0
        private void createField(DataGridView dgv, int indexRow) //function to create a new field
        {
            string name;

            name = dgv.Rows[indexRow].Cells["clmName"].Value.ToString();
            int   size;
            Field field = new Field();

            //creates the field according to the type chosen by the user
            if (dgv.Rows[indexRow].Cells["clmType"].Value.ToString() == "Long" && autoIncr < 2)
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["clmSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbLong, size);
                if (dgv.Rows[indexRow].Cells["clmAuto"].Value != null && dgv.Rows[indexRow].Cells["clmAuto"].Value.ToString() == "True")
                {
                    field.Attributes = (int)DAO.FieldAttributeEnum.dbAutoIncrField;
                }
            }

            else if (dgv.Rows[indexRow].Cells["clmType"].Value.ToString() == "Double")
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["clmSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbDouble, size);
            }

            else if (dgv.Rows[indexRow].Cells["clmType"].Value.ToString() == "Text")
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["clmSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbText, size);
            }
            else if (dgv.Rows[indexRow].Cells["clmType"].Value.ToString() == "Currency")
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["clmSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbCurrency, size);
            }
            else if (dgv.Rows[indexRow].Cells["clmType"].Value.ToString() == "Boolean")
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["clmSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbBoolean, size);
            }
            else
            {
                field = table.CreateField(name, DAO.DataTypeEnum.dbDate);
            }

            table.Fields.Append(field); //appends the field to the table

            if (dgv.Rows[indexRow].Cells["clmFieldProperty"].Value != null &&
                dgv.Rows[indexRow].Cells["clmFieldProperty"].Value.ToString() == "Primary")
            //check if field is primary, then append the property
            {
                pricount++;
                Index index = table.CreateIndex("pk_" + name);
                field             = index.CreateField(name);
                index.Primary     = true;
                index.Required    = true;
                index.IgnoreNulls = false;
                ((IndexFields)index.Fields).Append(field);
                table.Indexes.Append(index);
            }

            else if (dgv.Rows[indexRow].Cells["clmFieldProperty"].Value != null &&
                     dgv.Rows[indexRow].Cells["clmFieldProperty"].Value.ToString() == "Unique")
            //check if field is unique, then append the property
            {
                unqcount++;
                Index index = table.CreateIndex("unq_" + name);
                field             = index.CreateField(name);
                index.Required    = true;
                index.Unique      = true;
                index.IgnoreNulls = false;
                ((IndexFields)index.Fields).Append(field);
                table.Indexes.Append(index);
            }
            else if (dgv.Rows[indexRow].Cells["clmFieldProperty"].Value != null &&
                     dgv.Rows[indexRow].Cells["clmFieldProperty"].Value.ToString() == "Index")
            //check if field is index, then append the property
            {
                Index index = table.CreateIndex("idx_" + name);
                field             = index.CreateField(name);
                index.Required    = true;
                index.Unique      = true;
                index.IgnoreNulls = false;
                ((IndexFields)index.Fields).Append(field);
                table.Indexes.Append(index);
            }
        }
        private void createField(DataGridView dgv, int indexRow)
        {
            string name;

            name = dgv.Rows[indexRow].Cells["ColumnTableName"].Value.ToString();
            int   size;
            Field field = new Field();

            if (dgv.Rows[indexRow].Cells["ColumnType"].Value.ToString() == "LONG")
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["ColumnSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbLong, size);
                if (dgv.Rows[indexRow].Cells["ColumnAutoIncr"].Value != null && dgv.Rows[indexRow].Cells["ColumnAutoIncr"].Value.ToString() == "T")
                {
                    field.Attributes = (int)DAO.FieldAttributeEnum.dbAutoIncrField;
                }
            }

            else if (dgv.Rows[indexRow].Cells["ColumnType"].Value.ToString() == "DOUBLE")
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["ColumnSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbDouble, size);
            }

            else if (dgv.Rows[indexRow].Cells["ColumnType"].Value.ToString() == "TEXT")
            {
                size  = Convert.ToInt16(dgv.Rows[indexRow].Cells["ColumnSize"].Value.ToString());
                field = table.CreateField(name, DAO.DataTypeEnum.dbText, size);
            }
            else
            {
                field = table.CreateField(name, DAO.DataTypeEnum.dbDate);
            }

            table.Fields.Append(field);

            if (dgv.Rows[indexRow].Cells["ColumnIndex"].Value != null && dgv.Rows[indexRow].Cells["ColumnIndex"].Value.ToString() == "PRIMARY")
            {
                Index index = table.CreateIndex("Primary key" + name);
                field             = index.CreateField(name);
                index.Primary     = true;
                index.Required    = true;
                index.IgnoreNulls = false;
                ((IndexFields)index.Fields).Append(field);
                table.Indexes.Append(index);
            }

            else if (dgv.Rows[indexRow].Cells["ColumnIndex"].Value != null && dgv.Rows[indexRow].Cells["ColumnIndex"].Value.ToString() == "UNIQUE")
            {
                Index index = table.CreateIndex("Unique");
                field             = index.CreateField(name);
                index.Required    = true;
                index.Unique      = true;
                index.IgnoreNulls = false;
                ((IndexFields)index.Fields).Append(field);
                table.Indexes.Append(index);
            }
            else if (dgv.Rows[indexRow].Cells["ColumnIndex"].Value != null && dgv.Rows[indexRow].Cells["ColumnIndex"].Value.ToString() == "INDEX")
            {
                Index index = table.CreateIndex("Index" + name);
                field             = index.CreateField(name);
                index.Required    = true;
                index.Unique      = true;
                index.IgnoreNulls = false;
                ((IndexFields)index.Fields).Append(field);
                table.Indexes.Append(index);
            }
        }
Пример #11
0
        private bool createTblField(TableField newFld) //procedure of creating a field and adding it to the table
        {
            DataTypeEnum fldTypeEnum;
            Field        myFld;

            string fldName    = newFld.FieldName;
            string fldType    = newFld.FieldType;
            int    fldLenNum  = newFld.FieldLength;
            bool   isPkeyFld  = newFld.IsPkey;
            bool   isAutoIncr = newFld.IsAutoIncr;

            //create fields:
            switch (fldType)
            {
            case "dbInt":
                fldTypeEnum = DataTypeEnum.dbInteger;
                break;

            case "dbLong":
                fldTypeEnum = DataTypeEnum.dbLong;
                break;

            case "dbText":
                fldTypeEnum = DataTypeEnum.dbText;
                break;

            case "dbBoolean":
                fldTypeEnum = DataTypeEnum.dbBoolean;
                break;

            case "dbDate":
                fldTypeEnum = DataTypeEnum.dbDate;
                break;

            default:
                MessageBox.Show("Unknown error during type defenition! \nDefault type will be TEXT!", "Error");
                fldTypeEnum = DataTypeEnum.dbText;
                break;
            }

            //creating a field:
            if (fldTypeEnum == DataTypeEnum.dbText)
            {
                //text field - length set to 50 by default
                //fldLenNum = (fldLengthStr != "") ? Convert.ToInt32(fldLengthStr) : 50;
                myFld = myTb.CreateField(fldName, DAO.DataTypeEnum.dbText, fldLenNum);
            }
            else
            {
                //numeric data field - default length =1 but it is not used anyway
                //fldLenNum = (fldLengthStr != "") ? Convert.ToInt32(fldLengthStr) : 1;
                myFld = myTb.CreateField(fldName, fldTypeEnum);
                if (isAutoIncr)
                {
                    //Auto Increment numeric field if selected checkbox
                    if ((fldTypeEnum == DataTypeEnum.dbInteger) || (fldTypeEnum == DataTypeEnum.dbLong))
                    {
                        myFld.Attributes = (int)DAO.FieldAttributeEnum.dbAutoIncrField;
                    }
                }
            }
            //lets try to append a field to the table:
            try
            {
                myTb.Fields.Append(myFld);
                if (isPkeyFld)
                {
                    //create the indexes
                    Index myInd = myTb.CreateIndex("PrimaryKey");
                    myFld = myInd.CreateField(fldName);
                    ((IndexFields)(myInd.Fields)).Append(myFld);
                    myInd.Primary = true;
                    myTb.Indexes.Append(myInd);
                }
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not create field: " + newFld.FieldName + "\n" + ex.Message,
                                "Error while creating new field", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
        }
Пример #12
0
        private void save_new_field()
        {
            ListViewItem to_add;
            string       datatype = "";
            string       index    = "";

            nf = null;
            MessageBox.Show(fieldtype + " is the datatype");
            switch (fieldtype)
            {
            case 0:
                myFL     = myTB.CreateField(Field_Name, DataTypeEnum.dbText, 100);
                datatype = "Text";
                myTB.Fields.Append(myFL);
                break;

            case 1:
                myFL     = myTB.CreateField(Field_Name, DataTypeEnum.dbLong);
                datatype = "Number";
                myTB.Fields.Append(myFL);
                break;

            case 2:
                myFL     = myTB.CreateField(Field_Name, DataTypeEnum.dbDate);
                datatype = "DateTime";
                myTB.Fields.Append(myFL);
                break;

            case 3:
                myFL     = myTB.CreateField(Field_Name, DataTypeEnum.dbCurrency);
                datatype = "Currency";
                myTB.Fields.Append(myFL);
                break;

            case 4:
                myFL     = myTB.CreateField(Field_Name, DataTypeEnum.dbBoolean);
                datatype = "YesNo";
                myTB.Fields.Append(myFL);
                break;

            case 5:
                myFL     = myTB.CreateField(Field_Name, DataTypeEnum.dbLong);
                datatype = "AutoNumber";
                myTB.Fields.Append(myFL);
                myFL.Attributes = (int)FieldAttributeEnum.dbAutoIncrField;
                break;
            }
            if (!primary)
            {
                if (MessageBox.Show("Is this primary key?", "Index", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Index myInd = myTB.CreateIndex("PrimaryKey");
                    myFL = myInd.CreateField(Field_Name);
                    ((IndexFields)myInd.Fields).Append(myFL);
                    myInd.Primary = true;
                    myTB.Indexes.Append(myInd);
                    index   = "Primary";
                    primary = true;
                }
            }
            to_add = new ListViewItem(index);
            to_add.SubItems.Add(Field_Name);
            to_add.SubItems.Add(datatype);
            lstview_current_table.Items.Add(to_add);
        }