zoukankan      html  css  js  c++  java
  • 使用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);//返回主键
                            
  • 相关阅读:
    CentOS7配置VIP
    Linux定时任务crontab命令
    zabbix安装部署
    ansible部署EFK
    Python中参数的冒号与箭头表示注释
    pycharm 进行远程服务器修改与调试
    判别模型与生成模型
    写Python机器学习时的一些注意事项
    numpy基本操作
    手写神经网络
  • 原文地址:https://www.cnblogs.com/robinblogs/p/3721803.html
Copyright © 2011-2022 走看看