从 OleDb 数据库检索数据时出错

Error while retrieving data from OleDb database

我正在为我编写此代码的银行项目工作 但是,对于 Deposit/Withdrawal 事务,我收到错误消息

Error 1 'string' does not contain a definition for 'getDouble' and no extension method 'getDouble' accepting a first argument of type 'string' could be found

Error 2 The name 'result' does not exist in the current context

您似乎正试图从 table 获取余额值。方法是

        balance = Convert.ToInt32(df.ExecuteScalar());

GetDouble 方法可用于 OledbDataReader(DbDataReader) 并用作

        OleDbDataReader dr = df.ExecuteReader();
        while (dr.Read())
        {
            double val = dr.GetDouble(0);//ordinal of column
        }

但是我建议你给出正确的命名 conventions.Names 就像 'df' 导致程序员在调试代码时无处可去,因为它变得复杂

除了 Akshita2gud 在查询余额数据时从 reader 中获取双精度值之外,您的更新命令格式不正确,即使是内部命令,您能否对 SQL-通过将字符串附加在一起进行注入?当然...

我发现的 OleDB 命令使用“?”作为放置来自 activity 的参数值的占位符。您有参数字符串,但该字符串是根据屏幕上的手动输入构建的。您可能应该将其设置为:

string update = "Update Accounts Set Balance = ? Where acctNO = ?";

并且添加的参数与上面的SQL更新相同的顺序位置,首先添加余额字段,然后添加帐号。如果 balance 是数字,则不需要在其周围添加字符串,因为参数是数字。账号类似。如果是string,参数是string,它就知道怎么处理了。我喜欢在参数名称前加上前缀,这样就不会产生歧义,认为它知道列名与您打算进入的内容……因为 OleDB 查询中没有命名参数。我添加了 "p" 分别表示余额和账户号的 "parameter"。

df.Parameters.AddWithValue("@pBalance", balance);   // balance is already numeric
df.Parameters.AddWithValue("@pAcctNo", AcctDep.Text);   // account # is string

df.ExecuteNonQuery();
dc.Close();

应用类似的 SQL-更新您的存款和取款声明,并根据需要修复您的数字转换,例如 Akshita2gud 提出的建议。