且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新SQL数据库中的datagridview选中的值

更新时间:2022-10-18 12:43:11

首先,更改OKS_Status 数据类型为位 [ ^ ],接受 0 1 或空值。

其次,使用查询喜欢:

 更新 Client_feedback  SET  OKS_Status =  CASE   WHEN  OKS_Status =  1   THEN   0   ELSE   1   END  
WHERE ALID = @ alid



其中 @alid 是正确的值(我相信它是整数值)。



完整示例:

  DECLARE   @ Client_feedback  (ALID  INT ,OKS_Status  BIT 

INSERT INTO @ Client_feedback (ALID,OKS_Status)
VALUES 1 0

SELECT *
FROM @ Client_feedback
- 返回:1 0

更新 @ Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 那么 0 ELSE 1 END
WHERE ALID = 1

SELECT *
FROM @ Client_feedback
- 返回:1 1
更新 @ Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 那么 0 ELSE 1 END
WHERE ALID = 1

SELECT *
FROM @ Client_feedback
- 返回:1 0





当然,您可以使用与'boolean'版本相同的逻辑;)



如果您使用SQL服务器数据库,我建议使用存储过程。


只能编写更新查询在数据库中。


$ B $ T更新的东西b。您需要连接到数据库使用适当的的ConnectionString 。然后在 Command 对象的帮助下,您可以定义要执行的 Query 。然后使用 Command 对象的执行方法,您将执行该命令,以便执行查询并进行更新。


  private   void  btnUpdate_Click(对象发​​件人,EventArgs e)
{


foreach (DataGridViewRow row dataGridView1.Rows中的class =code-keyword>
{
if (row.Cells [ 5 ]。值!= null && Convert.ToBoolean(row.Cells [ 5 ]。值))
{
query = update Client_feedback设置OKS_Status ='true'其中ALID =' + row.Cells [ 2 ]。值+ ';
ClassMain.ExecuteNonQuery(query);

}
else
{
query = 更新Client_feedback设置OKS_Status ='false'其中ALID =' + row.Cells [ 2 ]。值+ ';
ClassMain.ExecuteNonQuery(query);

}

}





//它有效..谢谢大家..


private void btnUpdate_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells[5].Value != null && Convert.ToBoolean(row.Cells[5].Value))
        {
            query = "update  Client_feedback set OKS_Status = 'true' where ALID = '" + row.Cells[2].Value + "'";
        }
    }
}


I have a datagridview checkbox column and I want if the checkbox is checked "TRUE" value will be updated in sql database.. and if the checkbox is unchecked "false "value will be updated in sql database.. everything is fine..but data is not getting updated in sql.
Thanks for any help.

First of all, change OKS_Status data type to bit[^], which accepts 0, 1 or null values.
Secondly, use query like:
UPDATE  Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 THEN 0 ELSE 1 END
WHERE ALID = @alid


Where @alid is proper value (i believe it's integer value).

Complete sample:

DECLARE @Client_feedback TABLE(ALID INT, OKS_Status BIT)

INSERT INTO @Client_feedback (ALID, OKS_Status)
VALUES(1,0)

SELECT *
FROM @Client_feedback
--returns: 1	0

UPDATE  @Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 THEN 0 ELSE 1 END
WHERE ALID = 1

SELECT *
FROM @Client_feedback
--returns: 1	1
UPDATE  @Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 THEN 0 ELSE 1 END
WHERE ALID = 1

SELECT *
FROM @Client_feedback
--returns: 1	0



Of course, you can use the same logic with 'boolean' version ;)

If you use SQL server database, i suggest to use stored procedure.


Only writing an Update Query won't update things in Database.

You need to connect to the Database using proper ConnectionString. Then with the help of Command Object, you can define the Query to execute. Then using the Execute Methods of Command Object, you will execute the command, so that the query gets executed and your update will take place.


private void btnUpdate_Click(object sender, EventArgs e)
        {


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[5].Value != null && Convert.ToBoolean(row.Cells[5].Value))
                {
                    query = "update  Client_feedback set OKS_Status = 'true' where ALID = '" + row.Cells[2].Value + "'";
                    ClassMain.ExecuteNonQuery(query);

                }
                else
                {
                    query = "update  Client_feedback set OKS_Status = 'false' where ALID = '" + row.Cells[2].Value + "'";
                    ClassMain.ExecuteNonQuery(query);

                }

            }



//it worked..Thank you all..