// Verbinndung zum SQL-Server aufbauen
NpgsqlConnection SqlConn = new NpgsqlConnection(Program.Data.Settings.SQL_Server_ConnectionString);
SqlConn.Open();
NpgsqlDataAdapter daSql = new NpgsqlDataAdapter("SELECT * FROM " + Program.Data.Tab_SinterPersonal, SqlConn);
NpgsqlCommandBuilder cmds = new NpgsqlCommandBuilder(daSql);
daSql.DeleteCommand = cmds.GetDeleteCommand();
daSql.InsertCommand = cmds.GetInsertCommand();
daSql.UpdateCommand = cmds.GetUpdateCommand();
daSql.Update(Program.Data.dsSql.Tables[Program.Data.Tab_SinterPersonal]);
//NpgsqlConnection.Commit();
SqlConn.Commit();
SqlConn.Close();
错误:
NpgsqlConnection
不包含Commit
的定义,也找不到找不到接受类型为Commit
的第一个参数的扩展方法NpgsqlConnection
(您是否缺少using指令或组装参考?)
答案 0 :(得分:0)
提交不是在连接上完成的,而是在事务上完成的。该代码应如下所示:
using var conn = new NpgsqlConnection(Program.Data.Settings.SQL_Server_ConnectionString);
SqlConn.Open();
using var transaction = conn.BeginTransaction(); // Begin the transaction
var daSql = new NpgsqlDataAdapter("SELECT * FROM " + Program.Data.Tab_SinterPersonal, SqlConn);
var cmds = new NpgsqlCommandBuilder(daSql);
daSql.DeleteCommand = cmds.GetDeleteCommand();
daSql.InsertCommand = cmds.GetInsertCommand();
daSql.UpdateCommand = cmds.GetUpdateCommand();
daSql.Update(Program.Data.dsSql.Tables[Program.Data.Tab_SinterPersonal]);
transaction.Commit(); // Commit the transaction