C# 使用System.data.sqlite用ExecuteScalar的方式返回新插入的id

在C#中通过System.data.sqlite管理SQLite数据库时,要获取新插入数据的ID,可以在插入语句后添加';select last_insert_rowid()',然后使用ExecuteScalar方法返回ID。例如:sql=insert into table(字段)values(‘值’);sql+=;select last_insert_rowid(),执行后通过Convert.ToInt16将返回的ID转换为整数。

在C#中使用System.data.salite来管理数据库,如果想要得到新插入值的id,可以用下面的方式来获取:

sql = "insert into table(字段)values(“值”)";

sql += ";select last_insert_rowid()";

后面一定要加上";select last_insert_rowid()",这样就可以用ExecuteScalar的方式返回ID了。

int = Convert.ToInt16( cmd.ExecuteScalar(CommandBehavior.SingleResult).ToString());
Console.WriteLine("id="+id);

System.Data.SQLite.SQLiteException HResult=0x87AF202F Message=constraint failed UNIQUE constraint failed: tab_test.id Source=System.Data.SQLite StackTrace: at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt) at System.Data.SQLite.SQLiteDataReader.Step(SQLiteStatement statement) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteScalar(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteScalar() at SqlSugar.AdoProvider.GetScalar(String sql, SugarParameter[] parameters) at SqlSugar.AdoProvider.GetInt(String sql, SugarParameter[] parameters) at SqlSugar.InsertableProvider`1.ExecuteReturnIdentity() at Xcf.Global.DB.Insertable[T](List`1 mode) in F:\ProjectDir\VSProject\应知应会系统\NeedKnowNeedCan1\NeedKnowNeedCan\NeedKnowNeedCan\Global\GlobalSetting.cs:line 94 at Xcf.UI.Controls.UC_ManagerMain.ImportMain(List`1 pathList) in F:\ProjectDir\VSProject\应知应会系统\NeedKnowNeedCan1\NeedKnowNeedCan\NeedKnowNeedCan\UI\Controls\UC_ManagerMain.xaml.cs:line 1199 at Xcf.UI.Controls.UC_ManagerMain.<>c__DisplayClass52_0.<Btn_批量导入1>b__0() in F:\ProjectDir\VSProject\应知应会系统\NeedKnowNeedCan1\NeedKnowNeedCan\NeedKnowNeedCan\UI\Controls\UC_ManagerMain.xaml.cs:line 1120 at System.Threading.Tasks.Task.InnerInvoke() in System.Threading.Tasks\Task.cs:line 2404 at System.Threading.Tasks.Task.Execute() in System.Threading.Tasks\Task.cs:line 2194 此异常最初是在此调用堆栈中引发的: System.Data.SQLite.SQLite3.Step(System.Data.SQLite.SQLiteStatement) System.Data.SQLite.SQLiteDataReader.Step(System.Data.SQLite.SQLiteStatement) System.Data.SQLite.SQLiteDataReader.NextResult() System.Data.SQLite.SQLiteDataReader.SQLiteDataReader(System.Data.SQLite.SQLiteCommand, System.Data.CommandBehavior) System.Data.SQLite.SQLiteCommand.ExecuteReader(System.Data.CommandBehavior) System.Data.SQLite.SQLiteCommand.ExecuteScalar(System.Data.CommandBehavior) System.Data.SQLite.SQLiteCommand.ExecuteScalar() SqlSugar.AdoProvider.GetScalar(string, SqlSugar.SugarParameter[]) SqlSugar.AdoProvider.GetInt(string, SqlSugar.SugarParameter[]) SqlSugar.InsertableProvider<T>.ExecuteReturnIdentity() ... [调用堆栈已截断]
最新发布
09-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值