我从我的代码中收到以下错误:
发生以下错误:
System.Data.OleDb.OleDbException (0x80040E07):条件表达式中的数据类型不匹配。在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) 在 System.Data。 OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteScalar() at NJDataExtractExceltoAccess.Form1.FindAcctId(String x , OleDbConnection y) 在 C:\Path\Form1.cs:line 158
private int FindAcctId(string x, OleDbConnection y)
{
//try
//{
string[] ID = x.Split('-');
string lookup = "Select Id from tbl_name where Col1 = '110' And Col2 = " + ID[0] + "And Col3 = " + ID[1] + "And Col4 = " + ID[2] + "And Col5 = " + ID[3] + "And Col6 = " + ID[4] + "And Col7 = " + ID[5] + "And Col7 = " + ID[6];
OleDbCommand cmd = new OleDbCommand(lookup, y);
Int32 result = (Int32)cmd.ExecuteScalar(); // line 158
Return_val = (int)result;
//}
//catch (Exception Er)
//{
// OutputBox.Text = "The following errors occurred: " + Er;
//}
return Return_val;
}
微软页面是这样说的:
典型的 ExecuteScalar 查询可以按照以下 C# 示例进行格式化:
cmd.CommandText = "SELECT COUNT(*) FROM dbo.region";
Int32 count = (Int32) cmd.ExecuteScalar();
我也试过:
int result = (int)cmd.ExecuteScalar();
和
object result = cmd.ExecuteScalar();
但我在同一行收到相同的数据类型错误。如何从对象ExecuteScalar()
上投射OleDbCommand
(连接和插入字符串工作)?