【问题标题】:C# ASP.NET MySql Fatal error encountered during command execution命令执行期间遇到 C# ASP.NET MySql 致命错误
【发布时间】:2016-08-16 13:31:29
【问题描述】:
                try
            {
                conn.Open();

                MySqlCommand cmd = new MySqlCommand(@"UPDATE `users` SET `firstname`=@firstname,`lastname`=@lastname,`industry`=@industry,`companyname`=@companyname,`country`=@country,`addressone`=@addressone,`addresstwo`=@addresstwo,`city`=@city,`stateOrProvince`=@stateone,`zip`=@zip,`phone`=@phone,`companyphone`=@companyphone,`countrytwo`=@countrytwo,`citytwo`=@citytwo,`statetwo`=@statetwo,`ziptwo`=@ziptwo WHERE `email`=@email", conn);
                cmd.Parameters.AddWithValue("@firstname", firstName.Text);
                cmd.Parameters.AddWithValue("@lastname", lastName.Text);
                cmd.Parameters.AddWithValue("@industry", dropDownIndustry.Text);
                cmd.Parameters.AddWithValue("@companyname", companyInfo.Text);
                cmd.Parameters.AddWithValue("@country", countryTextbox.Text);
                cmd.Parameters.AddWithValue("@addressone", addressInfo.Text);
                cmd.Parameters.AddWithValue("@addresstwo", addresstwoInfo.Text);
                cmd.Parameters.AddWithValue("@city", cityTextBox.Text);
                cmd.Parameters.AddWithValue("@stateone", stateTextBox.Text);
                cmd.Parameters.AddWithValue("@zip", zipTextBox.Text);
                cmd.Parameters.AddWithValue("@phone", newphone);
                cmd.Parameters.AddWithValue("@companyphone", phonecompany);
                cmd.Parameters.AddWithValue("@countrytwo", countryTextBoxtwo.Text);
                cmd.Parameters.AddWithValue("@citytwo", cityTwoTextBox.Text);
                cmd.Parameters.AddWithValue("@statetwo", stateTwoTextBox.Text);
                cmd.Parameters.AddWithValue("@email", Session["email"]);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                serverErrorTextBox.Text = "Data updated Successfully...!" + companyPhoneTextBox.Text + " " + Session["email"] + "";
            }
            catch(MySqlException ex)
            {
                serverErrorTextBox.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }

我不太明白这个查询有什么问题,有人可以帮忙吗?

【问题讨论】:

  • 在这里发布错误
  • 您是否遇到异常或者这只是输出了错误的数据?请说明当您尝试执行此代码时当前的结果是什么,以及您期望它是什么。
  • 也许您更改了列的数据类型或从表中删除的内容?请在此处显示完整的错误消息
  • 如果没有更多信息或说明,我们可以给你一堆答案,这些答案都是合法的,但不能解决你的问题。

标签: c# mysql asp.net


【解决方案1】:

你没有 ziptwo 参数!!在您的更新声明中

【讨论】:

  • 看来这已经解决了我的问题,谢谢! (:不过我现在感觉很愚蠢。我将其更改为 using 语句而不是 try 语句,它输出我没有 ziptwo 啊哈哈哈希望我能注意到这一点,我不会经历这么大的挣扎。我想我不应该在我累的时候尝试做事xD
【解决方案2】:
try
        {
            conn.Open();

            MySqlCommand cmd = new MySqlCommand(@"UPDATE `users` SET `firstname`=@firstname,`lastname`=@lastname,`industry`=@industry,`companyname`=@companyname,`country`=@country,`addressone`=@addressone,`addresstwo`=@addresstwo,`city`=@city,`stateOrProvince`=@stateone,`zip`=@zip,`phone`=@phone,`companyphone`=@companyphone,`countrytwo`=@countrytwo,`citytwo`=@citytwo,`statetwo`=@statetwo,`ziptwo`=@ziptwo WHERE `email`=@email", conn);
            cmd.Parameters.Add("@firstname",SqlDbType.NVarChar).Value = firstName.Text);
            cmd.Parameters.Add("@lastname",SqlDbType.NVarChar).Value = lastName.Text);
            cmd.Parameters.Add("@industry",SqlDbType.NVarChar).Value = dropDownIndustry.Text);
            cmd.Parameters.Add("@companyname",SqlDbType.NVarChar).Value = companyInfo.Text);
            cmd.Parameters.Add("@country",SqlDbType.NVarChar).Value = countryTextbox.Text);
            cmd.Parameters.Add("@addressone",SqlDbType.NVarChar).Value = addressInfo.Text);
            cmd.Parameters.Add("@addresstwo",SqlDbType.NVarChar).Value = addresstwoInfo.Text);
            cmd.Parameters.Add("@city",SqlDbType.NVarChar).Value = cityTextBox.Text);
            cmd.Parameters.Add("@stateone",SqlDbType.NVarChar).Value = stateTextBox.Text);
            cmd.Parameters.Add("@zip",SqlDbType.NVarChar).Value = zipTextBox.Text);
            cmd.Parameters.Add("@phone",SqlDbType.NVarChar).Value = newphone);
            cmd.Parameters.Add("@companyphone",SqlDbType.NVarChar).Value = phonecompany);
            cmd.Parameters.Add("@countrytwo",SqlDbType.NVarChar).Value = countryTextBoxtwo.Text);
            cmd.Parameters.AddWithValue("@citytwo",SqlDbType.NVarChar).Value = cityTwoTextBox.Text);
            cmd.Parameters.AddWithValue("@statetwo",SqlDbType.NVarChar).Value = stateTwoTextBox.Text);
            cmd.Parameters.AddWithValue("@email",SqlDbType.NVarChar).Value = Session["email"]);
            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            serverErrorTextBox.Text = "Data updated Successfully...!" + companyPhoneTextBox.Text + " " + Session["email"] + "";
        }
        catch(MySqlException ex)
        {
            serverErrorTextBox.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }

【讨论】:

  • 如果你要不厌其烦地回答一个问题,至少写下你改变了什么。
  • 那么您是否有理由将 SqlDbType.NVarChar).Value 添加到 cmd.Parameters.Add 语句中。如果在安全问题上有适当的理由,请告诉我谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-06-14
  • 2021-09-02
  • 2014-10-13
  • 1970-01-01
  • 2020-10-15
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多