OleDbCommand.ExecuteReader 抛出异常

发布于 2024-12-25 05:24:40 字数 4595 浏览 0 评论 0原文

我的 C# .NET 3.5 应用程序在执行 OleDbCommand.ExecuteReader 时引发异常。该应用程序对 Access 2007 数据库进行 5 次类似的查询。其中三个查询执行没有任何问题;另外两个导致抛出完全相同的异常。

这是一个传递的查询:

String query =
    "SELECT " +
        "bas.[BAS BACnet Object Type/Instance], " + // OBJECT_IDENTIFIER_ATTRIBUTE/ITEM_REFERENCE_ATTRIBUTE
        "bas.[BAS BACnet Object Name], " +          // USER_NAME_ATTRIBUTE
        "bas.[BAS Point List Description], " +      // DESCRIPTION_ATTRIBUTE
        "bas.[BAS Monitor Only], " +                // MONITOR_ONLY_ATTRIBUTE
        "ref.[ENUM_H], " +                          // PROPERTY_REFERENCE_VALUE_ATTRIBUTE
        "yk.[CCC Max Value (eng units)], " +        // MAX_PRESENT_VALUE_ATTRIBUTE
        "yk.[CCC Min Value (eng units)], " +        // MIN_PRESENT_VALUE_ATTRIBUTE
        "yk.[CCC Enum/Data Set], " +                // UNITS_ATTRIBUTE
        "ore.[ORE COV Increment], " +               // COV_INCREMENT_ATTRIBUTE
        "ore.[ORE Display Precision] " +            // DISPLAY_PRECISION_ATTRIBUTE
    "FROM (([OV2 BAS] AS bas " +
    "INNER JOIN [OV2 ORE] AS ore ON bas.[Ref ID] = ore.[Ref ID]) " +
    "INNER JOIN [OV2 RefID] AS ref ON bas.[Ref ID] = ref.[Ref ID]) " +
    "INNER JOIN [YK CAPP] AS yk ON bas.[Ref ID] = yk.[Ref ID] " +
    "WHERE bas.[BAS BACnet Object Type/Instance] LIKE 'AV%';";

this.RunQuery(query, MappingTable.AV_QUERY_IP_FIELDS, this.IPAnalogValuesList);

这是一个抛出异常的查询:

String query =
    "SELECT " +
        "bas.[BAS BACnet Object Type/Instance], " + // OBJECT_IDENTIFIER_ATTRIBUTE/ITEM_REFERENCE_ATTRIBUTE
        "bas.[BAS BACnet Object Name], " +          // USER_NAME_ATTRIBUTE
        "bas.[BAS Point List Description], " +      // DESCRIPTION_ATTRIBUTE
        "bas.[BAS Monitor Only], " +                // MONITOR_ONLY_ATTRIBUTE
        "ref.[ENUM_H], " +                          // PROPERTY_REFERENCE_VALUE_ATTRIBUTE
        "ses.[ENUM_H], " +                          // STATES_TEXT_ATTRIBUTE
        "ore.[ORE States] " +                       // ACTIVE_TEXT_ATTRIBUTE/INACTIVE_TEXT_ATTRIBUTE
    "FROM (([OV2 BAS] AS bas " +
    "INNER JOIN [OV2 RefID] AS ref ON bas.[Ref ID] = ref.[Ref ID]) " +
    "INNER JOIN [OV2 ORE] AS ore ON bas.[Ref ID] = ore.[Ref ID]) " +
    "INNER JOIN [StatesEnumSet] AS ses ON ore.[ORE States] = ses.[ID] " +
    "WHERE bas.[BAS BACnet Object Type/Instance] LIKE 'BV%';";

this.RunQuery(query, MappingTable.BV_QUERY_FIELDS, this.BinaryValuesList);

以下是所有查询的执行方式:

private void RunQuery(String query, Int32 fieldCount, Object target)
{
    OleDbCommand cmd = null;
    OleDbDataReader reader = null;

    try
    {
        OleDbConnectionStringBuilder connection = new OleDbConnectionStringBuilder();
        connection.Provider = "Microsoft.ACE.OLEDB.12.0";
        connection.DataSource = XML_Generator.Program.MappingTableFilename;
        connection.PersistSecurityInfo = false;

        this.DbConnection = new OleDbConnection(connection.ToString());
        this.DbConnection.Open();

        using (cmd = new OleDbCommand(query, this.DbConnection))
        {
            cmd.Connection = this.DbConnection;
            cmd.CommandText = query;
            cmd.CommandType = System.Data.CommandType.Text;
            reader = cmd.ExecuteReader(); // <== Exception is thrown here.

            while (reader.Read())
            {
                . . .
            }
        }

        if (this.DbConnection != null)
        {
            this.DbConnection.Close();
            this.DbConnection = null;
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

这是异常:

System.Data.OleDb.OleDbException:没有为一个或多个必需参数指定值 之三。 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResul 小时) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARA MS dbParams、对象&执行结果) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior 行为,Ob 项目&执行结果) 在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为 ior,字符串方法) 在 System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior 行为) 在 System.Data.OleDb.OleDbCommand.ExecuteReader() 在 XML_Generator.MappingTable.RunQuery(字符串查询,Int32 fieldCount,对象 目标)位于 C:\ccmdb\prep\ov2_shared_cec_v1.0\ov2_shared_cec\Private\Tools\XML_G enerator\XML_Generator\MappingTable.cs:第 312 行

感谢任何帮助。谢谢。

My C# .NET 3.5 application is throwing an unusual exception when executing OleDbCommand.ExecuteReader. The application makes 5 similar queries of an Access 2007 database. Three of the queries execute without any problems; the other 2 cause exactly the same exception to be thrown.

This is a query that passes:

String query =
    "SELECT " +
        "bas.[BAS BACnet Object Type/Instance], " + // OBJECT_IDENTIFIER_ATTRIBUTE/ITEM_REFERENCE_ATTRIBUTE
        "bas.[BAS BACnet Object Name], " +          // USER_NAME_ATTRIBUTE
        "bas.[BAS Point List Description], " +      // DESCRIPTION_ATTRIBUTE
        "bas.[BAS Monitor Only], " +                // MONITOR_ONLY_ATTRIBUTE
        "ref.[ENUM_H], " +                          // PROPERTY_REFERENCE_VALUE_ATTRIBUTE
        "yk.[CCC Max Value (eng units)], " +        // MAX_PRESENT_VALUE_ATTRIBUTE
        "yk.[CCC Min Value (eng units)], " +        // MIN_PRESENT_VALUE_ATTRIBUTE
        "yk.[CCC Enum/Data Set], " +                // UNITS_ATTRIBUTE
        "ore.[ORE COV Increment], " +               // COV_INCREMENT_ATTRIBUTE
        "ore.[ORE Display Precision] " +            // DISPLAY_PRECISION_ATTRIBUTE
    "FROM (([OV2 BAS] AS bas " +
    "INNER JOIN [OV2 ORE] AS ore ON bas.[Ref ID] = ore.[Ref ID]) " +
    "INNER JOIN [OV2 RefID] AS ref ON bas.[Ref ID] = ref.[Ref ID]) " +
    "INNER JOIN [YK CAPP] AS yk ON bas.[Ref ID] = yk.[Ref ID] " +
    "WHERE bas.[BAS BACnet Object Type/Instance] LIKE 'AV%';";

this.RunQuery(query, MappingTable.AV_QUERY_IP_FIELDS, this.IPAnalogValuesList);

This is a query that throws throws an exception:

String query =
    "SELECT " +
        "bas.[BAS BACnet Object Type/Instance], " + // OBJECT_IDENTIFIER_ATTRIBUTE/ITEM_REFERENCE_ATTRIBUTE
        "bas.[BAS BACnet Object Name], " +          // USER_NAME_ATTRIBUTE
        "bas.[BAS Point List Description], " +      // DESCRIPTION_ATTRIBUTE
        "bas.[BAS Monitor Only], " +                // MONITOR_ONLY_ATTRIBUTE
        "ref.[ENUM_H], " +                          // PROPERTY_REFERENCE_VALUE_ATTRIBUTE
        "ses.[ENUM_H], " +                          // STATES_TEXT_ATTRIBUTE
        "ore.[ORE States] " +                       // ACTIVE_TEXT_ATTRIBUTE/INACTIVE_TEXT_ATTRIBUTE
    "FROM (([OV2 BAS] AS bas " +
    "INNER JOIN [OV2 RefID] AS ref ON bas.[Ref ID] = ref.[Ref ID]) " +
    "INNER JOIN [OV2 ORE] AS ore ON bas.[Ref ID] = ore.[Ref ID]) " +
    "INNER JOIN [StatesEnumSet] AS ses ON ore.[ORE States] = ses.[ID] " +
    "WHERE bas.[BAS BACnet Object Type/Instance] LIKE 'BV%';";

this.RunQuery(query, MappingTable.BV_QUERY_FIELDS, this.BinaryValuesList);

Here is how all of the queries are executed:

private void RunQuery(String query, Int32 fieldCount, Object target)
{
    OleDbCommand cmd = null;
    OleDbDataReader reader = null;

    try
    {
        OleDbConnectionStringBuilder connection = new OleDbConnectionStringBuilder();
        connection.Provider = "Microsoft.ACE.OLEDB.12.0";
        connection.DataSource = XML_Generator.Program.MappingTableFilename;
        connection.PersistSecurityInfo = false;

        this.DbConnection = new OleDbConnection(connection.ToString());
        this.DbConnection.Open();

        using (cmd = new OleDbCommand(query, this.DbConnection))
        {
            cmd.Connection = this.DbConnection;
            cmd.CommandText = query;
            cmd.CommandType = System.Data.CommandType.Text;
            reader = cmd.ExecuteReader(); // <== Exception is thrown here.

            while (reader.Read())
            {
                . . .
            }
        }

        if (this.DbConnection != null)
        {
            this.DbConnection.Close();
            this.DbConnection = null;
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

This is the exception:

System.Data.OleDb.OleDbException: No value given for one or more required parame
ters.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResul
t hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARA
MS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Ob
ject& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behav
ior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteReader()
at XML_Generator.MappingTable.RunQuery(String query, Int32 fieldCount, Object
target) in C:\ccmdb\prep\ov2_shared_cec_v1.0\ov2_shared_cec\Private\Tools\XML_G
enerator\XML_Generator\MappingTable.cs:line 312

Any help is appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街女流氓 2025-01-01 05:24:40

以下是可以声明 OleDbConnection、Command 和 DataReader 对象的示例
如果您选择将其设为静态变量,则连接字符串的外观示例

<add key="strAccessConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\castonmr\Documents\"Your AccessDB Name.mdb";Mode='Share Exclusive';Jet OLEDB:Database Password="your password";"/>    


try
{
    OleDbConnection oleconn = null;
    OleDbDataReader reader = null;
    oleconn = new OleDbConnection(strAccessConnectionString);
    oleconn.Open();
    using (OleDbCommand cmd = new OleDbCommand())
    {
       cmd.Connection = oleconn;
       cmd.CommandText = query;
       cmd.CommandType = CommandType.Text;
       reader = cmd.ExecuteReader();
    }
}//try
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

here is an example of what a OleDbConnection, Command and DataReader object can be declared
example of how the Connection String should look if you choose to make this a static variable

<add key="strAccessConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\castonmr\Documents\"Your AccessDB Name.mdb";Mode='Share Exclusive';Jet OLEDB:Database Password="your password";"/>    


try
{
    OleDbConnection oleconn = null;
    OleDbDataReader reader = null;
    oleconn = new OleDbConnection(strAccessConnectionString);
    oleconn.Open();
    using (OleDbCommand cmd = new OleDbCommand())
    {
       cmd.Connection = oleconn;
       cmd.CommandText = query;
       cmd.CommandType = CommandType.Text;
       reader = cmd.ExecuteReader();
    }
}//try
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
霓裳挽歌倾城醉 2025-01-01 05:24:40

有人重命名了数据库中的字段之一。 -_-

Someone renamed one of the fields in the database. -_-

寄人书 2025-01-01 05:24:40

此错误可能是因为您的 mdb 没有运行查询所需的所有列/对象,您可以仔细检查 mdb 文件并查看查询是否在那里正确运行

this error could be because your mdb doesnot have all the columns/object required to run query, you may double check mdb file and see if the query run there properly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文