Search code examples
c#sqlitedapper

How to get field with char data type from new records of SQLite database, and its also generated by triggers using dapper + c#


I see this video : https://youtu.be/eKkh5Xm0OlU?t=1590

I'am using SQLite version 3.11.2, and NuGet: Dapper version 1.50.2

I'am trying to get identity auto generated id by triggers from last insert

  public static void Save(Model1 model1, List<Model2> model2){

      using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())){

        var dp = new DynamicParameters(model1);
        dp.Add("@GuidAuto", "", DbType.String, ParameterDirection.Output);

        cnn.Open();
        using (var trans = cnn.BeginTransaction()){
          try{
              int recordsStat = cnn.Execute("INSERT INTO some_table (a, b) VALUES (@a, @b); SELECT @GuidAuto= @@IDENTITY", dp, trans);
              string rmed = dp.Get<string>("@GuidAuto");
              foreach (var item in model2){
                  item.GuidAuto= rmed;
              }
              try{
                  eff = cnn.Execute("INSERT INTO some_table2(c, GuidAuto) VALUES (@c, @GuidAuto)", model2, transaction: trans);
                  trans.Commit();
              }catch (Exception ex){
                  eff = 0;
                  Console.WriteLine($"Error : { ex.Message }");
                  trans.Rollback();
              }
          }catch(Exception err){
            Console.WriteLine($"Error : { err.Message }");
          } 
        }
      }

  }

I got error at this line :

int recordsStat = cnn.Execute("INSERT INTO some_table (a, b) VALUES (@a, @b); SELECT @GuidAuto= @@IDENTITY", dp, trans);

The output of StackTrace :

at System.Data.SQLite.SQLiteParameter.set_Direction(ParameterDirection value)
at Dapper.DynamicParameters.AddParameters(IDbCommand command, Identity identity)
at Dapper.CommandDefinition.SetupCommand(IDbConnection cnn, Action`2 paramReader)
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader)
at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command)
at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType)
at Library.SqliteDataAccess.SavePembedahan(Model1 model1, List`1 model2) in D:\abc\sys\Library\SqliteDataAccess.cs:line 200
at SYS.FormDD.cdr_Click(Object sender, EventArgs e) in D:\abc\FormDD.cs:line 400
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

Sorry for bad english. thank you all, have a nice day.


Solution

  • I dont know why, but for my case it's work. thank you

    string uid = con.Query<string>("SELECT uid from sometable WHERE ROWID = (SELECT last_insert_rowid() FROM sometable)", new DynamicParameters()).Single();
    //return last insert uid