• 新增修改删除等方法


    //创建datatable
    DataTable 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 = 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 = 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;
    }
  • 相关阅读:
    Android--adb
    Android 爬坑之路
    Android倒计时实现
    Android Studio常用设置
    Java Web开发——MySQL数据库的安装与配置
    DOS命令(系统错误5,拒绝访问)的解决方法
    Java EE开发环境——MyEclipse2017破解 和 Tomcat服务器配置
    设计模式-工厂模式
    设计模式-简单工厂模式
    设计模式简介
  • 原文地址:https://www.cnblogs.com/quke123/p/4175822.html
Copyright © 2020-2023  润新知