| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1437 人关注过本帖
标题:操作数据库时的一些问题
取消只看楼主 加入收藏
notlook
Rank: 2
等 级:论坛游民
帖 子:73
专家分:37
注 册:2008-11-6
结帖率:80%
收藏
 问题点数:0 回复次数:3 
操作数据库时的一些问题
本人新手,最近刚接触用c#操作数据库,但老是出现问题
希望各位高手能给我举几个具体实例让我参考参考
在这里先谢谢啦
搜索更多相关主题的帖子: 数据库 
2008-11-19 00:04
notlook
Rank: 2
等 级:论坛游民
帖 子:73
专家分:37
注 册:2008-11-6
收藏
得分:0 
帮我看看这段代码撒,那你有问题
提示  “WindowsApplication78.dbhelper”的类型初始值设定项引发异常。

class dbhelper
    {
        private static string connstring = "Data Sourse=.;Initial Catalog=master;Integrated Security=true";
        public static SqlConnection connection = new SqlConnection(connstring);
    }

public bool ceshi(string Userid, string Userpwd, string Userlevel)
        {
            int count = 0;
            bool ceshi = false;
            if (Userlevel == "管理员")
            {
                string sql = string.Format("select count(*) from master UserInfo where Userid='{0}' and Userpwd='{1}'", Userid, Userpwd);


                try
                {
                    SqlCommand cmd = new SqlCommand(sql, dbhelper.connection);
                    dbhelper.connection.Open();
                    count = (int)cmd.ExecuteScalar();
                    if (count == 1)
                    {
                        ceshi = true;
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码不存在");
                        ceshi = false;

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    dbhelper.connection.Close();
                }
            }
            return ceshi;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           if(ceshi(username.Text,password.Text,userleixing.Text))
           {
               MessageBox.Show("登陆成功");
           }
        }


还有这段
为什么提示UserInfo无效,UserInfo明明有的啊

 public bool ceshi()
        {
            int count = 0;
            bool ceshi = false;
            string UserName = username.Text;
            string number = password.Text;
            string leixing = userleixing.Text;
            if (leixing == "管理员")
            {

                string aa = "Data Source=.;Initial Catalog=master;Integrated Security=true";
                SqlConnection con = new SqlConnection(aa);
                con.Open();
                string sql = string.Format("Select count(*) from UserInfo where Userid='{0}'and Userpwd='{1}'", UserName, number);
                SqlCommand cmd = new SqlCommand(sql, con);
                count = (int)cmd.ExecuteScalar();
                try
                {

                    if (count == 1)
                    {
                        ceshi = true;
                    }
                    else
                    {
                        MessageBox.Show("账户或密码错误");
                        ceshi = false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                }
            }
            return ceshi;
        }
 private void button1_Click(object sender, EventArgs e)
        {
            if (ceshi())
            {
                MessageBox.Show("成功");
            }
        }
2008-11-24 22:08
notlook
Rank: 2
等 级:论坛游民
帖 子:73
专家分:37
注 册:2008-11-6
收藏
得分:0 
按照楼上帖子里的写法还是会出现  提示UserInfo无效的错误
是不是数据库有问题啊??
string uname = this.textbox1.Text.ToString();
            string upassword = this.textbox2.Text.ToString();
            SqlConnection thisconnection = new SqlConnection(@"Server=.;Integrated Security=True;" + "DataBase=master");
            thisconnection.Open();
            SqlCommand thiscommand = thisconnection.CreateCommand();
             = "select Userid,Userpwd from UserInfo where Userid='" + uname + " '";
            SqlDataReader thisreader = thiscommand.ExecuteReader();
            if (thisreader.Read())
            {
                if (thisreader["upassword"].ToString().Trim() == upassword)
                {
                    MessageBox.Show("恭喜您登陆成功!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //yhxx tt = new yhxx();
                    //tt.Show();

                }
                else { MessageBox.Show("密码错误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); }

            }
            else { MessageBox.Show("此用户不存在,请您注册!", "注册", MessageBoxButtons.OK, MessageBoxIcon.Information); }
            thisconnection.Close();
            thisreader.Close();
2008-11-25 17:39
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.020814 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved