1

我已注册为我的数据库表。我想将用户输入的信息更新到我的 SQL Server 数据库中。但它不起作用,它不会发生任何错误,但数据键不会更新到我的数据库中。如果我的代码有任何问题,请有人帮助我?谢谢。

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True");

    SqlCommand cmd = new SqlCommand("UPDATE registration SET username = @username, password = @password, retypepassword = @retypepassword, gender = @gender, birth = @birth, address = @address, city = @city, country = @country, postcode = @postcode, email = @email, carno = @carno", con);

    con.Open();

    cmd.Parameters.AddWithValue("@username", TextBoxUsername.Text);
    cmd.Parameters.AddWithValue("@password", TextBoxPassword.Text);
    cmd.Parameters.AddWithValue("@retypepassword", TextBoxRPassword.Text);
    cmd.Parameters.AddWithValue("@gender", DropDownListGender.Text);
    cmd.Parameters.AddWithValue("@birth", DropDownListDay.Text);
    cmd.Parameters.AddWithValue("@address", TextBoxAddress.Text);
    cmd.Parameters.AddWithValue("@city", TextBoxCity.Text);
    cmd.Parameters.AddWithValue("@country", DropDownListCountry.Text);
    cmd.Parameters.AddWithValue("@postcode", TextBoxPostcode.Text);
    cmd.Parameters.AddWithValue("@email", TextBoxEmail.Text);
    cmd.Parameters.AddWithValue("@carno", TextBoxCarno.Text);

    cmd.ExecuteNonQuery();

    con.Close();

    if (IsPostBack)
    {
        Response.Redirect("UpdateSuccess.aspx");
    }

在我单击确认后,它以某种方式仅更新我gender的从男性到女性的列,其他数据列不会更新。

4

5 回答 5

2

这可能是因为@username 在 UPDATE 和 WHERE 中都使用了。如果它改变了,WHERE 将是错误的,如果它没有改变,它可以被排除在查询之外。

于 2012-12-30T10:11:38.270 回答
-1

sql更新

SqlCommand cmd = new SqlCommand("update[Testing].[dbo].[student] set name='" + tb1.Text + "',age='" + tb2.Text + "',mobile='" + tb3 .Text+ "' 其中 id = '" + tb4.Text + "'", con);

于 2017-07-18T11:19:26.947 回答
-1
protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True");

    SqlCommand cmd = new SqlCommand("SELECT * FROM registration WHERE username = " + TextBoxUsername.Text + "UPDATE registration SET username = @username, password = @password, retypepassword = @retypepassword, gender = @gender, birth = @birth, address = @address, city = @city, country = @country, postcode = @postcode, email = @email, carno = @carno", con);

    con.Open();

    cmd.Parameters.AddWithValue("@username", TextBoxUsername.Text);
    cmd.Parameters.AddWithValue("@password", TextBoxPassword.Text);
    cmd.Parameters.AddWithValue("@retypepassword", TextBoxRPassword.Text);
    cmd.Parameters.AddWithValue("@gender", DropDownListGender.Text);
    cmd.Parameters.AddWithValue("@birth", DropDownListDay.Text);
    cmd.Parameters.AddWithValue("@address", TextBoxAddress.Text);
    cmd.Parameters.AddWithValue("@city", TextBoxCity.Text);
    cmd.Parameters.AddWithValue("@country", DropDownListCountry.Text);
    cmd.Parameters.AddWithValue("@postcode", TextBoxPostcode.Text);
    cmd.Parameters.AddWithValue("@email", TextBoxEmail.Text);
    cmd.Parameters.AddWithValue("@carno", TextBoxCarno.Text);

    cmd.ExecuteNonQuery();

    con.Close();

    if (IsPostBack)
    {
        Response.Redirect("UpdateSuccess.aspx");
    }
于 2017-12-24T03:07:46.957 回答
-3

我认为在页面加载中,您将再次加载所有数据,并且 Button1_Click 在页面加载后运行并且您丢失了所有数据,您可以在 page_load 方法中尝试您的代码

private void Page_Load()
{
    if (IsPostBack)
    {
        SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True");

    SqlCommand cmd = new SqlCommand("UPDATE registration SET username = @username, password = @password, retypepassword = @retypepassword, gender = @gender, birth = @birth, address = @address, city = @city, country = @country, postcode = @postcode, email = @email, carno = @carno " + "WHERE username = @username", con);

    con.Open();
.
.
.

    }
}
于 2012-12-30T10:20:26.627 回答
-4

我从未见过这种类型的语法。通常,如果一个人正在使用参数,那么一个人正在使用stored proc(最佳实践)。如果使用inline SQL,则将语句构建SQL为单个文本行并执行它。我建议为其中之一重新编码。

如果你想尝试你已经开始的SQL,你可能需要先声明所有的变量。例如

SqlCommand cmd = new SqlCommand("declare @username varchar(100), @password varchar(100), 
@retypepassword varchar(100) @gender varchar(10), @birth date, @address varchar(100), 
@city varchar(100) @country varchar(100), @postcode varchar(10), @email varchar(100), 
@carno varchar(100) UPDATE registration SET username = @username, password = @password, 
retypepassword = @retypepassword, gender = @gender, birth = @birth, address = @address, 
city = @city, country = @country, postcode = @postcode, email = @email, carno = @carno", 
con);
于 2012-12-31T05:46:29.850 回答