设为首页 加入收藏

TOP

使用oledb对数据库进行增删改查及批量插入操作(二)
2015-11-21 01:40:12 来源: 作者: 【 】 浏览:1
Tags:使用 oledb 数据库 进行 删改 查及 批量 插入 操作
ql = string.Format("update {0} set {1} {2}", className, values,id); OleDbDataAdapter da = new OleDbDataAdapter(); da.UpdateCommand = new OleDbCommand(sql, conn); da.UpdateCommand.CommandText = sql; conn.Open(); da.UpdateCommand.ExecuteNonQuery(); conn.Close(); return true; } catch (Exception e) { MessageBox.Show(e.Message); return false; } finally { conn.Close(); } } //根据条件删除数据 public bool Delete(string where) { try { var type = typeof(T); var className = type.Name; var sql = string.Format("delete from {0} {1}", className, where); OleDbDataAdapter da = new OleDbDataAdapter(); da.DeleteCommand = new OleDbCommand(sql, conn); da.DeleteCommand.CommandText = sql; conn.Open(); da.DeleteCommand.ExecuteNonQuery(); conn.Close(); return true; } catch (Exception e) { MessageBox.Show(e.Message); return false; } finally { conn.Close(); } } //批量插入数据 public bool InsertList(List entitysList) where T : class,new() { try { var type = typeof (T); var className = type.Name; var fields = ""; var values = ""; foreach (var property in type.GetProperties()) { if (property.Name.Equals("ID")) continue; fields += "," + property.Name; var isNumStr = (property.PropertyType == typeof (double) || property.PropertyType == typeof (int)) ? "" : "'"; values += ",?" ; } fields = fields.Substring(1); values = values.Substring(1); var sql = string.Format("insert into {0}({1}) values ({2}) ", className, fields, values); OleDbDataAdapter da = new OleDbDataAdapter(); da.InsertCommand = new OleDbCommand(sql, conn); da.InsertCommand.CommandText = sql; foreach (var property in type.GetProperties()) { if (property.Name.Equals("ID")) continue; var oleType = (property.PropertyType == typeof(double) || property.PropertyType == typeof(int)) ? OleDbType.Integer : OleDbType.VarChar; da.InsertCommand.Parameters.Add(property.Name, oleType, int.MaxValue, property.Name); fields += "," + property.Name; values += ",?"; } var table = ConverterUtil.ConvertListToDataTable(entitysList); table.TableName = className; da.Update(table); return true; } catch (Exception e) { MessageBox.Show(e.Message); return false; } finally { conn.Close(); } } //这个方法是用来执行无返回结果的插入语句,如 insert select public bool ExecuteInsertSql(string sql) { try { OleDbDataAdapter da = new OleDbDataAdapter(); da.InsertCommand = new OleDbCommand(sql, conn); da.InsertCommand.CommandText = sql; conn.Open(); da.InsertCommand.ExecuteNonQuery(); conn.Close(); return true; } catch (Exception e) { MessageBox.Show(e.Message); return false; } finally { conn.Close(); } } } } ??

?

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇十七、PythonSQLAlchemy 下一篇centos7安装navicat

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: