首页 > 编程 > .NET > 正文

ASP.NET技巧:一些常用的数据操作类

2024-07-10 13:10:24
字体:
来源:转载
供稿:网友

  偶也不知道算不算,反正我自己随时写的用,很粗浅的,适合跟我一样的新手收藏,指不定就用得上呢!

  一组是我这两天写留言本的,另组是以前vs2003写投票用的

  高手看见的话,麻烦把好的解决方案说下,谢谢哦!!新手的话,共同学习了!!

--------下面是我最近写个留言本用的------------
    public static oledbconnection con()
    {//数据库连接类
        oledbconnection con = new oledbconnection(configurationmanager.connectionstrings["book"].connectionstring);
        return con;
    }
    public static bool insert(string que)
    { //根据传进来的sql语句执行插入操作
        oledbconnection con = odb.con();
        con.open();
        oledbcommand cmd = new oledbcommand(que,con);
        int count = convert.toint32(cmd.executenonquery());
        if (count > 0)
            return true;
        else
            return false;
        con.close();
    }
    public static datatable ds(string que)
    {//返回一个装载了sql制定留言的数据表,
        oledbconnection con = odb.con();
        oledbdataadapter oda = new oledbdataadapter();
        oda.selectcommand=new oledbcommand(que,con);
        dataset ds = new dataset();
        oda.fill(ds,"thc");
        return ds.tables["thc"];
        con.close();
    }
    public static bool img(string que)
    {//根据传来的条件查询该项是否有内容,有就返回true
        oledbconnection con = odb.con();
        con.open();
        oledbcommand cmd = new oledbcommand(que,con);
        if (cmd.executescalar().tostring() != "")
            return true;
        else
            return false;
        con.close();
    }
    public static string scr(string que)
    {//同样是根据传来的sql语句返回一个字段的值,我不太喜欢把sql语句做在内中,感觉不灵活
        oledbconnection con = odb.con();
        con.open();
        oledbcommand cmd = new oledbcommand(que,con);
        return cmd.executescalar().tostring();
    }

-----------下面是以前2003的时候用的----------
public static sqlconnection con()
        {//(静态)初始化链接,因为数据库路径在web.config文件里面,所以这里返回的是哪个字符串,这是vs2003的数据库连接初始实例
            sqlconnection con=new sqlconnection(system.configuration.configurationsettings.appsettings["con"]);
            return con;
        }
        public static bool chklog(string name,string pwd)
        {//判断数据库里是否存在这个用户,返回真或则假
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand("select count(*) from admin where name='"+name+"' and pwd='"+pwd+"'",con);
            int count=convert.toint32(cmd.executescalar());
            if(count>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static string chkqx(string name,string pwd)
        {//判断当前用户的权限,并返回权限
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand("select qx from admin where name='"+name+"' and pwd='"+pwd+"'",con);
            string qx=convert.tostring(cmd.executescalar());
            return qx;
        }
        public static datatable fill(string query)
        {//根据传来的sql语句查询出数据并填充一个表给查询对象
            sqlconnection con=db.con();
            sqldataadapter sda=new sqldataadapter();
            sda.selectcommand=new sqlcommand(query,con);
            dataset ds=new dataset();
            sda.fill(ds,"vote");
            return ds.tables["vote"];
          
        }
        public static string title(int id)
        {//(静态)根据传来的id返回该投票项标题
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand("select xiang from votemaster where id='"+id+"'",con);
            return cmd.executescalar().tostring();
        }
        public static void delete(string query)
        {//(静态)执行指定的删除行为
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand(query,con);
            cmd.executenonquery();
        }
        public static void update(string query)
        {//(静态)执行指定的更新行为
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand(query,con);
            cmd.executenonquery();
        }
        public static int cid(string query)
        {//(静态)根据执行的语句查询出当前数据库最新的一个id
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand(query,con);
            int id=convert.toint32(cmd.executescalar());
            return id;
        }
        public static bool insert(string query)
        {//插入操作
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand(query,con);           
            int count=convert.toint32(cmd.executenonquery());
            if(count>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static int typ(int id)
        {//根据出来的id,返回该id所属于的投票属于多选还是单选
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand("select typ from votemaster where id='"+id+"'",con);
            int typ=convert.toint32(cmd.executescalar());
            return typ;
        }
        public static string count()
        {//返回总访问人数
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand("select coun from countline",con);
            string count=cmd.executescalar().tostring();
            return count;
        }
        public static string rengyi(string query)
        {//查询任意单字段,返回字段值的一个静态方法
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand(query,con);
            return cmd.executescalar().tostring();
        }
        public static bool vlog(string query)
        {//查询任意单字段,返回真与假的一个静态方法
            sqlconnection con=db.con();
            con.open();
            sqlcommand cmd=new sqlcommand(query,con);
            int count=convert.toint32(cmd.executescalar());
            if(count>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表