SQL 查询和下拉列表
本文关键字:下拉列表 查询 SQL | 更新日期: 2023-09-27 18:37:04
我有一个下拉列表,其中包含列表的值。
我在 c# 中有以下语句:
string raf = string.Format("select Id from Customer WHERE email="dropdownlist1");
如何将下拉列表的值分配给电子邮件?
您需要
使用.SelectedValue
属性来获取下拉列表的值:-
string raf = string.Format("select Id from Customer WHERE email={0}",
dropdownlist1.SelectedValue);
用于获取下拉文本:-
string raf = string.Format("select Id from Customer WHERE email={0}",
dropdownlist1.SelectedItem.Text);
另外,请注意,使用 String.Format
时,您需要一个像 {0}
这样的占位符。
虽然根据您的查询,您主要命中数据库,因此请注意SQL注入,请使用如下参数化查询:-
string raf = select Id from Customer WHERE email=@DropdownText;
SqlCommand cmd = new SqlCommand(raf,conn);
cmd.Parameters.Add("@DropdownText",SqlDbType.NVarchar,20).Value =
dropdownlist1.SelectedItem.Text;
试试这个
string raf = string.Format("select Id from Customer
WHERE email='{0}'",dropdownlist1.SelectedValue));
{0}
表示您正在获取string.Format
方法的第一个参数
当心SQL Injection
始终使用 SQL 参数