Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

mysql - Parameter '@myLeft' must be defined

My question is quite similar to already answered ones but yet not. I'd like some help sorting this out.

I'm trying to add data to a database table but I get keeping the titled error. Here's my MySQL-code:

Dim objCmdInsert As New MySqlCommand("LOCK TABLE threads WRITE;SET @myLeft := lft;SELECT @myLeft FROM threads WHERE threadid = '" & Request.QueryString("id") & "';UPDATE threads SET rgt = rgt + 2 WHERE rgt > @myLeft;UPDATE threads SET lft = lft + 2 WHERE lft > @myLeft;INSERT INTO threads(threadid, category, userid, ipaddress, section, subject, summary, body, tags, postedat, updatedat, rating, status, lft, rgt) VALUES(?threadid, ?category, ?userid, ?ipaddress, ?section, ?subject, ?summary, ?body, ?tags, ?postedat, ?updatedat, ?rating, ?status, @myLeft + 1, @myLeft + 2);UNLOCK TABLES;", objConn)

objCmdInsert.Parameters.Add("?threadid", MySqlDbType.VarChar, 36).Value = Guid.NewGuid.ToString()
objCmdInsert.Parameters.Add("?category", MySqlDbType.VarChar, 36).Value = "articlecomment"
objCmdInsert.Parameters.Add("?ipaddress", MySqlDbType.VarChar, 20).Value = HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
objCmdInsert.Parameters.Add("?section", MySqlDbType.VarChar, 36).Value = DBNull.Value
objCmdInsert.Parameters.Add("?userid", MySqlDbType.VarChar, 36).Value = GetStubs.GetUserGuid(HttpContext.Current.User.Identity.Name.ToString())
objCmdInsert.Parameters.Add("?subject", MySqlDbType.VarChar, 500).Value = DBNull.Value
objCmdInsert.Parameters.Add("?summary", MySqlDbType.Text).Value = DBNull.Value
objCmdInsert.Parameters.Add("?body", MySqlDbType.Text).Value = CommentBody.Text
objCmdInsert.Parameters.Add("?tags", MySqlDbType.VarChar, 500).Value = DBNull.Value
objCmdInsert.Parameters.Add("?postedat", MySqlDbType.DateTime).Value = DateTime.Now.ToString()
objCmdInsert.Parameters.Add("?updatedat", MySqlDbType.DateTime).Value = DateTime.Now.ToString()
objCmdInsert.Parameters.Add("?rating", MySqlDbType.Int32).Value = "0"
objCmdInsert.Parameters.Add("?status", MySqlDbType.VarChar, 200).Value = "Open"

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the solution to this problem by making an extensive Google search for undefined parameters in MySQL. This article (MySql.Data.MySqlClient.MySqlException: Parameter ‘@id’ must be defined) covers the basics of the problem. It's as far as I understand related to an upgrade of the connector.

You need to add Allow User Variables=True in the connection string in order to use custom variables. Pretty simple solution to an equally tough problem. ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...