sql 查询和下拉列表
sql query and dropdownlist
我有一个下拉列表,其中包含列的 table 的值。
并且我在 c# 中有以下语句:
string raf = string.Format("select Id from Customer WHERE email="dropdownlist1");
如何将下拉列表的值分配给电子邮件?
试试这个
string raf = string.Format("select Id from Customer
WHERE email='{0}'",dropdownlist1.SelectedValue));
{0}
表示您正在获取 string.Format
方法的第一个参数
注意 SQL Injection
始终使用 SQL Parameters
您需要使用.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 Injection,使用如下参数化查询:-
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;
我有一个下拉列表,其中包含列的 table 的值。
并且我在 c# 中有以下语句:
string raf = string.Format("select Id from Customer WHERE email="dropdownlist1");
如何将下拉列表的值分配给电子邮件?
试试这个
string raf = string.Format("select Id from Customer
WHERE email='{0}'",dropdownlist1.SelectedValue));
{0}
表示您正在获取 string.Format
方法的第一个参数
注意 SQL Injection
始终使用 SQL Parameters
您需要使用.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 Injection,使用如下参数化查询:-
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;