开发者

ODBC command to update

What is the error in the below code, and how do I pass karthik@domain.com and kars@domain.com using forms?

string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=new_db;" + "UID=root;" + "PASSWORD=pa开发者_如何学编程ssword;" + "OPTION=3";

OdbcConnection MyConnection = new OdbcConnection(MyConString);

OdbcCommand cmd = new OdbcCommand("UPDATE awm_create SET referral_email=karthik@domain.com WHERE email=kars@domain.com" , MyConnection);

MyConnection.Open();

cmd.ExecuteNonQuery();


Try this:

OdbcCommand cmd = new OdbcCommand("UPDATE awm_create 
   SET referral_email='karthik@domain.com' WHERE email='kars@domain.com'",
   MyConnection);

If you need to parameterize that query, you can do something like:

OdbcCommand cmd = new OdbcCommand("UPDATE awm_create
   SET referral_email=? WHERE email=?", MyConnection);

cmd.Parameters.Add("@referral_email", OdbcType.VarChar).Value = "karthik@domain.com";
cmd.Parameters.Add("@email", OdbcType.VarChar).Value = "kars@domain.com";


Place single quotes around the email addresses.


Your query has syntax errors:

UPDATE awm_create SET referral_email=karthik@domain.com WHERE email=kars@domain.com

there's no quotes around either email address. It should be

UPDATE awm_create SET referral_email='karthik@domain.com' WHERE email='kars@domain.com'
                                     ^--                ^--           ^--             ^--


My trick when faced with this type of error is to copy the SQL query from the code and execute it directly against the database using the database user interface. This way I ensure where the error lies. Yes you're missing the string delimiters and the query interface to MYSql would tell you that while the IDE interface only knows that there was an error.
If that step works then go up the chain to figure out what doesn't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜