将数据库连接到C#.net中的下拉列表 [英] Connect database to Drop down list in C# .net

查看:97
本文介绍了将数据库连接到C#.net中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows应用程序的C#中有一个表单

登录:
密码:
国家:
状态:


所以我想在下拉列表中显示国家/地区名称...基于选择的国家/地区,我想在下拉列表中显示国家/地区从数据库中获取状态...
我该怎么办?

以及如何避免登录页面的sql注入?

i have a form in windows application in c#

login :
password:
country:
state:


so i want to display country names in drop downlist...based upon the selection of country i wanna display states in dropdown list fetch from database...
how can i do that?

and also how can i avoid sql injection for login page ?

推荐答案

使用
从数据库中获取国家/地区名称
从表名中选择国家名


使用上面的查询填充数据集/数据表,然后将此数据集/数据表放置到下拉列表中.


对于状态名称,请使用下面显示的查询填充另一个数据集.


从表名中选择州名,其中countryname =''"+ dropdownlist.Text +"


并将此数据集作为状态下拉列表的数据源.


要进行SQL注入,您可以编写一条语句以在执行查询之前将''替换为,这也将防止SQL注入.


注意:在上面的示例中,例如,查询只是为了向您提供有关实现的概述,请根据您的应用程序引用正确的表名.




希望这会对您有所帮助.
fetch the country name from database using

select countryname from tablename


Fill a dataset/data table with the above query , place this dataset/data table a datasorce to the dropdown list.


for the name of a state , fill another dataset with the query shown below.


select statename from tablename where countryname=''"+dropdownlist.Text+"


and place this dataset as datasource for state dropdown list.


to prvent SQL injection you can write a statement to replace '' with ", before executing the query, this will prevent SQL injection too.


Note: here in above eg the queries are just written to give you an overview about the implementation, refer correct table names according to your application.




Hope this will help you out.


这篇关于将数据库连接到C#.net中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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