简体   繁体   English

使用ASP.NET中的ID仅更新一列

[英]Update only one column using ID in asp.net

I want to update only one column in my table by ID . 我只想通过ID更新表中的一列。

I don't have any error but this don't work, it won't update. 我没有任何错误,但这不起作用,它不会更新。 I have ID column and 7 more columns. 我有ID栏和另外7栏。

SqlCommand cmd1 = new SqlCommand("update table set amount=@kol where ID=@id" , con);
cmd1.Parameters.AddWithValue("@id", textbox1.Text);
cmd1.Parameters.AddWithValue("@kol", textbox2.Text );

Is your table named "table" or is that just for the example here? 您的表是命名为“表”还是仅用于此处的示例?

Because otherwise you properbly need to change "table" to whatever table your're trying to update. 因为否则您可能需要将“表”更改为要更新的表。 or surround it with [] if it is actually called "table" 或用[]括起来,如果它实际上被称为“表格”

Can you please check that you have commited your work , if there is no exception then that will be the reason 能请您检查一下您的工作吗,如果没有例外,那么这就是原因

and if not put setautocommit(true) - java version 如果不放setautocommit(true)-Java版本

you can find it for c# 您可以在C#中找到它

please check whether table name is correct and the table which you are verifying is correct 请检查表名是否正确以及您要验证的表是否正确

please give some other table name than table for good practice 请提供除表格以外的其他表格名称以作好习惯

As long as you have con.Open and ExecuteNonQuery and have the username/password and connectionstring right your code will work. 只要您具有con.Open和ExecuteNonQuery并具有正确的用户名/密码和连接字符串,您的代码就可以使用。

This will work after you change the connectionstring, if not the problem is sql server. 更改连接字符串后,这将起作用,如果不是问题,则是sql server。

private void UpdateTable()
            {
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=YourDataBase;Persist Security Info=True;User ID=username;Password=pass");
                SqlCommand cmd1 = new SqlCommand("update YourTable set amount=@kol where ID=@id", con);
                cmd1.Parameters.AddWithValue("@id", textBox1.Text);
                cmd1.Parameters.AddWithValue("@kol", textBox2.Text);
                con.Open();
                cmd1.ExecuteNonQuery();
            }

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

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