我是C#的新手,我正在制作一个简单的数据库项目,其中我要添加一个功能,如果用户输入身份编号并单击按钮,所有记录将显示在列表框中...请帮助我完成它......我要添加的代码位于button2_click
代码的最后一部分。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace main_form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=windowDb;Integrated Security=True;Pooling=False");
SqlCommand command = new SqlCommand();
//SqlDataReader dataSearch;
private void Form1_Load(object sender, EventArgs e) {}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {}
private void btn_save_Click(object sender, EventArgs e)
{
if(id_Txt.Text!=""& f_txt.Text!=""& Lname_txt.Text!=""& m_txt.Text!=""& dateTimePicker1.Text!=""){
command.Connection = con;
con.Open();
command.CommandText = "insert into people(id,fname,lname,mobile,TDATE) values('"+id_Txt.Text+"','"+f_txt.Text+"','"+Lname_txt.Text+"','"+m_txt.Text+"','"+dateTimePicker1.Text/*Value.ToString()*/+"')";
command.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Saved Successfully");
id_Txt.Clear();
f_txt.Clear();
Lname_txt.Clear();
m_txt.Clear();
dateTimePicker1.Value = DateTime.Now;
search();
}
}
private void search() {
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
listBox4.Items.Clear();
listBox5.Items.Clear();
con.Open();
command.CommandText = "select * from people";
command.Connection = con;
SqlDataReader dataSearch = command.ExecuteReader();
if(dataSearch.HasRows){
while (dataSearch.Read())
{
listBox1.Items.Add(dataSearch["id"].ToString());
listBox2.Items.Add(dataSearch["fname"].ToString());
listBox3.Items.Add(dataSearch["lname"].ToString());
listBox4.Items.Add(dataSearch["mobile"].ToString());
listBox5.Items.Add(dataSearch["TDATE"].ToString());
}
}
con.Close();
}
private void btn_view_Click(object sender, EventArgs e)
{
search();
}
private void bn_update_Click(object sender, EventArgs e)
{
if (id_Txt.Text != "" & f_txt.Text != "" & Lname_txt.Text != "" & m_txt.Text != "" & dateTimePicker1.Text != "")
{
command.Connection = con;
con.Open();
command.CommandText = "update people set fname='" + f_txt.Text + "',lname='" + Lname_txt.Text + "',mobile='" + m_txt.Text + "',TDATE='" + dateTimePicker1.Text + "' where id='" +id_Txt.Text + "'";
command.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data updated Successfully");
id_Txt.Clear();
f_txt.Clear();
Lname_txt.Clear();
m_txt.Clear();
dateTimePicker1.Value = DateTime.Now;
search();
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox list = sender as ListBox;
if(list.SelectedIndex!=-1)
{
listBox1.SelectedIndex = list.SelectedIndex;
listBox2.SelectedIndex = list.SelectedIndex;
listBox3.SelectedIndex = list.SelectedIndex;
listBox4.SelectedIndex = list.SelectedIndex;
listBox5.SelectedIndex = list.SelectedIndex;
// retrive to TextBox of AllowDrop events;
/* id_Txt.Text = listBox1.SelectedIndex.ToString();
f_txt.Text = listBox2.SelectedIndex.ToString();
Lname_txt.Text = listBox3.SelectedIndex.ToString();
m_txt.Text = listBox4.SelectedIndex.ToString();
dateTimePicker1.Value = DateTime.Now;*/
id_Txt.Text = listBox1.SelectedItem.ToString();
f_txt.Text = listBox2.SelectedItem.ToString();
Lname_txt.Text = listBox3.SelectedItem.ToString();
m_txt.Text = listBox4.SelectedItem.ToString();
dateTimePicker1.Value = DateTime.Now;
}
}
private void btn_delete_Click(object sender, EventArgs e)
{
if (id_Txt.Text != "")
{
command.Connection = con;
con.Open();
command.CommandText = "delete from people where id='" + id_Txt.Text + "'";
command.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data deleted Successfully");
id_Txt.Clear();
f_txt.Clear();
Lname_txt.Clear();
m_txt.Clear();
dateTimePicker1.Value = DateTime.Now;
search();
}
}
private void label1_Click(object sender, EventArgs e) {}
private void button1_Click(object sender, EventArgs e) {}
private void button2_Click(object sender, EventArgs e)
{
if (text_search.Text == "")
{
MessageBox.Show("Enter the Rollno.");
}
else {
}
}
}
}