【问题标题】:Update Table, Where Column Name is selected dynamically - DropDown.SelectedValue更新表,其中列名是动态选择的 - DropDown.SelectedValue
【发布时间】:2010-03-10 22:10:25
【问题描述】:

我想更新一个表的列,其中 ColumnName 将为 DropDown.SelectedValue。

示例:将显示客户表中的一组记录,其中 CUstNo ='1234' 和 City='Chicago' 和 Grade ='B'

一旦显示,我想根据上述标准将所有这些客户的等级更新为“A”。

在我的情况下,我有 100 个列,因此将从 DaropDown 中选择子句列名。

在我的查询中,更新客户 SET ColumnName= 'some value' where ColumnName ='ActualValue'

那么如何传递作为下拉选择值的 ColumnName。

我相信我不能给予 更新客户 SET DropDown.SelectedValue = 'some value' where DropDown.SelectedValue ='ActualValue'

我该如何解决这个问题?...

【问题讨论】:

    标签: c# vb.net


    【解决方案1】:

    所以听起来您想基于您的网格构建动态 sql?如果是这样的话,你总是可以做类似的事情

    string updateStmt = String.Format("UPDATE Customer SET {0} = 'some value' WHERE...", dropDown.SelectedValue);
    

    然后你就可以执行这个语句了。我真的不知道你在使用什么,但有更好的方法来进行数据库更新。你可以使用像 Linq to SqlEntity Framework 这样的 ORM,或者你甚至可以使用像 SqlCommandBuilder 这样的东西

    编辑:这是 ASP.NET 中的一个示例(尽管 winform 应用程序非常相似)

    假设这个下拉列表是从某个数据源填充的

    <form id="form1" runat="server" method="post" target="Default2.aspx">              
        <asp:DropDownList ID="test" runat="server" AutoPostBack="true" OnSelectedIndexChanged="test_SelectedIndexChanged">
            <asp:ListItem Value="" Selected="True"/>
            <asp:ListItem Value="Col1"/>
            <asp:ListItem Value="Col2"/>
            <asp:ListItem Value="Col3"/>
        </asp:DropDownList>
    </form>
    

    在事件处理程序中根据所选列构建 updateStatement

    protected void test_SelectedIndexChanged(object sender, EventArgs e)
    {
        string updateStatement = String.Format("UPDATE Customer SET {0} = 'some value' WHERE {0} = 'some other value'", test.SelectedValue);
        //execute the updatestatement;
    }
    

    字符串初始化后更新语句字符串如下所示(如果选择了Col1):

    "UPDATE Customer SET Col1 = 'some value' WHERE Col1 = 'some other value'"
    

    编辑 2:好的,这是一个 winforms 应用程序中的示例

    首先,我在组合框中添加了一些值。接下来,当单击表单上的按钮时,我从组合框中获取所选值并将其用于动态 sql 字符串。它将为您提供与上述 asp.net 解决方案相同的结果。

    public Form1()
        {
            InitializeComponent();
            //add values to combobox from some datasource
            comboBox1.Items.Add("Col1");
            comboBox1.Items.Add("Col2");
            comboBox1.Items.Add("Col3");
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            string updateStatement = String.Format("UPDATE Customer SET {0} = 'some value' WHERE {0} = 'some other value'", comboBox1.SelectedItem);
            //execute the updatestatement;
        }
    

    【讨论】:

    • 感谢您的回复。我的客户使用的是 .NET 2.0,所以无法获得 ORM。好吧,在上面的代码 SET {0} 的 sn-p 中——它怎么知道 columnName,因为 ColumnName 将从 DropDownList 中选择,甚至在 where 子句中我如何给出 ColumnName。列名将由用户从 DropdownList 中动态选择..
    • 好吧,如果不看代码就很难判断你的程序是什么样子,但我假设下拉列表中的值是列名,所以当用户从 dll 中选择一个值时,会触发一些事件并在您的事件处理程序中,您可以根据他们在下拉菜单中选择的值构建动态 sql。
    • 嗨再次感谢您的回复。是的,当我从 ddl...
    • 我继续并用 ASP.NET 中的示例更新了我的答案
    • 我想在 UpdateButton_Click 事件处理程序中更新......所以我可以在那里使用那个 sn-p......我正在使用 ComboBox {Winforms} 所以它没有 ListItem,我试图使用它的 ITEM 属性,但它对我不起作用。我想让它更简单,而不是从 ComboBox 中获取 ColumnNam,而是让我获取一些 Label.Text 值....当我将 Label1.Text 作为 ColumnName 时,SQL Exceltion 被抛出,因为它没有被识别...... .. 任何选择.....如果我可以将至少 Label1.Text 值作为 ColumnName 传递,那对我来说已经足够了
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    相关资源
    最近更新 更多