如何用数据库数据填充组合框

编程入门 行业动态 更新时间:2024-10-12 18:22:39
本文介绍了如何用数据库数据填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Hello Guys 你可以帮我吗 我尝试过的事情:

private void button1_Click(object sender,EventArgs e) { { comboBox2.Enabled = true; string klijent = comboBox1.Text; CON.Open(); string sqlquery =SELECT Naziv FROM [Dobavljaci_+ klijent +] WHERE NazivFirme ='+ klijent +'; SqlCommand cmd = new SqlCommand(sqlquery,CON); SqlDataAdapter da = new SqlDataAdapter(sqlquery,CON); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteNonQuery(); comboBox2.DisplayMember =Naziv; comboBox2.ValueMember =Naziv; comboBox2.DataSource = ds; } { string klijent = comboBox1.Text; string sqlquery1 =SELECT stopa FROM NKlijent WHERE NazivFirme ='+ klijent +'; CMD = new SqlCommand(sqlquery1,CON); SqlDataReader sdr1 = CMD.ExecuteReader(); while(sdr1.Read()) { label1.Text = sdr1 [stopa]。ToString(); } CON.Close(); }

在Label7上我从数据库获得价值,但在组合框上我得到了这个

System.Data。 DataViewManagerListItemTypeDescriptor

解决方案

尝试

comboBox2.DataSource = ds.Tables [0] ;

注意: 格式化sql查询字符串是易受攻击到 SQL注入 [ ^ ]攻击 总是使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]

不是你问题的解决方案,而是你遇到的另一个问题。 永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。 名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。 SQL注入 - 维基百科 [ ^ ] SQL注入 [ ^ ]

Hello Guys can you help me with this What I have tried:

private void button1_Click(object sender, EventArgs e) { { comboBox2.Enabled = true; string klijent = comboBox1.Text; CON.Open(); string sqlquery = "SELECT Naziv FROM [Dobavljaci_" + klijent + "] WHERE NazivFirme = '" + klijent + "'"; SqlCommand cmd = new SqlCommand(sqlquery, CON); SqlDataAdapter da = new SqlDataAdapter(sqlquery, CON); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteNonQuery(); comboBox2.DisplayMember = "Naziv"; comboBox2.ValueMember = "Naziv"; comboBox2.DataSource = ds; } { string klijent = comboBox1.Text; string sqlquery1 = "SELECT stopa FROM NKlijent WHERE NazivFirme='" + klijent + "'"; CMD = new SqlCommand(sqlquery1, CON); SqlDataReader sdr1 = CMD.ExecuteReader(); while (sdr1.Read()) { label1.Text = sdr1["stopa"].ToString(); } CON.Close(); }

On Label7 i am getting value from data base but on combobox I am getting this

System.Data.DataViewManagerListItemTypeDescriptor

解决方案

try

comboBox2.DataSource = ds.Tables[0];

Note: Formatting the sql Query string is vulnerable to SQL Injection[^] attacks always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

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[^]

更多推荐

如何用数据库数据填充组合框

本文发布于:2023-10-26 17:45:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   如何用   数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!