认为
cmd.Parameters.AddWithValue("@name", listView1.SelectedItems );处有问题 有人对此有想法吗?
private void Form_Load(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(tools.ConnectionString);
SqlCommand cmd = new SqlCommand("select * from Employees",cnn);
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
ListViewItem lvi = new ListViewItem();
lvi.Text = dr["FirstName"].ToString();
lvi.SubItems.Add(dr["LastName"].ToString());
listView1.Items.Add(lvi);
}
cnn.Close();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(tools.ConnectionString);
SqlCommand cmd = new SqlCommand("select EmployeeId,BirthDate from Employees where FirstName = @name ",cnn);
cmd.Parameters.AddWithValue("@name", listView1.SelectedItems );
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
MessageBox.Show("Id= "+dr["EmployeeID"].ToString() + "\nBirth Date= "+dr["BirthDate"].ToString());
}
cnn.Close();
}
谢谢