Click here to Skip to main content
14,355,239 members
Rate this:
Please Sign up or sign in to vote.
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
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
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 12:29pm
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
Rate this:
Please Sign up or sign in to vote.

Solution 4

Try this One (Added Space after To and added &):
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. :)
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

Add a space...

Rename = ("RENAME TABLE new To" TextBox1.Text)
Becomes
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.
   
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:
Rate this:
Please Sign up or sign in to vote.

Solution 2

try with
Rename = "RENAME TABLE best.new TO " & TextBox1.Text
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100