奇怪! MySQL更新无法正常运行

时间:2011-10-27 19:09:19

标签: asp.net mysql

以下是我在项目中创建帐户的代码。代码工作正常,直到Response.Write(iduser),但UPDATE命令不起作用。使用异常未发现错误,但MySQL的记录未更新。

    try
                {

                    string pet1 = "admin@testlite.php";
                    string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=newtest;" + "UID=root;" + "PASSWORD=**********;" + "OPTION=3";
                    OdbcConnection MyConnection = new OdbcConnection(MyConString);
                    OdbcCommand cmd = new OdbcCommand("Select id_user from awm_accounts where email=?", MyConnection);
                    cmd.Parameters.Add("@val1", OdbcType.VarChar, 255).Value = pet1;
                    MyConnection.Open();
                    OdbcDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows == false)
                    {
                        throw new Exception();
                    }
                    if (dr.Read())
                    {
                        int iduser = Convert.ToInt32(dr[0].ToString());
                        Account acct = new Account();
                        acct.Email = username.Text + domain.Text;
                        acct.MailIncomingLogin = username.Text + domain.Text;
                        acct.MailIncomingHost = "imap." + DropDownList1.SelectedValue;
                        acct.MailIncomingPassword = password.Text;
                        acct.MailIncomingPort = 993;
                        acct.MailOutgoingHost = "smtp." + DropDownList1.SelectedValue;
                        acct.MailOutgoingPort = 465;
                        acct.MailIncomingProtocol = IncomingMailProtocol.Imap4;
                        acct.MailOutgoingAuthentication = true;
                        acct.DefaultAccount = false;
                        acct.IDUser = 1;
                        integr.CreateUserFromAccount(acct);
                        Response.Write(iduser);
                        if (!IsPostBack)
                        {
                            cmd = new OdbcCommand("UPDATE awm_accounts SET id_user=? WHERE email=? ", MyConnection);
                            cmd.Parameters.Add("@tb_nickname", OdbcType.Int, 11).Value = iduser;
                            cmd.Parameters.Add("@tb_fullname", OdbcType.VarChar, 255).Value = username.Text + domain.Text;
cmd.ExecuteNonQuery();
                        }
                    }
                    MyConnection.Close();
                }
            catch (Exception exp)
            {
                Response.Write(exp);
            }

1 个答案:

答案 0 :(得分:2)

我不确定Response.Write部分,但我确信你创建了一个SQLCommand但从未真正执行它。

你忘了在那里放cmd.executeNonScalar()吗?