好的,所以我正在尝试将已在我的程序中更新的私人成员发送到访问数据库。但是,每当我运行程序并按下保存按钮时,我都会收到“标准表达式中的数据类型不匹配”异常。
这是我正在使用的代码片段。它在 da.Update 命令中引发异常。所以我知道这与我的更新命令或我的参数有关。由于我一直试图缩小问题范围,因此我还注释掉了重复的部分。
OleDbConnection 连接 = 新 OleDbConnection(ConnString);
string sql = @" SELECT * FROM Account where AccountID = '" + accountName + @"'";
string update = @" UPDATE Account SET
Cash = '@Cash'";/*, PaidInCapital = '@PaidInCapital',
TotalRetainedEarnings = '@TotalRetainedEarnings',
StockholdersEquity = '@StockholdersEquity',
CommonStock = '@CommonStock', PreferredStock = '@PreferredStock',
TreasuryStock = '@TreasuryStock', CashDividends = '@CashDividends',
StockDividends = '@StockDividends', @"TotalNumberPreferred = '@TotalNumberPreferred',
PreferredMarketPrice = '@PreferredMarketPrice', PreferredPar = '@PreferredPar',
Cumulative = '@Cumulative', TotalNumberCommon = '@TotalNumberCommon',
CommonMarketPrice = '@CommonMarketPrice', CommonPar = '@CommonPar',
NumberTransactTreasuryStock = '@NumberTransactTreasuryStock',
AvgPriceTreasury = '@AvgPriceTreasury'";*/
try
{
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand(sql, conn);
AccountDatabaseDataSet ds = new AccountDatabaseDataSet();
da.Fill(ds, "Account");
DataTable dt = ds.Tables["Account"];
dt.Rows[0][1] = cash;
/*dt.Rows[0][2] = paidInCapital;
dt.Rows[0][3] = totalRetainedEarnings;
dt.Rows[0][4] = stockholdersEquity;
dt.Rows[0][5] = commonStock;
dt.Rows[0][6] = preferredStock;
dt.Rows[0][7] = treasuryStock;
dt.Rows[0][8] = cashDividends;
dt.Rows[0][9] = stockDividends;
dt.Rows[0][10] = totalNumberPreferred;
dt.Rows[0][11] = preferredMarketPrice;
dt.Rows[0][12] = preferredPar;
dt.Rows[0][13] = preferredRate;
dt.Rows[0][14] = cumulative;
dt.Rows[0][15] = totalNumberCommon;
dt.Rows[0][16] = commonMarketPrice;
dt.Rows[0][17] = commonPar;
dt.Rows[0][18] = numberTransactTreasury;
dt.Rows[0][19] = avgPriceTreasury;*/
OleDbCommand cmd = new OleDbCommand(update, conn);
cmd.Parameters.Add("@Cash", OleDbType.Decimal, 18, "Cash");
/*cmd.Parameters.Add("@PaidInCapital", OleDbType.Decimal, 18, "PaidInCapital");
cmd.Parameters.Add("@TotalRetainedEarnings", OleDbType.Decimal, 18, "TotalRetainedEarnings");
cmd.Parameters.Add("@StockholdersEquity", OleDbType.Decimal, 18, "StockholdersEquity");
cmd.Parameters.Add("@CommonStock", OleDbType.Decimal, 18, "CommonStock");
cmd.Parameters.Add("@PreferredStock", OleDbType.Decimal, 18, "PreferredStock");
cmd.Parameters.Add("@TreasuryStock", OleDbType.Decimal, 18, "TreasuryStock");
cmd.Parameters.Add("@CashDividends", OleDbType.Decimal, 18, "CashDividends");
cmd.Parameters.Add("@StockDividends", OleDbType.Decimal, 18, "StockDividends");
cmd.Parameters.Add("@TotalNumberPreferred", OleDbType.Integer, 16, "TotalNumberPreferred");
cmd.Parameters.Add("@PreferredMarketPrice", OleDbType.Decimal, 10, "PreferredMarketPrice");
cmd.Parameters.Add("@PreferredPar", OleDbType.Decimal, 10, "PreferredPar");
cmd.Parameters.Add("@PreferredRate", OleDbType.Decimal, 5, "PreferredRate");
cmd.Parameters.Add("@Cumulative", OleDbType.Boolean, 2, "Cumulative");
cmd.Parameters.Add("@TotalNumberCommon", OleDbType.Integer, 16, "TotalNumberCommon");
cmd.Parameters.Add("@CommonMarketPrice", OleDbType.Decimal, 10, "CommonMarketPrice");
cmd.Parameters.Add("@CommonPar", OleDbType.Decimal, 10, "CommonPar");
cmd.Parameters.Add("@NumberTransactTreasuryStock", OleDbType.Integer, 16, "NumberTransactTreasuryStock");
cmd.Parameters.Add("@AvgPriceTreasury", OleDbType.Decimal, 10, "AvgPriceTreasury");*/
da.UpdateCommand = cmd;
da.Update(ds, "Account");
如果您取出注释部分,基本上可以缩小到这一点:
OleDbConnection conn = new OleDbConnection(ConnString);
string sql = @" SELECT * FROM Account where AccountID = '" + accountName + @"'";
string update = @" UPDATE Account SET
Cash = '@Cash'";
try{
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand(sql, conn);
AccountDatabaseDataSet ds = new AccountDatabaseDataSet();
da.Fill(ds, "Account");
DataTable dt = ds.Tables["Account"];
dt.Rows[0][1] = cash;
OleDbCommand cmd = new OleDbCommand(update, conn);
cmd.Parameters.Add("@Cash", OleDbType.Decimal, 18, "Cash");
da.UpdateCommand = cmd;
da.Update(ds, "Account");
}
所以,基本上我拥有的是一个“帐户”表,我想将第二个 [1] 列设置为等于十进制类型的私有成员变量“现金”。然后我为“@Cash”参数设置我的参数,其中 OleDbType 是十进制,大小是 18,访问数据库中的列是“现金”。最后我用这个值更新了“账户”表。
现在,我尝试将我的 oledbtype 中的 .Decimal 更改为列表中的每种可能类型,但它似乎永远不会起作用,甚至某些异常将其声明为“无法转换为 DateTime 的十进制类型”。所以我被引导相信 oledbtype 不是问题。我也尝试过弄乱参数中的大小,但这也没有奏效。
我已尽力解释这一点,但如果还有其他有助于解决此问题的内容,我将提供。