Click here to Skip to main content
15,913,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am making a simple program to create a mysql table from vb.net and renaming it by user choice.
i got success in creating the table it is quite simple but how to rename a table from textbox tortured me a lot.
here's the code to create a table
VB
Try
 cnn.Open()
 Dim query As String
 query = "CREATE TABLE best.new (id INT NOT NULL,  name VARCHAR(45) NULL, date DATETIME NULL,   PRIMARY KEY (id));"

 cmd = New MySqlCommand(query, cnn)
 reader = cmd.ExecuteReader
 MessageBox.Show("table created")
 cnn.Close()


i tried to rename it using this code but no success....! achieved yet
i tried few more but it gives error
i want to rename it from the "id" given by the user
VB
Dim rename As String
 Rename = ("RENAME TABLE new To" TextBox1.Text)
 cmd = New MySqlCommand(Rename, cnn)
 reader = cmd.ExecuteReader
 MessageBox.Show("renamed")
 cnn.Close()


please help me to find it out
Posted
Updated 8-Jun-14 11:29am
v3
Comments
Nirav Prabtani 7-Jun-14 6:43am    
what have you tried for that??
the challanger 7-Jun-14 6:51am    
the above code i mentioned is the last one, i also tried to change the file name from directory but it also gives errors

Try this One (Added Space after To and added &):
VB
Dim rename As String
Rename = ("RENAME TABLE new To " & TextBox1.Text)
cmd = New MySqlCommand(Rename, cnn)
reader = cmd.ExecuteReader
MessageBox.Show("renamed")
cnn.Close()

I hope this will help you. :)
 
Share this answer
 
Comments
the challanger 12-Jun-14 4:44am    
i tried your code and it gives an exception
you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'new to 1' at line 1
the challanger 12-Jun-14 4:49am    
cnn = New MySqlConnection
cnn.ConnectionString = ("server=localhost;userid=root;password=********;database=best")

Dim reader As MySqlDataReader
Try
cnn.Open()
Dim rename As String
rename = ("RENAME TABLE new To " & textbox1.text)
cmd = New MySqlCommand(rename, cnn)
reader = cmd.ExecuteReader
MessageBox.Show("renamed")
cnn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)

End Try
Add a space...

VB
Rename = ("RENAME TABLE new To" TextBox1.Text)
Becomes
VB
Rename = ("RENAME TABLE new To " TextBox1.Text)


But you had better be damn careful.
You leave your whole DB wide open to SQL Injection with this.
 
Share this answer
 
Comments
the challanger 7-Jun-14 7:14am    
i fixed it but a new error occured
System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
the challanger 8-Jun-14 9:31am    
last night i tried this one

Try
cnn.Open()
Dim rename As String
rename = ("RENAME TABLE lotta To "(TextBox1.Text))
cmd = New MySqlCommand(rename, cnn)
reader = cmd.ExecuteReader
MessageBox.Show(" created & renamed ")
cnn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)

End Try


but it gives exception

System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
Source=newapplication
InnerException:
try with
VB
Rename = "RENAME TABLE best.new TO " & TextBox1.Text
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900