当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Create an MySQL Alter Table statement from a DataGridView with fields and options?

问题描述:

Form http://i61.tinypic.com/2yjv4w9.png

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:

  • Looking at the MySql specs on CREATE TABLE and ALTER TABLE it seems obvious that you can only implement a subset of the full range of options. That is OK, but you need to define that subset and also think about how it might change once you notice that some tables are using one of the things you left out after all..
  • With such a complicated task a few long and involved function are to expected
  • That being said you should try to modularize the parsing function:
    • separate parsing the column lines from the rest into a parse_COLUMN(..) function
    • separate other clause into named parsing functions of their own like parse_PF(..) and parse_FK(..)
  • RegEx is a mighty tool but I doubt that it will help you to magically remove the complexities of the parsing. Instead work through the specs, pretty much like you do!
  • I noticed that your "AUTO_INCREMENT" string is missing the underscore.
  • Also: You expect the PRIMARY KEY clause with the column; I have seen this but more often is comes in a separate clause after the column definitions
  • Watch out for COMMENT clauses!
  • Yes, I would use 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?

分享给朋友:
您可能感兴趣的文章:
随机阅读: