首页 > 代码库 > 新增修改删除等方法
新增修改删除等方法
//创建datatableDataTable dt = new DataTable("个人简历"); dt.Columns.Add("id", typeof(int)); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("sex", typeof(int)); dt.Columns.Add("address", typeof(string)); dt.Columns.Add("aihao", typeof(string)); dt.Columns.Add("photo", typeof(string)); dt.Rows.Add(new object[] { 1, "张三", 1, "东大街6号", "看书", "" }); dt.Rows.Add(new object[] { 1, "王五", 0, "西大街2号", "上网,游戏", "" }); dt.Rows.Add(new object[] { 1, "李四", 1, "南大街3号", "上网,逛街", "" }); dt.Rows.Add(new object[] { 1, "钱八", 0, "北大街5号", "上网,逛街,看书,游戏", "" }); dt.Rows.Add(new object[] { 1, "赵九", 1, "中大街1号", "看书,逛街,游戏", "" }); //查询(getDatas)public static DataSet getDatas(string tableName,string strCondition){ string sql = "select * from "+tableName+" where 1=1 "+strCondition; DB.DataHelper dtHelper = new DB.DataHelper(); return dtHelper.ExecuteSql(sql); }//获取(getItems)public static DataSet getItems(string code,string describe,string tableName,string strCondition){ string sql = "select distinct "+code+" as code ,"+describe+" as describe from "+tableName+" where 1=1 "+strCondition; DB.DataHelper dtHelper = new DB.DataHelper(); return dtHelper.ExecuteSql(sql); }//新增多行(insertDatas)public static int insertDatas(DataTable dt){ if(dt ==null || dt.Rows.Count<=0) return 0; string tableName = dt.TableName; ArrayList sqlList = new ArrayList(); string sql =""; string sql1=""; string sql2=""; foreach(DataRow row in dt.Rows) ////遍历行拼sql语句 { sql = "insert into "+tableName+"("; sql1 =""; sql2 =""; for(int i =0;i<dt.Columns.Count;i++) { sql1=sql1+dt.Columns[i].ColumnName.ToString()+","; sql2=sql2+"‘"+row[i].ToString()+"‘,"; } sql = sql +sql1.Substring(0, sql1.Length - 1)+") values("+sql2.Substring(0, sql2.Length - 1)+ ")"; sqlList.Add(sql); } DB.DataHelper dtHelper = new DB.DataHelper(); dtHelper.ExecuteSqlTran(sqlList); return 1;}//修改多行(updateDatas)public static int updateDatas(DataTable dt){ if(dt ==null || dt.Rows.Count<=0) return 0; string tableName = dt.TableName; string pk = dt.Columns[0].ColumnName.ToString(); //获取第一列名(主键列) string pkValue = http://www.mamicode.com/row[0].ToString(); //获取第一行的(主键值) ArrayList sqlList = new ArrayList(); string sql =""; string sql1=""; foreach (DataRow row in dt.Rows) //遍历行 { sql = "update " + tableName + " set "; for (int i = 1; i < dt.Columns.Count; i++) //从1开始,因为第一列为主键值 { sql1 = sql1 + dt.Columns[i].ColumnName + "= ‘" + row[i].ToString() + "‘,";//列名+列值 } sql = sql + sql1.Substring(0, sql1.Length - 1) + " where " + pk + " = ‘" + pkValue + "‘"; sqlList.Add(sql); } DB.DataHelper dtHelper = new DB.DataHelper(); dtHelper.ExecuteSqlTran(sqlList); return 1;}//删除单条,返回影响条数(deleteData)public static int deleteData(string tableName,string strCondition){ string sql = "delete from "+tableName+" where 1=1 "+strCondition; DB.DataHelper dtHelper = new DB.DataHelper(); return dtHelper.ExecuteSql(sql); }//删除多条(deleteDatas)public static int deleteDatas(DataTable dt){ if(dt == null||dt.Rows.Count<=0) return 0; string tableName = dt.TableName; ArrayList sqlList = new ArrayList(); string sql = ""; foreach(DataRow row in dt.Rows) { string pk = dt.Columns[0].ColumnName.ToString(); //获取第一列名(主键列) string pkValue = http://www.mamicode.com/row[0].ToString(); //获取第一行的(主键值) sql="delete from "+tableName+" where "+pk+" = ‘"+pkValue+"‘"; sqlList.Add(sql); } DB.DataHelper dtHelper = new DB.DataHelper(); dtHelper.ExecuteSqlTran(sqlList); return 1;}//多步操作使用数组接受参数(multiStep)public static int multilStep(DataTable dt){ string[] arr = new string[dt.Columns.Count]; foreach(DataRow row in dt.Rows) { for (int i = 0; i < dt.Columns.Count; i++) { arr[i]=row[i].ToString(); } } //参数1arr[0] //参数2arr[1] //参数3arr[2] ... string sql1=""; string sql2=""; string sql3=""; ArrayList sqlList = new ArrayList(); sqlList.Add(sql1); sqlList.Add(sql2); sqlList.Add(sql2);3. DB.DataHelper dtHelper = new DB.DataHelper(); dtHelper.ExecuteSqlTran(sqlList); return 1;}
新增修改删除等方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。