gpt4 book ai didi

C# MySqlDataAdapter 不更新数据库中的数据

转载 作者:行者123 更新时间:2023-11-29 18:57:25 26 4
gpt4 key购买 nike

我使用c#更新Mysql数据库中数据的过程是这样的

        cQuoteData quoteData = new cQuoteData();
DateTime? startDate = Convert.ToDateTime("01/01/1900");
if (pQuoteData.Length > 0)
{
quoteData = pQuoteData[0];
startDate = quoteData.datetime;
}
string sTableName = "Quotes" + pTimeFrame;
DataTable vDataTable = new DataTable();

MySqlCommand sqlCommand = new MySqlCommand("select symbol,date,openprice from " + sTableName + " where date >= @date and symbol = @symbol order by date",
rzSqlConnection.connection());
sqlCommand.Parameters.AddWithValue("@date", startDate);
sqlCommand.Parameters.AddWithValue("@symbol", pSymbol);


MySqlDataAdapter cdataAdapter = new MySqlDataAdapter(sqlCommand);
DataColumn[] keys = new DataColumn[0];
keys = new DataColumn[1];
vDataTable.PrimaryKey = keys;
cdataAdapter.Fill(vDataTable);

foreach (DataRow row in vDataTable.Rows)
{
row["openPrice"] = Convert.ToDouble(row["openPrice"]) * -1;
}


MySqlCommandBuilder vSqlCommand = new MySqlCommandBuilder(cdataAdapter);

cdataAdapter.UpdateCommand = vSqlCommand.GetUpdateCommand();


cdataAdapter.Update(vDataTable);

return true;

表的结构是这样的

> CREATE TABLE `quotesd1` (
`symbol` VARCHAR(40) COLLATE latin1_swedish_ci NOT NULL,
`date` DATETIME NOT NULL,
`openPrice` FLOAT NOT NULL,
`highPrice` FLOAT DEFAULT NULL,
`lowPrice` FLOAT DEFAULT NULL,
`close` FLOAT DEFAULT NULL,
`volume` FLOAT DEFAULT NULL,
`m4` FLOAT DEFAULT NULL,
`m5` FLOAT DEFAULT NULL,
`m17` FLOAT DEFAULT NULL,
`m21` FLOAT DEFAULT NULL,
`m50` FLOAT DEFAULT NULL,
`m72` FLOAT DEFAULT NULL,
`m305` FLOAT DEFAULT NULL,
**PRIMARY KEY USING BTREE (`symbol`, `date`),**
KEY `symbol` USING BTREE (`symbol`),
KEY `quotesd1_idx1` USING BTREE (`date`),
CONSTRAINT `quotesd1_fk1` FOREIGN KEY (`symbol`) REFERENCES `symbols` (`symbol`)
) ENGINE=InnoDB
CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci';

MySqlCommandBuilder生成的更新命令

"UPDATE `quotesd1` SET `symbol` = @p1, `date` = @p2, `openPrice` = @p3 WHERE ((`symbol` = @p4) AND (`date` = @p5) AND (`openPrice` = @p6))"

上面是where子句说的问​​题

WHERE ((`symbol` = @p4) AND (`date` = @p5) **AND (`openPrice` = @p6))"**

但是更新子句只需要通过主键选择更新值

这是 MysqlAdapter Bug 还是我让一些人认为错误??

详细信息,如果我手动指定更新参数,此更新工作正常,但我需要对每个更新执行此操作?

最佳答案

你的字段和表名不正确,不需要反勾引用文献:MySql Developer , Tutorials Point

"UPDATE quotesd1   SET symbol = @p1, date = @p2, openPrice = @p3   WHERE (symbol = @p4) AND (date = @p5) AND (openPrice = @p6)"

关于C# MySqlDataAdapter 不更新数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44075382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com