首页 > 数据库技术 > 详细

使用sqlite保存数据返回主键

时间:2014-05-11 16:08:28      阅读:597      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
  /// <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;
                    }
                }
            }
        }
bubuko.com,布布扣

注意:要在同一个连接下可以

    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);//返回主键
                        

 

使用sqlite保存数据返回主键,布布扣,bubuko.com

使用sqlite保存数据返回主键

原文:http://www.cnblogs.com/robinblogs/p/3721803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!