【问题标题】:select some records and then update them simultaneuosly in asp.net c#选择一些记录,然后在asp.net c#中同时更新它们
【发布时间】:2013-04-17 21:54:42
【问题描述】:

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

我不知道怎么解决。

我能得到任何帮助吗?

【问题讨论】:

  • 你尝试过什么......并从更新查询中删除“frm”......
  • @Rahul-实际上我不知道从哪里开始。
  • 实际上你想要什么代码或方法..你发现的错误或中断是什么。
  • ya 或任何实现这一目标的方法。
  • 我已经告诉过您,只需更正您的更新查询即可。

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


【解决方案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();

我希望它对你有用。

【讨论】:

  • string getpaid = "update tblfameface set img_f_p='free' where img_f_p=(select TOP 2 * from tblfameface where img_f_p='paid')";
  • 不 * 使用 img_f_p ,仅运行该部分并忘记您在问题上写的选择查询部分..
【解决方案2】:

这里你的更新语句是错误的:

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

【讨论】:

    【解决方案3】:

    在这里,您可以只执行如下所示的单个更新语句。

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

    【讨论】:

    • 请再读一遍这个问题,它不仅仅是我需要执行的更新,首先我想获取记录然后只更新那些记录。
    • 看看你应用什么条件来获取记录,你可以用同样的条件来更新。
    【解决方案4】:

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

    这可能对 C# 有帮助:

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

    这对于 sql:

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 在我的问题中,我刚刚写了我想开发的原型。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 2021-08-27
      • 2022-01-21
      • 1970-01-01
      • 2019-06-05
      相关资源
      最近更新 更多