| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1439 人关注过本帖
标题:操作数据库时的一些问题
只看楼主 加入收藏
guxcy
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-11-4
收藏
得分:0 
大家多多共享啊
2008-11-25 20:30
notlook
Rank: 2
等 级:论坛游民
帖 子:73
专家分:37
注 册:2008-11-6
收藏
得分:0 
我想调用下面类中 Getdataset中的ds在窗体上用,但调不出来,谁帮我看看要怎么写啊


class DataAccess
    {
        public static string Connectionstring = "Data Source=.;Initial Catalog=CJGL;Integrated Security=true";
        public bool ExecuteSql(string sql)
        {
            SqlConnection con = new SqlConnection(DataAccess.Connectionstring);
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                con.Close();
                con.Dispose();
                cmd.Dispose();
            }
        }
        public SqlDataReader Getreader(string sql)
        {
            SqlConnection con = new SqlConnection(DataAccess.Connectionstring);
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader dr = null;
            try
            {
                con.Open();
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch(Exception ex)
            {            
                dr.Close();//每次调用完DataReader对象后都要关闭的
                con.Dispose();
                cmd.Dispose();
                throw new Exception(ex.ToString());
            }
                return dr;
        }
        public  DataSet Getdataset(string sql, string tablename)
        {
            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(DataAccess.Connectionstring);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            try
            {
                da.Fill(ds, tablename);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                con.Close();
                con.Dispose();
                da.Dispose();
            }
            return ds;
        }
        public int Getcount(string sql)
        {
            SqlConnection con = new SqlConnection(DataAccess.Connectionstring);
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                con.Open();
                int count = (int)cmd.ExecuteScalar();
                return count;
            }
            catch
            {
                return 0;
            }
            finally
            {
                con.Close();
                con.Dispose();
                cmd.Dispose();
            }
        }
        public  bool Checkadmin(string strname, string strpwd)
        {
            string sql;
            //strname =NameReplace(strname);
            sql = "select count(1) from UserInfo where Userid='"+strname+"'and Userpwd='" + strpwd + "'";
            if (Getcount(sql) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
2008-11-27 21:55
快速回复:操作数据库时的一些问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026617 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved