我的数据库包含员工ID,姓名,电子邮件地址和电话号码。我已经使用员工ID选择一个下拉列表,为了这个,我写了这个代码:通过从DropDownList中选择employee_id来获取员工信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Employeedetails : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=REVATI-PC;Initial Catalog=Test_Database;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter da;
string query;
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
query = "select Employee_ID from Employee";
cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr[0].ToString());
}
con.Close();
而是通过点击这些特定的ID,我没有得到其他信息。为此,我使用SelectIndexchanged
事件,写一个选择查询
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string query = "select Employee_ID from Employee where Employee_ID='" + DropDownList1.SelectedValue.ToString() + "'";
con.Open();
}
但它不工作
一点也没有” t看起来像你曾经在DropDownList1_SelectedIndexChanged()执行您的查询... –
[SQL注入警报](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29。 aspx) - 您应该不**将您的SQL语句连接在一起 - 使用**参数化查询**来避免SQL注入 –