如何更改C#中每个组合框项的值? [英] How do I change the value of each combobox items in C#?

查看:119
本文介绍了如何更改C#中每个组合框项的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助才能在MS Access数据库中获取数字输出。那个comboBOx项目是字符。



我尝试过:



  private   void  comboBox1_SelectedIndexChanged_1(对象发​​件人,EventArgs e)
{
如果(comboBox1.Text == Mr){comboBox1.SelectedValue = 1; }
if (comboBox1.Text == 太太){comboBox1.SelectedValue = 2; }
}

private void button1_Click( object sender,EventArgs e)
{


try
{
OleDbConnection con = new OleDbConnection( @ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\ dropdown.accdb; Persist Security Info = True);
OleDbCommand cmd = con.CreateCommand();
con.Open();
cmd.CommandText = INSERT INTO下拉列表([AS_TITLE],[Name])VALUES(' + comboBox1.SelectedValue + ',' + textBox1.Text + ');

cmd.Connection = con;
cmd.ExecuteNonQuery();



MessageBox.Show( 已成功上传);
con.Close();

}


catch (例外ee)
{
MessageBox .Show(ee.Message);
}

解决方案

不是您问题的解决方案,而是您遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

Need help to get numeric output in MS Access database. That comboBOx items are characters.

What I have tried:

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Mr") { comboBox1.SelectedValue = "1"; }
            if (comboBox1.Text == "Mrs") { comboBox1.SelectedValue = "2"; }
        } 

private void button1_Click(object sender, EventArgs e)
        {
            
           
            try
            {
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\dropdown.accdb; Persist Security Info = True");
                OleDbCommand cmd = con.CreateCommand();
                con.Open();
                cmd.CommandText = "INSERT INTO dropdown([AS_TITLE],[Name]) VALUES ('" + comboBox1.SelectedValue + "','" + textBox1.Text + "')";
                
                cmd.Connection = con;
                cmd.ExecuteNonQuery();

                
                
            MessageBox.Show("Successfully Uploaded");
                con.Close();
                
            }


            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

解决方案

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]


这篇关于如何更改C#中每个组合框项的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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