コード例 #1
0
        private void upd_org_button_Click(object sender, EventArgs e)
        {
            string id      = orgId.Text;
            Regex  regexId = new Regex(@"^[1-9]\d*$");

            if (regexId.IsMatch(id))
            {
                string    name       = orgName.Text;
                string    searchId   = "Id = " + id;
                string    searchName = "Name = '" + name + "'";
                DataRow[] rowId      = virusesDBDataSet.Organism.Select(searchId);
                DataRow[] rowName    = virusesDBDataSet.Organism.Select(searchName);
                Regex     regexName  = new Regex(@"^[A-Z][a-z]+");

                if (rowId.Length == 1 && rowName.Length == 0 && regexName.IsMatch(name))
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    String         sql     = "UPDATE Organism SET Name = '" + name + "' Where Id = " + id;
                    adapter.UpdateCommand = new SqlCommand(sql, cnn);
                    adapter.UpdateCommand.ExecuteNonQuery();
                    this.organismTableAdapter.Fill(this.virusesDBDataSet.Organism);
                    OrganismsGrid.Refresh();
                    OrganismsGrid.Update();
                }
                else
                {
                    MessageBox.Show("Name is not correct or element with this ID or Name is not in table!");
                }
            }
            else
            {
                MessageBox.Show("Id is not correct!");
            }
        }
コード例 #2
0
        private void del_org_button_Click(object sender, EventArgs e)
        {
            string id      = orgId.Text;
            Regex  regexId = new Regex(@"^[1-9]\d*$");

            if (regexId.IsMatch(id))
            {
                string         searchId = "Id = " + id;
                DataRow[]      rowId    = virusesDBDataSet.Organism.Select(searchId);
                SqlDataAdapter adapter  = new SqlDataAdapter();
                String         vCheck   = "Select Id from Virus where OrganismId = '" + id + "'";
                adapter.SelectCommand = new SqlCommand(vCheck, cnn);
                Object check = adapter.SelectCommand.ExecuteScalar();
                if (rowId.Length == 1 && check == null)
                {
                    String sql = "DELETE from Organism Where Id = " + id;
                    adapter.DeleteCommand = new SqlCommand(sql, cnn);
                    adapter.DeleteCommand.ExecuteNonQuery();
                    this.organismTableAdapter.Fill(this.virusesDBDataSet.Organism);
                    OrganismsGrid.Refresh();
                    OrganismsGrid.Update();
                }
                else
                {
                    MessageBox.Show("Cannot delete this element!");
                }
            }
            else
            {
                MessageBox.Show("Id is not correct!");
            }
        }