如何更新列的id等于dropdownlist1.items的数据库列 [英] How to update column of database where id of column is equal to dropdownlist1.items

查看:142
本文介绍了如何更新列的id等于dropdownlist1.items的数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我在asp.net网站上有页面。我的数据库有3列表。

Id(int),title(varchar),state_p(int)。 state_p = 0的默认值。



我在其中有带Checkbox的GridView



我想要每个检查CheckBox1时,网格视图行中的行为true,然后返回DropDownList1中的行ID。此代码成功完成:



  foreach (GridViewRow gvr  GridView1.Rows中的class =code-keyword> 
{
if (((CheckBox)gvr.FindControl( CheckBox1))。选中== true
{
string 数据;
data = gvr.Cells [ 0 ]。文字;
DropDownList1.Items.Add(data);
}
}





问题是我想将数据库中的state_p列更新为1 WHERE id ='+ DropDownList1.Items +'。

我该怎么做。请帮帮我。



我尝试了什么:



怎么样更新列的数据库,其中列的id等于DropDownList1.Items

解决方案

如果您通过在应用程序中构建它来使用动态SQL,则可以执行以下操作;但是,这只会将state_p设置为1.您还需要输入代码才能反转。如果state_p为1,但需要为零(未选中),该怎么办?



**仅用此步骤查看代码是如何构建的where子句的数据。要正确执行任何SQL,应该是构建值列表并将这些值传递给方法,该方法在数据库服务器上执行存储过程或函数。 **



 StringBuilder sql =  new  StringBuilder( ); 

sql.Append( UPDATE< TABLE>);
sql.Append( SET state_p = 1);
sql.Append( WHERE id IN(

foreach (GridViewRow gvr in GridView1.Rows)
{
if (((CheckBox)gvr.FindControl( CheckBox1))。选中== true
{
string 数据;
data = gvr.Cells [ 0 ]。文本;
sql.Append(data + );
DropDownList1.Items.Add(data);
}
}

string exeSql = sql.ToString()。Substring( 0 ,sql.Length - 1 )+ ;

// 使用exeSql执行sql语句;


Hi All. I have page in asp.net Website. My database has table with 3 columns.
Id(int) , title(varchar), state_p(int). default value of state_p = 0 .

I Have GridView with Checkbox within it

I want for each row in Grid View rows when checked of CheckBox1 is true then return id of rows in DropDownList1. This code done it successfully:

foreach (GridViewRow gvr in GridView1.Rows)
        {
            if (((CheckBox)gvr.FindControl("CheckBox1")).Checked == true)
            {
                string data;
                data = gvr.Cells[0].Text;
                DropDownList1.Items.Add(data);
            }
        }



The problem is that I want to update state_p columns in my database to "1" WHERE id= '" + DropDownList1.Items + "'.
How can I do that. Please help me.

What I have tried:

how to update column of database where id of column is equal to DropDownList1.Items

解决方案

If you are using dynamic SQL by building it in the application, you can do something like this; however, this will only work setting the state_p to 1. You will need to also put in code to reverse. What if the state_p is one, but needs to be zero (unchecked).

** Use this only to step through to see how the code is building the data for the where clause. To properly execute any SQL should be to build a list of values and pass those values to a method, which executes a stored procedure or function on the database server. **

StringBuilder sql = new StringBuilder();

sql.Append(" UPDATE  <TABLE>");
sql.Append(" SET state_p = 1 ");
sql.Append(" WHERE id IN ("

foreach (GridViewRow gvr in GridView1.Rows)
        {
            if (((CheckBox)gvr.FindControl("CheckBox1")).Checked == true)
            {
                string data;
                data = gvr.Cells[0].Text;
				sql.Append(data + ",");
                DropDownList1.Items.Add(data);
            }
        }
		
 string exeSql = sql.ToString().Substring(0, sql.Length - 1) + ")";

//Execute the sql statment by using exeSql;


这篇关于如何更新列的id等于dropdownlist1.items的数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆