0

我正在开发一个 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();
                }
}

我不知道如何解决它。

我能得到任何帮助吗??

4

5 回答 5

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

我希望这个对你有用。

于 2013-04-24T11:23:40.387 回答
0

在这里您的更新声明是错误的:

UPDATE <TABLE NAME> SET <Column> = <New Value> WHERE <Condition>
于 2013-04-24T11:17:47.970 回答
0

在这里,您可以只执行一条更新语句,如下所示。

UPDATE YourTable
SET status='Y'
WHERE status='X'
于 2013-04-24T11:22:05.457 回答
0

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

这可能对 C# 有帮助:

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

这对于 sql:

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

于 2013-04-24T11:22:58.993 回答
-1

更新 tblx set status='y' where status='x';

于 2013-04-24T11:16:02.117 回答