我的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:没有给出一个或多个必需参数的值 TER值。 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResul) 小时) 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARA) MS dbParams,Object&amp;的ExecuteReuslt) 在System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&amp; executeResult) 在System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior,Ob JECT&安培;的ExecuteReuslt) 在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behaviorv) ior,String方法) 在System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior行为) 在System.Data.OleDb.OleDbCommand.ExecuteReader() 在XML_Generator.MappingTable.RunQuery(String query,Int32 fieldCount,Object 目标)在C:\ ccmdb \ prep \ ov2_shared_cec_v1.0 \ ov2_shared_cec \ Private \ Tools \ XML_G中 enerator \ XML_Generator \ MappingTable.cs:第312行
感谢任何帮助。感谢。
答案 0 :(得分:1)
这是一个可以声明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);
}
答案 1 :(得分:1)
有人重命名了数据库中的一个字段。 -_-
答案 2 :(得分:0)
此错误可能是因为您的mdb没有运行查询所需的所有列/对象,您可以仔细检查mdb文件并查看查询是否正常运行