I have a form like that and I can create tables easily and intuitively. To alter a table is much more difficult. I have done another constructor to the form that takes the syntax of table to alter (from show create table table_name_to_alter
) From constructor I call a function that should fill all datagrid fields:
private void load_table_fields_into_datagrid(string table_syntax)
{
/*
CREATE TABLE `città` (
`ID_CITTA` int(11) NOT NULL,
`Città` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ID_CITTA`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
I have to decompress that syntax and insert into data grid view
*/
string[] app = table_syntax.Split('\n');
table_name_textbox.Text = app[0].Split(new char[] { '`', '`' })[1];
for (int i = 1; app[i][2]=='`' ; i++)
{
if (app[i].Contains("NOT NULL"))
{
dataGridView1.Rows[i - 1].Cells[4].Value = true;
app[i] = app[i].Replace("NOT NULL", "");
}
if (app[i].Contains("PRIMARY KEY"))
{
dataGridView1.Rows[i - 1].Cells[2].Value = true;
app[i] = app[i].Replace("PRIMARY KEY", "");
}
if (app[i].Contains("UNIQUE"))
{
dataGridView1.Rows[i - 1].Cells[3].Value = true;
app[i] = app[i].Replace("UNIQUE", "");
}
if (app[i].Contains("BINARY COLUMN"))
{
dataGridView1.Rows[i - 1].Cells[5].Value = true;
app[i] = app[i].Replace("BNARY COLUMN", "");
}
if (app[i].Contains("UNSIGNED DATA"))
{
dataGridView1.Rows[i - 1].Cells[6].Value = true;
app[i] = app[i].Replace("UNSIGNED DATA", "");
}
if (app[i].Contains("AUTO INCREMENT"))
{
dataGridView1.Rows[i - 1].Cells[7].Value = true;
app[i] = app[i].Replace("AUTO INCREMENT", "");
}
if (app[i].Contains("ZERO FILL"))
{
dataGridView1.Rows[i - 1].Cells[8].Value = true;
app[i] = app[i].Replace("ZERO FILL", "");
}
if (app[i].Contains("DEFAULT"))
{
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(app[i], @"DEFAULT\s+\S+");
if (m.Success)
{
dataGridView1.Rows[i - 1].Cells[9].Value = m.Value.TrimEnd(',').Split(' ')[1];
app[i].Replace(dataGridView1.Rows[i - 1].Cells[9].Value.ToString(), "");
}
}
/* NOW I ONLY HAVE NAME + TYPE*/
dataGridView1.Rows.Add();
dataGridView1.Rows[i - 1].Cells[0].Value = app[i].Split(new char[] { '`', '`' })[1];
app[i]= app[i].Replace(app[i].Split(new char[] { '`', '`' })[1], "");
app[i] = app[i].Replace("``", "");
//type
(dataGridView1.Rows[i].Cells[1] as DataGridViewComboBoxCell).Items.Add(app[i].Substring(0, app[i].IndexOf(')') + 1));
app[i] = app[i].Replace(app[i].Substring(0, app[i].IndexOf(')') + 1), "");
}
}
To enter type values doesn't work. But this function seems to be unclear and too long. Is possible make an algorithm with Regex to compact table_syntax and alter table? After that how can I do the statements such as alter name ecc... Do I should use Tag to see items modified? I hope that someone can help me. Thanks in advance.
This is a very ambitious project and I congratulate you on your success so far.
Let me offer you a few notes:
Tags
to store the old values so I can compare and finally contruct the actual ALTER command You wrote To enter types value doesn't work. What do you mean by that?
All the best, this sounds fun. Is it for fun/study or for work?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.