数据库访问搜索文本框

本文关键字:文本 搜索 访问 数据库 | 更新日期: 2023-09-27 18:03:11

StudentID > Number
StudentName > Text
StudentCNIC > Text
StudentDOB > Date/Time

我试图创建一个搜索文本框,其中的结果显示在文本框。我有一个命名为finddbtn的按钮,其中用户输入学生id,学生名或学生CNCI(只是疼痛数字)。然后结果显示在文本框中…studdtb(显示学生ID)、studnamethb(显示学生姓名)、StudCNCITb(显示学生CNCI)和StudDOBTb(显示学生DOB)。

我在互联网上看到的很多例子都使用gridview,但我使用文本框来显示结果。我感兴趣的是……

这里这里这里这里和这里

    public Form1()
    {
        InitializeComponent();
        //Connection String for Access 2003. 
        myCon = new OleDbConnection(@" Provider=Microsoft.Jet.OLEDB.4.0;Data 
        Source=C....'StudDB.mdb ");
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        this.studentsTableAdapter.Fill(this.studDBDataSet.Students);
    }

在FindBtn函数中完成以下操作....

    private void FindBtn_Click(object sender, EventArgs e)
    {
        string title = textBox1.Text.ToString();
        if (title != "")
        {
            string queryString = "SELECT * FROM Students + title;
            //I need help here
            StudIDTb.Text = queryString;
            StudNameTb.Text = queryString;
            StudCNCITb.Text = queryString;
            StudDOBTb.Text = queryString;
        }
        else
            MessageBox.Show("Please try again'nError: ");
    }

结果显示....

SELECT *
FROM Students 103

中的每个文本框和exe冻结,所以很明显这是错误的。请问这里有人能帮我吗,提前谢谢。

更新1 我已经包括OleDbCommand, CommandType和打开连接,就在字符串变量之前,防止屏幕冻结,但它并没有解决问题

更新2 找到了我喜欢的东西,但是不能用

更新3

作为C Sharp Corner,请提供一段代码....

 private void btnFind_Click(object sender, EventArgs e)
    {
        string title = textBox1.Text.ToString();
        string queryString = "SELECT * FROM Students" + title;
        OleDbCommand command = new OleDbCommand();
        command.CommandText = queryString;
        command.Connection = myCon;
        myCon.Open();
        OleDbDataReader dr = command.ExecuteReader();
        while (dr.Read())
        {
             StudIDTb.Text += String.Format("Student ID:                
             {0}'n",dr["StudID"].ToString());
            StudNameTb.Text += String.Format("StudentName ID: {0}'n", 
            dr["StudentName"].ToString());
            StudCNCITb.Text += String.Format("StudentCNIC: {0}'n", 
            dr["StudentCNIC"].ToString());
            StudDOBTb.Text += String.Format("StudentCNIC: {0}'n", 
            dr["StudentCNIC"].ToString());
        }
        myCon.Close();

修改格式后出现错误....

" Microsoft Jet数据库引擎找不到输入表或查询"Students101"。

我现在正在看它,看看它是什么意思

数据库访问搜索文本框

这里有一个应该工作的例子(如果你包括oledbcommand, commandtype等也更新你的代码,这样我们可以看到你改变了什么):

OleDbCommand command = new OleDbCommand();
command.CommandText = queryString;
command.Connection = myCon;
myCon.Open();
OleDbDataReader dr = command.ExecuteReader();
while(dr.Read())
{
   StudIDTb.Text += dr["StudID"].ToString();
   StudNameTb.Text += dr["StudName"].ToString();
   StudCNCITb.Text += dr["StudCNCI"].ToString();
   StudDOBTb.Text += dr["StudDOB"].ToString();
}
myCon.Close();