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

时间:2013-04-24 11:11:02

标签: c# asp.net sql-server select sql-update

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

就像:

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();
                }
}

我不知道如何解决它。

我可以得到任何帮助吗?

5 个答案:

答案 0 :(得分:1)

除了“更新”查询之外,您的原型是可以的。重写您的更新查询,如

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();

我希望它适合你。

答案 1 :(得分:0)

此处您的更新声明错误:

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

答案 2 :(得分:0)

在这里,你可以执行如下的单个更新语句。

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

答案 3 :(得分:0)

请更具体一点。你有sql或c#的问题吗?

这可能对C#有用:

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

这适用于sql:

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

答案 4 :(得分:-1)

更新tblx set status ='y',其中status ='x';

相关问题