标题:C#与数据库连接出错
只看楼主
wuyue970907
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-12-19
结帖率:0
已结贴  问题点数:5 回复次数:2 
C#与数据库连接出错
private void btnlogin_Click(object sender, EventArgs e)
        {
            //password=123456789;Persist Security Info=True;User ID=sa;Initial Catalog=学生成绩管理系统;Data =Source=LQ-PC\\SQLEXPRESS
            string connString = "Data Source=吴月-PC;Initial Catalog=学生成绩管理系统;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connString);
            string label2 = type.Text;
            string userName = txtname.Text;
            string password = txtpassword.Text;
            string sql = string.Format("select count(*) from 用户登录 where  用户名='{0}'and 密码='{1}'and 类别='{2}'", txtname.Text, txtpassword.Text,type.Text);


            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand(sql, connection);

                int num = (int)command.ExecuteScalar();
                if (num > 0)
                {
                    if (type.Text == "学生")
                    {
                        MessageBox.Show("欢迎进入学生成绩系统!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Form2 dd = new Form2();
                        dd.Show();
                        this.Visible = false;
                    }
                    else if (type.Text == "教师")
                    {
                        MessageBox.Show("欢迎进入学生成绩系统!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Form3 dd = new Form3();
                        dd.Show();
                        this.Visible = false;
                    }
                    else if (type.Text == "管理员")
                    {
                        MessageBox.Show("欢迎进入学生成绩系统!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Form4 dd = new Form4();
                        dd.Show();
                        this.Visible = false;
                    }

                }
                else
                {
                    txtpassword.Text = "";
                    MessageBox.Show("您输入的用户名或密码错误!", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
           /* catch (Exception ex)
            {
                MessageBox .Show(ex.Message ,"操作数据库出错!",MessageBoxButtons .OK,MessageBoxIcon.Exclamation);
            }*/
             finally
             {
                 connection.Close();
             }
            
            

           
       }

运行时出现:对象名,’用户登录‘无效,请问什么原因
搜索更多相关主题的帖子: private password 数据库连接 管理系统 
2013-12-19 19:39
shangsharon
Rank: 9Rank: 9Rank: 9
来 自:湖北武汉
等 级:蜘蛛侠
威 望:7
帖 子:221
专家分:1261
注 册:2012-3-25
得分:3 
string sql = string.Format("select count(*) from 用户登录 where  用户名='{0}'and 密码='{1}'and 类别='{2}'", txtname.Text, txtpassword.Text,type.Text);

表名,列明最好用"[]"括起来
string sql = string.Format("select count(*) from [用户登录] where  [用户名]='{0}'and [密码]='{1}'and [类别]='{2}'", txtname.Text, txtpassword.Text,type.Text);
2013-12-20 15:49
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
得分:3 
连接字符串给的不对。

♂ 死后定当长眠,生前何须久睡。♀
2013-12-20 16:14



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




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

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