您能否解释一下如何解决以下问题?
Could you please explain how to solve following problem?
// 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
does not contain a definition for Commit
and no extension method Commit
accepting a first argument of type NpgsqlConnection
could be found (are you missing a using directive or an assembly reference?)
提交不是在连接上完成的,而是在事务上完成的。代码应如下所示:
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
// 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
does not contain a definition forCommit
and no extension methodCommit
accepting a first argument of typeNpgsqlConnection
could be found (are you missing a using directive or an assembly reference?)
提交不是在连接上完成的,而是在事务上完成的。代码应如下所示:
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