如何从SQLite数据库的新记录中获取char数据类型的字段,它也是使用dapper + c#通过触发器生成的
How to get field with char data type from new records of SQLite database, and its also generated by triggers using dapper + c#
我看到了这个视频:
https://youtu.be/eKkh5Xm0OlU?t=1590
I'am using SQLite version 3.11.2, and NuGet: Dapper version 1.50.2
我正在尝试通过上次插入的触发器获取身份自动生成的 ID
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 }");
}
}
}
}
我在这一行出错:
int recordsStat = cnn.Execute("INSERT INTO some_table (a, b) VALUES (@a, @b); SELECT @GuidAuto= @@IDENTITY", dp, trans);
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)
抱歉英语不好。谢谢大家,祝你有个愉快的一天。
我不知道为什么,但就我而言,这是可行的。谢谢
string uid = con.Query<string>("SELECT uid from sometable WHERE ROWID = (SELECT last_insert_rowid() FROM sometable)", new DynamicParameters()).Single();
//return last insert uid
我看到了这个视频: https://youtu.be/eKkh5Xm0OlU?t=1590
I'am using SQLite version 3.11.2, and NuGet: Dapper version 1.50.2
我正在尝试通过上次插入的触发器获取身份自动生成的 ID
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 }");
}
}
}
}
我在这一行出错:
int recordsStat = cnn.Execute("INSERT INTO some_table (a, b) VALUES (@a, @b); SELECT @GuidAuto= @@IDENTITY", dp, trans);
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)
抱歉英语不好。谢谢大家,祝你有个愉快的一天。
我不知道为什么,但就我而言,这是可行的。谢谢
string uid = con.Query<string>("SELECT uid from sometable WHERE ROWID = (SELECT last_insert_rowid() FROM sometable)", new DynamicParameters()).Single();
//return last insert uid