将值插入 mysql 数据库未按预期工作
Inserting values into a mysql database does not work as expected
我尝试从 C# 中填充 mySQL/ MariaDB table。读取 table 并填充数据集有效。
table 结构及其键等已给出且无法更改:
CREATE TABLE `fhem`.`history` (TIMESTAMP TIMESTAMP, DEVICE varchar(32), TYPE varchar(32), EVENT varchar(512), READING varchar(32), VALUE varchar(32), UNIT varchar(32));
CREATE TABLE `fhem`.`current` (TIMESTAMP TIMESTAMP, DEVICE varchar(32), TYPE varchar(32), EVENT varchar(512), READING varchar(32), VALUE varchar(32), UNIT varchar(32));
CREATE INDEX Search_Idx ON `fhem`.`history` (DEVICE, READING, TIMESTAMP);
这是我连接到数据库的代码,创建 sql 命令和适配器,然后用数据库 table 中的数据填充数据集。
然后我创建一个新的数据行,用值填充列并将其添加到数据集的数据表中。
class Program
{
static void Main(string[] args)
{
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
connBuilder.Server = "ds2";
connBuilder.Database = "fhem";
connBuilder.UserID = "fhemdbuser";
connBuilder.Port = 3307;
connBuilder.Password = "!2345Abcde";
//connBuilder.DefaultCommandTimeout = 120;
//connBuilder.UseDefaultCommandTimeoutForEF = true;
//connBuilder.AllowUserVariables = true;
MySqlConnection conn = new MySqlConnection(connBuilder.ConnectionString);
conn.Open();
MySqlCommand selectCmdCurrent = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from current;", conn);
MySqlCommand selectCmdCurrentTest = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from currentTest;", conn);
MySqlCommand updateCmd = new MySqlCommand("update `currentTest` set `TIMESTAMP`=@p1,`DEVICE`=@p2,`TYPE`=@p3,`EVENT`=@p4,`READING`=@p5,`VALUE`=@p6,`UNIT`=@p7", conn);
updateCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
updateCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
updateCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlCommand insertCmd = new MySqlCommand("insert into `currentTest` values(@p1, @p2, @p3, @p4, @p5, @p6, @p7)", conn);
insertCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
insertCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
insertCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlDataAdapter dataAdapterCurrent = new MySqlDataAdapter(selectCmdCurrent);
MySqlDataAdapter dataAdapterCurrentTest = new MySqlDataAdapter(selectCmdCurrentTest);
// MySqlCommandBuilder builder = new MySqlCommandBuilder(dataAdapterCurrent);
dataAdapterCurrentTest.InsertCommand = insertCmd;
dataAdapterCurrentTest.UpdateCommand = updateCmd;
DataSet ds = new DataSet();
MySqlCommand prepareCmd = new MySqlCommand("truncate currentTest;", conn);
prepareCmd.ExecuteNonQuery();
//prepareCmd = new MySqlCommand("insert into currentTest select * from current;", conn);
//prepareCmd.ExecuteNonQuery();
prepareCmd = new MySqlCommand("INSERT INTO `currentTest` VALUES(\"2020-06-06 12:45:23\",\"DEVICE\", \"TYPE\", \"EVENT\", \"READING\", \"VALUE\", \"UNIT\")", conn);
for (int i = 0; i < 10; i++)
prepareCmd.ExecuteNonQuery();
dataAdapterCurrentTest.Fill(ds, "currentTest");
dataAdapterCurrent.Fill(ds, "current");
int row = 0;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 10; i++)
{
DataRow newrow = ds.Tables["currentTest"].NewRow();
newrow["Timestamp"] = DateTime.Now;
newrow["Device"] = "Device";
newrow["TYPE"] = "Type";
newrow["EVENT"] = "Event";
newrow["READING"] = "Reading";
newrow["VALUE"] = "Value";
newrow["UNIT"] = "Unit";
ds.Tables["currentTest"].Rows.Add(newrow);
}
dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
ds.Tables["currentTest"].AcceptChanges();
}
}
虽然手动插入命令将正确的值放入数据库,但通过
的编程方法
DataRow newrow = ds.Tables["currentTest"].NewRow();
ds.Tables["currentTest"].Rows.Add(newrow);
dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
ds.Tables["currentTest"].AcceptChanges();
仅添加具有有效日期的行,但其他列为 NULL。
[编辑#1]:
我对我的代码做了一些更改,基本上我修改了第二个 for 循环以插入新数据,就像我在第一个循环中所做的那样。然后数据被正确写入数据库。但我怀疑,调用 .ExecuteNonQuery()
数千次以将我的日志文件放入数据库是否是一种高效的方法...
[编辑#2]:
120 分钟后,.ExecuteNonQuery()
向数据库中插入的 100 000 条数据仍然是 运行。这很慢。
对于 MysqlDataAdapter,我们只需要设置 selectCommand。它会通过 MySqlCommandBuilder
自动生成 insertCommand
您的代码几乎是正确的,只需更改 2 处。
1.在 dataAdapterCurrentTest.Update(ds.Tables["currentTest"])
之前添加以下行
new MySqlCommandBuilder(dataAdapterCurrentTest);
2。在代码行后面评论
dataAdapterCurrentTest.InsertCommand=insertCmd
所以最后的代码是
class Program
{
static void Main(string[] args)
{
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
connBuilder.Server = "ds2";
connBuilder.Database = "fhem";
connBuilder.UserID = "fhemdbuser";
connBuilder.Port = 3307;
connBuilder.Password = "!2345Abcde";
//connBuilder.DefaultCommandTimeout = 120;
//connBuilder.UseDefaultCommandTimeoutForEF = true;
//connBuilder.AllowUserVariables = true;
MySqlConnection conn = new MySqlConnection(connBuilder.ConnectionString);
conn.Open();
MySqlCommand selectCmdCurrent = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from current;", conn);
MySqlCommand selectCmdCurrentTest = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from currentTest;", conn);
MySqlCommand updateCmd = new MySqlCommand("update `currentTest` set `TIMESTAMP`=@p1,`DEVICE`=@p2,`TYPE`=@p3,`EVENT`=@p4,`READING`=@p5,`VALUE`=@p6,`UNIT`=@p7", conn);
updateCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
updateCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
updateCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlCommand insertCmd = new MySqlCommand("insert into `currentTest` values(@p1, @p2, @p3, @p4, @p5, @p6, @p7)", conn);
insertCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
insertCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
insertCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlDataAdapter dataAdapterCurrent = new MySqlDataAdapter(selectCmdCurrent);
MySqlDataAdapter dataAdapterCurrentTest = new MySqlDataAdapter(selectCmdCurrentTest);
// MySqlCommandBuilder builder = new MySqlCommandBuilder(dataAdapterCurrent);
// dataAdapterCurrentTest.InsertCommand = insertCmd;
dataAdapterCurrentTest.UpdateCommand = updateCmd;
DataSet ds = new DataSet();
MySqlCommand prepareCmd = new MySqlCommand("truncate currentTest;", conn);
prepareCmd.ExecuteNonQuery();
//prepareCmd = new MySqlCommand("insert into currentTest select * from current;", conn);
//prepareCmd.ExecuteNonQuery();
prepareCmd = new MySqlCommand("INSERT INTO `currentTest` VALUES(\"2020-06-06 12:45:23\",\"DEVICE\", \"TYPE\", \"EVENT\", \"READING\", \"VALUE\", \"UNIT\")", conn);
for (int i = 0; i < 10; i++)
prepareCmd.ExecuteNonQuery();
dataAdapterCurrentTest.Fill(ds, "currentTest");
dataAdapterCurrent.Fill(ds, "current");
int row = 0;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 10; i++)
{
DataRow newrow = ds.Tables["currentTest"].NewRow();
newrow["Timestamp"] = DateTime.Now;
newrow["Device"] = "Device";
newrow["TYPE"] = "Type";
newrow["EVENT"] = "Event";
newrow["READING"] = "Reading";
newrow["VALUE"] = "Value";
newrow["UNIT"] = "Unit";
ds.Tables["currentTest"].Rows.Add(newrow);
}
new MySqlCommandBuilder(dataAdapterCurrentTest);
dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
ds.Tables["currentTest"].AcceptChanges();
}
}
我尝试从 C# 中填充 mySQL/ MariaDB table。读取 table 并填充数据集有效。
table 结构及其键等已给出且无法更改:
CREATE TABLE `fhem`.`history` (TIMESTAMP TIMESTAMP, DEVICE varchar(32), TYPE varchar(32), EVENT varchar(512), READING varchar(32), VALUE varchar(32), UNIT varchar(32));
CREATE TABLE `fhem`.`current` (TIMESTAMP TIMESTAMP, DEVICE varchar(32), TYPE varchar(32), EVENT varchar(512), READING varchar(32), VALUE varchar(32), UNIT varchar(32));
CREATE INDEX Search_Idx ON `fhem`.`history` (DEVICE, READING, TIMESTAMP);
这是我连接到数据库的代码,创建 sql 命令和适配器,然后用数据库 table 中的数据填充数据集。 然后我创建一个新的数据行,用值填充列并将其添加到数据集的数据表中。
class Program
{
static void Main(string[] args)
{
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
connBuilder.Server = "ds2";
connBuilder.Database = "fhem";
connBuilder.UserID = "fhemdbuser";
connBuilder.Port = 3307;
connBuilder.Password = "!2345Abcde";
//connBuilder.DefaultCommandTimeout = 120;
//connBuilder.UseDefaultCommandTimeoutForEF = true;
//connBuilder.AllowUserVariables = true;
MySqlConnection conn = new MySqlConnection(connBuilder.ConnectionString);
conn.Open();
MySqlCommand selectCmdCurrent = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from current;", conn);
MySqlCommand selectCmdCurrentTest = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from currentTest;", conn);
MySqlCommand updateCmd = new MySqlCommand("update `currentTest` set `TIMESTAMP`=@p1,`DEVICE`=@p2,`TYPE`=@p3,`EVENT`=@p4,`READING`=@p5,`VALUE`=@p6,`UNIT`=@p7", conn);
updateCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
updateCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
updateCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlCommand insertCmd = new MySqlCommand("insert into `currentTest` values(@p1, @p2, @p3, @p4, @p5, @p6, @p7)", conn);
insertCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
insertCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
insertCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlDataAdapter dataAdapterCurrent = new MySqlDataAdapter(selectCmdCurrent);
MySqlDataAdapter dataAdapterCurrentTest = new MySqlDataAdapter(selectCmdCurrentTest);
// MySqlCommandBuilder builder = new MySqlCommandBuilder(dataAdapterCurrent);
dataAdapterCurrentTest.InsertCommand = insertCmd;
dataAdapterCurrentTest.UpdateCommand = updateCmd;
DataSet ds = new DataSet();
MySqlCommand prepareCmd = new MySqlCommand("truncate currentTest;", conn);
prepareCmd.ExecuteNonQuery();
//prepareCmd = new MySqlCommand("insert into currentTest select * from current;", conn);
//prepareCmd.ExecuteNonQuery();
prepareCmd = new MySqlCommand("INSERT INTO `currentTest` VALUES(\"2020-06-06 12:45:23\",\"DEVICE\", \"TYPE\", \"EVENT\", \"READING\", \"VALUE\", \"UNIT\")", conn);
for (int i = 0; i < 10; i++)
prepareCmd.ExecuteNonQuery();
dataAdapterCurrentTest.Fill(ds, "currentTest");
dataAdapterCurrent.Fill(ds, "current");
int row = 0;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 10; i++)
{
DataRow newrow = ds.Tables["currentTest"].NewRow();
newrow["Timestamp"] = DateTime.Now;
newrow["Device"] = "Device";
newrow["TYPE"] = "Type";
newrow["EVENT"] = "Event";
newrow["READING"] = "Reading";
newrow["VALUE"] = "Value";
newrow["UNIT"] = "Unit";
ds.Tables["currentTest"].Rows.Add(newrow);
}
dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
ds.Tables["currentTest"].AcceptChanges();
}
}
虽然手动插入命令将正确的值放入数据库,但通过
的编程方法DataRow newrow = ds.Tables["currentTest"].NewRow();
ds.Tables["currentTest"].Rows.Add(newrow);
dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
ds.Tables["currentTest"].AcceptChanges();
仅添加具有有效日期的行,但其他列为 NULL。
[编辑#1]:
我对我的代码做了一些更改,基本上我修改了第二个 for 循环以插入新数据,就像我在第一个循环中所做的那样。然后数据被正确写入数据库。但我怀疑,调用 .ExecuteNonQuery()
数千次以将我的日志文件放入数据库是否是一种高效的方法...
[编辑#2]:
120 分钟后,.ExecuteNonQuery()
向数据库中插入的 100 000 条数据仍然是 运行。这很慢。
对于 MysqlDataAdapter,我们只需要设置 selectCommand。它会通过 MySqlCommandBuilder
您的代码几乎是正确的,只需更改 2 处。
1.在 dataAdapterCurrentTest.Update(ds.Tables["currentTest"])
new MySqlCommandBuilder(dataAdapterCurrentTest);
2。在代码行后面评论
dataAdapterCurrentTest.InsertCommand=insertCmd
所以最后的代码是
class Program
{
static void Main(string[] args)
{
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
connBuilder.Server = "ds2";
connBuilder.Database = "fhem";
connBuilder.UserID = "fhemdbuser";
connBuilder.Port = 3307;
connBuilder.Password = "!2345Abcde";
//connBuilder.DefaultCommandTimeout = 120;
//connBuilder.UseDefaultCommandTimeoutForEF = true;
//connBuilder.AllowUserVariables = true;
MySqlConnection conn = new MySqlConnection(connBuilder.ConnectionString);
conn.Open();
MySqlCommand selectCmdCurrent = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from current;", conn);
MySqlCommand selectCmdCurrentTest = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from currentTest;", conn);
MySqlCommand updateCmd = new MySqlCommand("update `currentTest` set `TIMESTAMP`=@p1,`DEVICE`=@p2,`TYPE`=@p3,`EVENT`=@p4,`READING`=@p5,`VALUE`=@p6,`UNIT`=@p7", conn);
updateCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
updateCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
updateCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
updateCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlCommand insertCmd = new MySqlCommand("insert into `currentTest` values(@p1, @p2, @p3, @p4, @p5, @p6, @p7)", conn);
insertCmd.Parameters.Add("@p1", MySql.Data.MySqlClient.MySqlDbType.DateTime);
insertCmd.Parameters.Add("@p2", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p3", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p4", MySql.Data.MySqlClient.MySqlDbType.VarString, 512);
insertCmd.Parameters.Add("@p5", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p6", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
insertCmd.Parameters.Add("@p7", MySql.Data.MySqlClient.MySqlDbType.VarString, 128);
MySqlDataAdapter dataAdapterCurrent = new MySqlDataAdapter(selectCmdCurrent);
MySqlDataAdapter dataAdapterCurrentTest = new MySqlDataAdapter(selectCmdCurrentTest);
// MySqlCommandBuilder builder = new MySqlCommandBuilder(dataAdapterCurrent);
// dataAdapterCurrentTest.InsertCommand = insertCmd;
dataAdapterCurrentTest.UpdateCommand = updateCmd;
DataSet ds = new DataSet();
MySqlCommand prepareCmd = new MySqlCommand("truncate currentTest;", conn);
prepareCmd.ExecuteNonQuery();
//prepareCmd = new MySqlCommand("insert into currentTest select * from current;", conn);
//prepareCmd.ExecuteNonQuery();
prepareCmd = new MySqlCommand("INSERT INTO `currentTest` VALUES(\"2020-06-06 12:45:23\",\"DEVICE\", \"TYPE\", \"EVENT\", \"READING\", \"VALUE\", \"UNIT\")", conn);
for (int i = 0; i < 10; i++)
prepareCmd.ExecuteNonQuery();
dataAdapterCurrentTest.Fill(ds, "currentTest");
dataAdapterCurrent.Fill(ds, "current");
int row = 0;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 10; i++)
{
DataRow newrow = ds.Tables["currentTest"].NewRow();
newrow["Timestamp"] = DateTime.Now;
newrow["Device"] = "Device";
newrow["TYPE"] = "Type";
newrow["EVENT"] = "Event";
newrow["READING"] = "Reading";
newrow["VALUE"] = "Value";
newrow["UNIT"] = "Unit";
ds.Tables["currentTest"].Rows.Add(newrow);
}
new MySqlCommandBuilder(dataAdapterCurrentTest);
dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
ds.Tables["currentTest"].AcceptChanges();
}
}