首页 > 代码库 > 使用sqlite保存数据返回主键
使用sqlite保存数据返回主键
/// <summary> /// 返回insert后的主键值 /// </summary> /// <param name="SQLString"></param> /// <param name="para"></param> /// <returns></returns> public static int ExecuteSql(string SQLString, List<SQLiteParameter> para) { using (SQLiteConnection connection = GetSQLiteConnection()) { using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection)) { try { connection.Open(); if (para!=null) { foreach (SQLiteParameter p in para) { cmd.Parameters.Add(p); } } int rows = 0; if (SQLString.IndexOf("insert") != -1) rows = Convert.ToInt32(cmd.ExecuteScalar()); else rows = cmd.ExecuteNonQuery(); return rows; } catch (SQLiteException e) { connection.Close(); throw e; } } } }
注意:要在同一个连接下可以
string sql = "insert into ims_tbl_HotelInfo(HotelId,HotelName,HotelTelephone,HotelAddress,Remark,KId,Vip,XId,YId,BId,CId,DId,EId,FId,HId) values(‘" + hotelId + "‘,‘" + hotelName + "‘,‘" + hotalTelephone + "‘,‘" + hotalAddress + "‘,‘" + hremark + "‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘)"; int result = SQLiteDBHelper.ExecuteSql(sql + ";select last_insert_rowid();", null);//返回主键
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。