2013-04-24 68 views
-1

我正在開發一個web服務,其中我使用基於狀態的select語句獲取一些記錄,然後我需要更改狀態。選擇一些記錄,然後在asp.net c中同步更新它們#

它像:

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

我不知道如何解決它。

我可以得到任何幫助嗎?

+0

你試過什麼......並從更新查詢中刪除「frm」.. – Rahul 2013-04-24 11:13:19

+0

@ Rahul-實際上我不知道從哪裏開始。 – Khushbu 2013-04-24 11:15:03

+0

實際上你想要一個代碼或一種方式..什麼是你發現的錯誤或入侵。 – Rahul 2013-04-24 11:18:07

回答

1

你的原型是確定的,除了你的 「更新」 query.Rewrite您更新查詢像

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

我希望它爲你工作。

+0

string getpaid =「update tblfameface set img_f_p ='free'其中img_f_p =(從tblfameface中選擇TOP 2 *,其中img_f_p ='paid')」; – Khushbu 2013-04-24 12:28:30

+0

不使用img_f_p ,,只運行該部分,忘記選擇查詢部分,你寫在你的問題上.. – Rahul 2013-04-24 12:30:05

-1

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

+0

在我的問題中,我寫了我想開發的原型。 – Khushbu 2013-04-24 11:17:05

0

這裏你update語句是錯誤的:

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

在這裏你可以只執行一個更新語句像下面。

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

請再次閱讀這個問題,它不只是更新我需要執行,首先我想獲取記錄,然後只更新這些記錄。 – Khushbu 2013-04-24 11:40:49

+0

查看您應用於獲取記錄的條件,您可以使用相同的條件進行更新。 – AnandPhadke 2013-04-24 11:42:52

相關問題