You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TextChanged搜索功能异常及MySQL表不存在报错求助

问题排查:TextChanged搜索功能抛出MySQL表不存在异常

异常信息

Exception Unhandled...
MySql.Data.MySqlClient.MySqlException: 'Table 'bc-database.bdcb' doesn't exist'

异常调用栈

[External Code]
BusinessCardDB.frmMain.searchData(string) in frmMain.cs
BusinessCardDB.frmMain.txtSearch_TextChanged(object, System.EventArgs) in frmMain.cs
[External Code]
BusinessCardDB.Program.Main() in Program.cs

相关代码片段

private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e)
{
    StateTextboxes(true);
    btnUpdate.Enabled = true;
    btnDelete.Enabled = true;
    btnNew.Enabled = false;
    btnClear.Enabled = true;
    StateTextboxes(true);
    DataGridToTextBoxes();
}
private void DataGridToTextBoxes()
{
    txtID.Text = dataGridView.CurrentRow.Cells["id"].Value.ToString();
    id = Int32.Parse(txtID.Text);
    txtCompany.Text = dataGridView.CurrentRow.Cells["Company"].Value.ToString();
    txtFirstname.Text = dataGridView.CurrentRow.Cells["FirstName"].Value.ToString();
    txtLastname.Text = dataGridView.CurrentRow.Cells["LastName"].Value.ToString();
}
private void TextChanged_Search_Load(object sender, EventArgs e)
{
    searchData("");
} 
private void txtSearch_TextChanged(object sender, EventArgs e)
{
    searchData(txtSearch.Text);
}
private void searchData(string text)
{ 
    using (MySqlConnection mySqlConnection = new MySqlConnection(stdConnection))
    {
        mySqlConnection.Open();
        MySqlDataAdapter msda = new MySqlDataAdapter("BC_Search", mySqlConnection);
        msda.SelectCommand.CommandType = CommandType.StoredProcedure;
        msda.SelectCommand.Parameters.AddWithValue("_SearchValue", txtSearch.Text);

        DataTable dt = new DataTable();
        msda.Fill(dt);
        dataGridView.DataSource = dt;
        throw new NotImplementedException();
    }
} 

问题分析

  1. 核心异常原因:异常提示bc-database库中的bdcb表不存在,但你的C#代码调用的是存储过程BC_Search,说明存储过程内部的SQL语句引用了错误的表名(大概率是拼写错误,比如实际表名是business_cards或其他正确名称,也可能是大小写匹配问题——MySQL在区分大小写的系统中会识别错误)。
  2. 代码额外问题:
    • searchData方法定义了参数text,但实际使用的是txtSearch.Text,造成UI控件与业务逻辑耦合;
    • 方法末尾的throw new NotImplementedException();会强制抛出异常,即使查询成功也会中断流程。

解决方案

1. 修正存储过程中的表名

登录MySQL客户端,找到bc-database库中的BC_Search存储过程,将内部错误的表名bdcb替换为实际存在的业务表名。示例修正(假设正确表名为business_cards):

DELIMITER //
CREATE PROCEDURE BC_Search(_SearchValue VARCHAR(255))
BEGIN
    SELECT id, Company, FirstName, LastName 
    FROM business_cards  -- 替换为你的实际表名
    WHERE Company LIKE CONCAT('%', _SearchValue, '%') 
       OR FirstName LIKE CONCAT('%', _SearchValue, '%') 
       OR LastName LIKE CONCAT('%', _SearchValue, '%');
END //
DELIMITER ;

2. 修正C#代码中的问题

  • 使用方法参数text替代直接访问UI控件:
    msda.SelectCommand.Parameters.AddWithValue("_SearchValue", text);
    
  • 删除searchData方法末尾的throw new NotImplementedException();语句。

内容的提问来源于stack exchange,提问作者Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 14:22:52