0

I am developing a sequence in MySQL & VB.NET in which, when deleting a record, the number that identifies it, is deleted and put a sequence of 1 to 'n', the sequence is as follows.

this is the instruccion for MySQL.

SET @rownum=0;
UPDATE id_line t, (SELECT @rownum:=@rownum+1 rownum, id_line.* FROM id_line WHERE id_line.line_name=59999 and id_line.line_no<>0) r 
SET t.line_no = r.rownum 
WHERE (t.id_line_b = r.id_line_b)

in VB.net I use this

cmdB = New MySqlCommand("SET @rownum=0 UPDATE id_line t, (SELECT @rownum=:@rownum+1 rownum, id_line.* FROM id_line WHERE id_line.line_name='" & TextBox1.Text & "' and id_line.line_no<>0) r ) SET t.line_no = r.rownum WHERE(t.id_line_b = r.id_line_b)", conn)

but VB sends this error

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 'UPDATE id_line t, (SELECT @rownum=:@rownum+1 rownum, id_line.* FROM id_lin' at line 1

Could you help me please in this error?

Updated 1: This is my string connection.

Public conString As String = "Data Source=server_one;port=3306;Initial Catalog=test_db;User Id=root;password=root;Allow User Variables=True"
2
  • For a start, I see a semi-colon at the end of SET @rownum=0; but it doesn't appear in the string in the VB.NET code. Commented Mar 25, 2019 at 18:38
  • Yes, the first part of the code is for the database engine, add the semicolon in VB.NET, but the error persists. Commented Mar 25, 2019 at 19:34

2 Answers 2

1

other better way for create mysql procedure for delete mysql data query

https://www.tutorialspoint.com/What-is-stored-procedure-and-how-can-we-create-MySQL-stored-procedures

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your contribution, but the error is in VB, in MySQL engine is working fine, anyway I'll try whit the method you propose. Thank you again. Regards.
0

I found the error, a parenthesis, it was misplaced, this is the correct code.

cmdB = New MySqlCommand("SET @rownum:=0; UPDATE id_line t, (SELECT @rownum:=@rownum+1 rownum, id_line.* FROM id_line WHERE id_line.line_name='" & TextBox1.Text & "' and id_line.line_no<>0) r SET t.line_no = r.rownum WHERE(t.id_line_b = r.id_line_b)", conn)

Thank you everyone for your attention and support.

Regards.

2 Comments

I am glad you found your error but please use parameters. That TextBox1.Text can cause damage to your database. It needs to be sanitized by setting it as the value of a parameter which will be treated as a value not executable code by the database engine.
OK, I understand, the code was updated whit a variable before the sentence to do the MySQLCommand, is working too!. Thank you so much for your contribution too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.