您能解释一下如何解决以下问题吗?

时间:2020-07-27 12:08:11

标签: npgsql

 // 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指令或组装参考?)

1 个答案:

答案 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