Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在更新查询中使用组合框文本_C# - Fatal编程技术网

C# 如何在更新查询中使用组合框文本

C# 如何在更新查询中使用组合框文本,c#,C#,请告诉我如何将组合框文本作为列名 这是我的密码: string query = "update teacher set '"+comboBox1.Text+"' = '" + textBox2.Text + "' where teacherid='" + textBox1.Text + "'"; SqlDataAdapter sda = new SqlDataAdapter(query, conn); sda.SelectCommand.ExecuteNonQuery(); 我想使用combo

请告诉我如何将组合框文本作为列名

这是我的密码:

string query = "update teacher set '"+comboBox1.Text+"' = '" + textBox2.Text + "' where teacherid='" + textBox1.Text + "'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
sda.SelectCommand.ExecuteNonQuery();
我想使用combobox的文本作为更新查询中的一个设置列名,它给出了错误

comboBox1.selectedItem.Text 
试试这个

comboBox1.selectedItem.Text 

您可以尝试使用以下代码

string abc = comboBox1.SelectedItem.ToString();
MessageBox.Show(abc);
因此,您的代码应该如下所示:

string query = "update teacher set '" + comboBox1.SelectedItem.ToString() + "' = '" + textBox2.Text + "' where teacherid='" + textBox1.Text + "'";
此外,您还可以查看下面的文档文章,了解有关组合框项目选择的更多信息


您可以尝试使用以下代码

string abc = comboBox1.SelectedItem.ToString();
MessageBox.Show(abc);
因此,您的代码应该如下所示:

string query = "update teacher set '" + comboBox1.SelectedItem.ToString() + "' = '" + textBox2.Text + "' where teacherid='" + textBox1.Text + "'";
此外,您还可以查看下面的文档文章,了解有关组合框项目选择的更多信息


请尝试以下代码:

string query = "update teacher set '"+comboBox1.SelectedItem.ToString(+"' = '" + textBox2.Text + "' where teacherid='" + textBox1.Text + "'"; 

SqlDataAdapter sda = new SqlDataAdapter(query, conn);
sda.SelectCommand.ExecuteNonQuery();

请尝试以下代码:

string query = "update teacher set '"+comboBox1.SelectedItem.ToString(+"' = '" + textBox2.Text + "' where teacherid='" + textBox1.Text + "'"; 

SqlDataAdapter sda = new SqlDataAdapter(query, conn);
sda.SelectCommand.ExecuteNonQuery();

comboBox1.selecteditem以获取文本。请先输入值,如热码,然后检查其是否工作。如果它工作,然后使用ComboBox检查是否包含值,那么您将知道问题出在哪里。您永远不应根据原始用户输入构造SQL查询。现在你很容易受到攻击。想象一下,如果
textBox1.Text
包含
42';落桌老师--
?comboBox1.选择EdItem以获取文本。请先输入值,就像热代码一样使用一些值,然后检查其是否工作。如果它工作,然后使用ComboxBox检查是否包含值,那么您将知道问题在哪里您永远不应该从原始用户输入构造SQL查询。现在你很容易受到攻击。想象一下,如果
textBox1.Text
包含
42';落桌老师--