标题:visual studio 2005连接SQL2000数据库,哪里出错啦。高手们指点下~
只看楼主
gaogaoggs
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-5-8
结帖率:0
已结贴  问题点数:20 回复次数:3 
visual studio 2005连接SQL2000数据库,哪里出错啦。高手们指点下~
private void Form1_Load(object sender, EventArgs e)
        {
            string strConn = "server=.;user id=dbo;pwd=;database=admin";
            SqlConnection con = new SqlConnection(strConn);
            string sql = "select * from adminpswd";
            SqlDataAdapter ada = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            ada.Fill(ds);

        }
        private void button1_Click(object sender, EventArgs e)
        {
         
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空!", "错误");
            }
            else if (this.maskedTextBox1.Text == "")
            {
                MessageBox.Show("密码不能为空!", "错误");
            }
            else
            {
                if (IsUser(this.textBox1.Text))
                {
                    if (this.maskedTextBox1.Text == LoginUser(this.textBox1.Text))
                    {
                        Form2 frm2 = new Form2();
                        // //frm2.showdialog();
                        frm2.Show();
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("密码错误!");
                    }
                }
                else
                {
                    MessageBox.Show("用户名错误!");
                }
            }

        }

        private string LoginUser(string uName) //检测用户是否能够登录
        {
            DataSet ds = new DataSet();
            return ds.adminpswd[0].Rows[0]["密码"].ToString();
            con.Close();
        }


        private bool IsUser(string uName) //检测用户输入的用户名是否存在
        {
            DataSet ds = new DataSet();
            ada.Fill(ds, "adminpswd");

            int n;
            n = ds.adminpswd[0].Rows.Count;
            if (n > 0)
                return true;//存在就返回true
            else
                return false;

            con.Close();
        }

        private void button2_Click(object sender, EventArgs e)//button2的 Text属性是:取消登录
        {
            Application.Exit();//用户取消登录,退出系统
        }
搜索更多相关主题的帖子: 数据库 visual studio 
2010-05-08 21:05
gaogaoggs
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-5-8
得分:0 
说是,系统知不道adminpswd...
2010-05-08 21:06
hainandgm
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:111
专家分:475
注 册:2007-6-21
得分:20 
LZ,你的代码中好多都是定义了局部变量,外部函数怎能访问这些变量呢,注译如下:
private void Form1_Load(object sender, EventArgs e)
        {
            string strConn = "server=.;user id=dbo;pwd=;database=admin";
            SqlConnection con = new SqlConnection(strConn);
            string sql = "select * from adminpswd";
            SqlDataAdapter ada = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            ada.Fill(ds);

        }
        private void button1_Click(object sender, EventArgs e)
        {
         
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空!", "错误");
            }
            else if (this.maskedTextBox1.Text == "")
            {
                MessageBox.Show("密码不能为空!", "错误");
            }
            else
            {
                if (IsUser(this.textBox1.Text))
                {
                    if (this.maskedTextBox1.Text == LoginUser(this.textBox1.Text))
                    {
                        Form2 frm2 = new Form2();
                        // //frm2.showdialog();
                        frm2.Show();
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("密码错误!");
                    }
                }
                else
                {
                    MessageBox.Show("用户名错误!");
                }
            }

        }

        private string LoginUser(string uName) //检测用户是否能够登录
        {
            DataSet ds = new DataSet();
            return ds.adminpswd[0].Rows[0]["密码"].ToString();         //ds的数据集何来?
            con.Close();                                               //con的变量定义?是Load事件函数的局部变量吗?如果是,就不能这样访问
                                                                       //最后,前一行已使用了"return","con.Close()"就不能执行了
                                                                       //建议:将"con.Close()"放在"return"行前面
        }


        private bool IsUser(string uName) //检测用户输入的用户名是否存在
        {
            DataSet ds = new DataSet();
            ada.Fill(ds, "adminpswd");                            //ada定义何在?如果是Load事件函数中的局部变量,不能这样访问

            int n;
            n = ds.adminpswd[0].Rows.Count;
            if (n > 0)
                return true;//存在就返回true
            else
                return false;

            con.Close();                                           //con的变量定义?是Load事件函数的局部变量吗?如果是,就不能这样访问
                                                                   //上面已经使用了"return","con.Close()"不能再执行了
                                                                   //建议:将"con.Close()"放在If前面
        }

        private void button2_Click(object sender, EventArgs e)//button2的 Text属性是:取消登录
        {
            Application.Exit();//用户取消登录,退出系统
        }

建议:
将con和ada定义为全局变量来测试
2010-05-11 11:09
lixiaoyu1987
Rank: 1
等 级:新手上路
帖 子:6
专家分:4
注 册:2009-12-27
得分:0 
试试这个数据库连接
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = "Persist Security Info=False;User id=dbo;pwd=;                                  database=admin;server=(local)";
myCon.Open();
2010-05-23 22:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-305885-1-1.html




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

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