选择一些记录,然后在asp.net c#中同时更新它们

本文关键字:更新 net asp 记录 然后 选择 | 更新日期: 2023-09-27 18:10:35

我正在开发一个web服务,在这个web服务中,我根据状态获取一些记录,然后我需要更改状态。

其像

:

select * from tblx where status='x'

然后

update tblx set status='y' where status='x'为获取的记录。

示例代码:

 string getpaid = "select top2 * from tblfameface where img_f_p='paid'";
                con1 = new SqlConnection(conString1);
                con1.Open();
                SqlCommand cmd = new SqlCommand(getpaid, con1);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt1 = new DataTable();
                da.Fill(dt1);
                foreach (DataRow row in dt1.Rows)
                {
                    string updatepaid = "update tblfameface set img_f_p='free' where img_f_p='paid' ";
                    con1 = new SqlConnection(conString1);
                    con1.Open();
                    SqlCommand cmd1 = new SqlCommand(updatepaid, con1);
                    int temp = cmd1.ExecuteNonQuery();
                    con1.Close();
                }
}

我不知道如何解决它。

我能得到帮助吗?

选择一些记录,然后在asp.net c#中同时更新它们

除了你的"Update"查询之外,你的Prototype是ok的。重写您的更新查询,如

update tblx set status='y' where status=(select status from tblx where status='x');

根据您的要求:

string updatepaid = "update tblfameface set img_f_p='free' where img_f_p=(select top 2 img_f_p from tblfameface where img_f_p='paid')";
con1 = new SqlConnection(conString1);
con1.Open();
SqlCommand cmd1 = new SqlCommand(updatepaid, con1);
int temp = cmd1.ExecuteNonQuery();
con1.Close();

这里你的update语句是错误的:

UPDATE <TABLE NAME> SET <Column> = <New Value> WHERE <Condition>

在这里,您可以像下面这样执行单个更新语句。

UPDATE YourTable
SET status='Y'
WHERE status='X'

请说得更具体些。你对sql或c#有问题吗?

这对c#很有帮助:

http://msdn.microsoft.com/en-us/library/tyy0sz6b.aspx

和sql:

http://www.w3schools.com/sql/sql_update.asp

update TBLX set status='y' where status='x';