简体   繁体   English

使用Microsoft Access数据库引擎在Visual c#中评估Access计算字段时,数据类型不匹配

[英]Data Type Mismatch when evaluating Access Calculated Fields in Visual c# using Microsoft Access Database Engine

I have a OleDbCommand in a Visual Studios c# windows forms project, and I am trying to select the name of every item in my Access Table Stock where the value of a calculated field in that table is less than one. 我在Visual Studios c#Windows窗体项目中有一个OleDbCommand,并且尝试选择Access Table Stock中每个项目的名称,其中该表中计算字段的值小于一。 The result type of the calculated field in Access is set to decimal, and the code looks as if it should work, but for whatever reason it doesn't. Access中计算字段的结果类型设置为十进制,并且代码看起来像应该工作,但是由于某种原因它不起作用。 Could you help me? 你可以帮帮我吗?

Here is my code: 这是我的代码:

        loginForm.connection.Open();
        stockLowString = "";
        var checkStockLowCommand = new OleDbCommand("SELECT stockName FROM Stock WHERE (stockLowCalculation < '" + Convert.ToDecimal(1) + "')",loginForm.connection);
        OleDbDataReader checkStockLowReader = checkStockLowCommand.ExecuteReader();
        while (checkStockLowReader.Read())
        {
            stockLowString = stockLowString + checkStockLowReader.GetString(0) + " ";
        }
        if (stockLowString != "")
        {
            MessageBox.Show("There are some Stock Items that are low, these are" + Environment.NewLine + stockLowString);
        }
        loginForm.connection.Close();

The error occurs on the line 错误发生在网上

OleDbDataReader checkStockLowReader = checkStockLowCommand.ExecuteReader(); OleDbDataReader checkStockLowReader = checkStockLowCommand.ExecuteReader();

Thanks in advance for your help. 在此先感谢您的帮助。

Problem Solved, or at least avoided. 问题已解决,或至少避免了。 I just put the calculation in the Command rather than use a calculated field. 我只是将计算放在命令中,而不是使用计算字段。 The question is still valid though, as I didn't solve it. 这个问题仍然有效,因为我没有解决。

Open the database and check the type of the field "stockLowCalculation" it's most likely not decimal.. I suggest you rework your query and make it parametrized. 打开数据库并检查“ stockLowCalculation”字段的类型(很可能不是十进制)。我建议您对查询进行重新处理并对其进行参数化。 This way you would evade most of the possible data type mismatch errors. 这样,您可以避免大多数可能的数据类型不匹配错误。

string conS ="..."; // connection string
var param = 1;
using (var connection = new OleDbConnection(conS))
{
       string queryString = "SELECT stockName FROM Stock WHERE stockLowCalculation < @var"
       var cmd = new OleDbCommand(queryString, connection);
       cmd.Parameters.Add(new OleDbParameter("@var", param));
       connection.Open();
       OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);                    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM