如何在c#中搜索数据库中的记录

问题描述:

您好我正在使用sql在c#中创建数据库应用程序。我有一个名为Teachers的表单,其中包含一个名为Teacher的数据库表。我想使用教师表格从数据库表教师中搜索记录。在教师表单中,我使用的是conboBox,TextBox和Button。因此,当我从组合框中选择一个项目并输入其相应的文本并按下一个按钮时,它应根据我的搜索给出结果。最初我使用的是datagridview,但我不想使用datagridview,但我想使用listbox,列表框应该与其相应的文本框绑定。

这是我试过的:

hello i am making a database application in c# using sql. i have a form called Teachers with a database table called Teacher. I want to search records from the database table "Teacher" using the form Teachers. In the Teachers Form I am using a conboBox,TextBox and Button. so that when i select an item from the comboBox and enter its corresponding text and press a button it should give a results based on my search. Initially i was using datagridview but i dont want to use datagridview but i want to use listbox and the listbox should bind with its corresponding textboxes.
this is what i have tried :

string str = (@"Data Source=BROTHER-PC;Initial Catalog=iEMYS;Integrated Security=True");


            SqlConnection con = new SqlConnection(str);
            if (comboBox1.SelectedIndex == 0)
            {
                try
                {
                    string searchtext = textBox1.Text;
                    SqlCommand cmd = new SqlCommand(
                        "select  * from teacher where TeacherId like '%" + searchtext + "%'", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    DataSet ds = new DataSet();
                    da.Fill(ds, "teacher");
                    teacherDataGridView.DataSource = ds.Tables["teacher"];
                    //listBox1.DataSource = ds.Tables["teacher"];
                    load_listBox1();
                    if (teacherDataGridView.RowCount == 0)
                    {

                        MessageBox.Show("Teacher ID was Not Found. Try Another!");


                    }
                }
                catch (Exception)
                {

                }
            }

            else if (comboBox1.SelectedIndex == 1)
            {

                try
                {
                    string searchtext = textBox1.Text;
                    SqlCommand cmd = new SqlCommand(
                        "select  * from teacher where EMYSNO like '%" + searchtext + "%'", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    DataSet ds = new DataSet();
                    da.Fill(ds, "teacher");
                    teacherDataGridView.DataSource = ds.Tables["teacher"];
                    load_listBox1();
                    //listBox1.DataSource = ds.Tables["teacher"];
                    if (teacherDataGridView.RowCount == 0)
                    {

                        MessageBox.Show("EMYSNO was Not Found. Try Another!");


                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (comboBox1.SelectedIndex == 2)
            {

                try
                {
                    string searchtext = textBox1.Text;
                    SqlCommand cmd = new SqlCommand(
                        "select  * from teacher where [First Name] like '%" + searchtext + "%'", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    DataSet ds = new DataSet();
                    da.Fill(ds, "teacher");
                    teacherDataGridView.DataSource = ds.Tables["teacher"];
                    //listBox1.DataSource = ds.Tables["teacher"];
                    load_listBox1();
                    if (teacherDataGridView.RowCount == 0)
                    {

                        MessageBox.Show("First Name was Not Found. Try Another!");


                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (comboBox1.SelectedIndex == 3)
            {

                try
                {
                    string searchtext = textBox1.Text;
                    SqlCommand cmd =
                        new SqlCommand("select  * from teacher where [Last Name] like '%" + searchtext + "%'",
                            con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    DataSet ds = new DataSet();
                    da.Fill(ds, "teacher");
                    teacherDataGridView.DataSource = ds.Tables["teacher"];
                    //listBox1.DataSource = ds.Tables["teacher"];
                    load_listBox1();
                    if (teacherDataGridView.RowCount == 0)
                    {

                        MessageBox.Show("Last Name was Not Found. Try Another!");


                    }
                }
                catch (Exception ex)
                {
                }
            }

首先根据以下方式创建存储过程:

First of all create Store Procedure as per follow :
Create Procedure spSelectAllTechers
@search varchar(100)
AS
BEGIN

SELECT * FROM Teacher
 WHERE name like '%'+@search+'%'

END





然后将下面的代码写入你的班级:



Then Write Below code to your class:

SqlCommand cmd = new SqlCommand("spSelectAllTechers",con);
cmd.CommandType = CommandType.StoredProcedure;

SqlCmd.Parameters.Add("@search",SqlDbType.VarChar).Value = txtSearch.Text;
con.Open();
SqlDataReader sqlRdr = cmd.ExecuteReader();

While(sqlRdr.Read())
{
     this.name = Convert.ToString(sqlRdr["name"]);
//Write here....
//Add to list and bind it with gridview
}