繁体   English   中英

使用不更新参数更新SQL表

[英]Update sql table with parameters not updating

我在ASP页面中有此方法,执行时没有错误,甚至没有更新。 在这种不更新表的方法中,我在做什么错?

protected void saveSettings()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sendyourmessageConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    string userName = "something";

        try
        {
            cmd = new SqlCommand("UPDATE Tb_Registration SET Country = @Country, City = @City Where Username = @Username" , con);

            cmd.Parameters.AddWithValue("@Username", SqlDbType.VarChar);
            cmd.Parameters["@Username"].Value=userName;

            cmd.Parameters.AddWithValue("@Country", SqlDbType.VarChar);
            cmd.Parameters["@Country"].Value= txtCountry.Text.Trim();

            cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar);
            cmd.Parameters["@City"].Value= txtCity.Text.Trim();

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
            return;
        }
        finally
        {
            con.Close();
            cmd.Dispose();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Your settings have been saved');", true);
        }
}

我认为问题是AddWithValue方法。

它使用参数值作为第二个参数,而不是SqlDbType

但不要再使用此方法。 可能会产生意外的结果。 使用.Add()方法 ,它是重载的。

阅读: 我们可以停止使用AddWithValue()吗?

也可以使用using语句来处理SqlConnectionSqlCommand

using(SqlConnection con = new SqlConnection(connString))
using(SqlCommand cmd = con.CreateCommand())
{
   cmd.CommandText = @"UPDATE Tb_Registration SET Country = @Country, City = @City
                       Where Username = @Username";
   cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = userName;
   cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim();
   cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim();
   con.Open();
   cmd.ExecuteNonQuery();
}

您的问题在这里: cmd.Parameters.AddWithValue("@Country", SqlDbType.VarChar);

它应该是参数的值,如果使用的是AddWithValue ,则不能输入

但更好的做法-完全不要使用AddWithValue ,因为它可能导致不同的问题。

而是使用类似:

cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = userName;

插入直接值,而不是提供数据类型

 protected void saveSettings()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sendyourmessageConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        string userName = "something";

            try
            {
                cmd = new SqlCommand("UPDATE Tb_Registration SET Country = @Country, City = @City Where Username = @Username" , con);

                cmd.Parameters.AddWithValue("@Username", userName);


                cmd.Parameters.AddWithValue("@Country",  txtCountry.Text.Trim());


                cmd.Parameters.AddWithValue("@City", txtCity.Text.Trim());


                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
                return;
            }
            finally
            {
                con.Close();
                cmd.Dispose();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Your settings have been saved');", true);
            }
    }

AddWithValue的第二个参数是您的值而不是sql类型

 cmd.Parameters.AddWithValue("@Username", userName);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM