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

C# WinForms读取Access数据库时出现参数未赋值错误

问题详情

我正在开发一款为公司生产的电脑生成序列号的简易程序,会将系统信息和序列号一同存储到Access文件中。目前程序能成功插入新数据行,但根据指定序列号查询数据库时,会抛出**"未为一个或多个参数提供值"**错误。

查询代码

private void btn_Search_Click(object sender, EventArgs e)
{
    string filePath = @"c:\serial\Settings.txt";

    try
    {
        // 检查文件是否存在
        if (File.Exists(filePath))
        {
            // 使用StreamReader读取第一行
            using (StreamReader reader = new StreamReader(filePath))
            {
                string dataBaseLocation = reader.ReadLine();
                //MessageBox.Show(dataBaseLocation);
                string newConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dataBaseLocation;
                // 从TextBox读取自增主键值,假设TextBox名为textBoxKey
                //MessageBox.Show(txtBox_SNLookUp.Text);
                if (long.TryParse(txtBox_SNLookUp.Text, out long keyValue))
                {
                    MessageBox.Show("The Value of keyValue is " + keyValue);
                    try
                    {
                        using (OleDbConnection newConnection = new OleDbConnection(newConnectionString))
                        {
                            newConnection.Open();

                            // 从新数据库的指定表中获取数据的SQL查询
                            string query = "SELECT * FROM [Serial Numbers] WHERE [Serial Numbers] = @Key";

                            MessageBox.Show(query);

                            using (OleDbCommand command = new OleDbCommand(query, newConnection))
                            {
                                // 为命令添加参数
                                command.Parameters.AddWithValue("@Key", keyValue);

                                OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                                DataTable serial_Data_Table = new DataTable();

                                 // 用新数据库的数据填充DataTable
                                adapter.Fill(serial_Data_Table); // 这是报错的第552行

                                // 将DataGridView的数据源设置为新的DataTable
                                dataGridView_SerialNumbers.DataSource = serial_Data_Table;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // 处理数据库操作中可能出现的异常
                        MessageBox.Show("Error: " + ex.Message);
                        MessageBox.Show(ex.ToString());
                        richTextBox_Software.Text = ex.ToString();
                    }
                }
                else
                {
                    MessageBox.Show("Invalid key value.");
                }
            }
        }
        else
        {
            Console.WriteLine("The file does not exist.");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error occurred look hard: " + ex.Message);
    }
}

数据库配置与排查情况

Access文件中,序列号被设置为自动递增主键。我已核对所有变量的准确性,看起来只有@Key参数的值无法正常传递。运行时通过弹窗验证了变量值,但错误依旧出现。

我尝试直接传入序列号而非变量,仍触发相同错误;已做异常捕获但未找到原因;也排查过空值或空参数问题。AI检查后认为代码语法和数据库配置均无问题,但仍会抛出以下异常。

完整调试错误信息

第一个异常

System.Data.OleDb.OleDbException (0x80040E10): No value given for one or more required parameters.
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at Serial_Numbers.SLT_SSI.btn_Search_Click(Object sender, EventArgs e) in C:\Users\ccran\OneDrive - Star Light Technologies inc\General\Star light tech\Serial Numbers\Serial Numbers\Serial Numbers\Form1.cs:line 552

第二个异常

System.InvalidOperationException
HResult=0x80131509
Message=Command parameter[0] '' data value could not be converted for reasons other than sign mismatch or data overflow.

Source=System.Data
StackTrace:
 at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
 at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
 at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
 at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
 at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
 at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
 at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
 at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
 at Serial_Numbers.SLT_SSI.btn_Search_Click(Object sender, EventArgs e) in C:\Users\ccran\OneDrive - Star Light Technologies inc\General\Star light tech\Serial Numbers\Serial Numbers\Serial Numbers\Form1.cs:line 552

Inner Exception 1:
OleDbException: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 07:17:06