Rafael Diaz Rafael Diaz - 1 year ago 57
MySQL Question

Changing Column Name of a Table in MySQL using VB.net

I was wondering if there are any ways that I can change the column name in MySQL using VB.net I'm creating a program that can update a 5 column names in a fixed table in MySQL using VB.net. The only thing I can do so far is change the header text in the DataGridView but not update the Column's name in mySQL. Everytime I re run the program the column's name in the DataGridView reverts back into its old name.

Here's what I've done in my Update Button so far.

DataGridView1.Columns(0).HeaderText = TextBox1.Text
DataGridView1.Columns(1).HeaderText = TextBox2.Text
DataGridView1.Columns(2).HeaderText = TextBox3.Text
DataGridView1.Columns(3).HeaderText = TextBox4.Text
DataGridView1.Columns(4).HeaderText = TextBox5.Text

Any help would be appreciated.

Answer Source

Are you wanting to change the column name in the database table itself? If so, you'll need to execute a statement to MySQL itself. Changes to a DataGrid don't modify schema definitions.

Set up a MySQLConnection and MySQLCommand and .ExecuteNonQuery a line similar to:

ALTER TABLE test CHANGE OldColumnName NewColumnName varchar(120);

Given your explanation of what you're doing, you should have OldColumnName and NewColumnName available. I imagine you know what the type is as well.