qq_38655109
2017-05-19 02:59
采纳率: 100%
浏览 1.6k

C#连接数据库实现登陆,现在连接不上数据库,请各位大佬指点一下代码哪有问题

namespace _日记本
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
            MessageBox.Show("用户名不能为空!", "提示");
        else if (textBox2.Text == "")
            MessageBox.Show("密码不能为空!", "提示");
        try //try...catch...异常处理语句
        {
            string name, pass;
            bool flag = false;
            name = textBox1.Text;
            pass = textBox2.Text; //获取用户名,密码
            string str = Properties.Settings.Default.Database1ConnectionString;//连接字符串
            SqlConnection Conn = new SqlConnection(str);//建立到数据库的连接

            Conn.Open(); //将连接打开
                           //SQL语句:从数据库的登录表中搜索登录名,密码
            string sqlstring = "select * from dbo.Table where Id ='" + name + "'and Password ='" + pass + "'";
            //执行con对象的函数,返回一个SqlCommand类型的对象
            SqlCommand command = new SqlCommand(sqlstring, Conn);
            //用cmd的函数执行语句,返回SqlDataReader对象thisReader,thisReader就是返回的结果集(也就是数据库中查询到的表数据)
            SqlDataReader thisReader = command.ExecuteReader();
            //判断用户名及密码是否正确,对flag进行赋值
            while (thisReader.Read())
            {
                if ((thisReader.GetValue(0).ToString().Trim()) == (name.ToString().Trim()))
                {
                    if (thisReader.GetValue(1).ToString().Trim() == pass.ToString().Trim())
                    {
                        flag = true;
                    }
                }
            }



            //用完后关闭连接,以免影响其他程序访问
            Conn.Close();
            if (flag)
            {
                MessageBox.Show("登陆成功!");
                Form2 F = new Form2(); //显示主页面
                F.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("请检查你的用户名和密码!");
                textBox1.Focus();
            }
        }
        catch (Exception ex2)
        {
            MessageBox.Show("连接远程SQL数据库发生错误:" + ex2.ToString(), "错误!");
        }

        }
    }



    }![图片说明](https://img-ask.csdn.net/upload/201705/19/1495162964_885089.jpg)
            表里就一个Id一个Password报错是这样的,是不是我的sql语句有语法错误,查了半天查不出来,小弟还是新手,请各位大手子指点一下,不胜感激
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • FEN_TA 2017-05-19 03:04
    已采纳

    Conn.Open(); //将连接打开
    //SQL语句:从数据库的登录表中搜索登录名,密码
    string sqlstring = "select * from dbo.Table where Id ='" + name + "'and Password ='" + pass + "'";
    SQL语句这里FROM 表名 就好了 dbo.Table这个应该是错的

    打赏 评论
  • 角刀牛 2017-05-19 03:09

    string sqlstring = "select * from dbo.Table where Id ='" + name + "'and Password ='" + pass + "'";

    and前面没有空格

    打赏 评论
  • qq_38655109 2017-05-19 04:09

    namespace _日记本
    {
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                MessageBox.Show("用户名不能为空!", "提示");
            else if (textBox2.Text == "")
                MessageBox.Show("密码不能为空!", "提示");
            try //try...catch...异常处理语句
            {
                string name, pass;
                bool flag = false;
                name = textBox1.Text;
                pass = textBox2.Text; //获取用户名,密码
                string str = Properties.Settings.Default.Database1ConnectionString;//连接字符串
                SqlConnection Conn = new SqlConnection(str);//建立到数据库的连接
    
                Conn.Open(); //将连接打开
                               //SQL语句:从数据库的登录表中搜索登录名,密码
                string sqlstring = "select * from Table where Id ='" + name + "' and Password ='" + pass + "'";
                //执行con对象的函数,返回一个SqlCommand类型的对象
                SqlCommand command = new SqlCommand(sqlstring, Conn);
                //用cmd的函数执行语句,返回SqlDataReader对象thisReader,thisReader就是返回的结果集(也就是数据库中查询到的表数据)
                SqlDataReader thisReader = command.ExecuteReader();
                //判断用户名及密码是否正确,对flag进行赋值
                while (thisReader.Read())
                {
                    if ((thisReader.GetValue(0).ToString().Trim()) == (name.ToString().Trim()))
                    {
                        if (thisReader.GetValue(1).ToString().Trim() == pass.ToString().Trim())
                        {
                            flag = true;
                        }
                    }
                }
    
    
    
                //用完后关闭连接,以免影响其他程序访问
                Conn.Close();
                if (flag)
                {
                    MessageBox.Show("登陆成功!");
                    Form2 F = new Form2(); //显示主页面
                    F.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("请检查你的用户名和密码!");
                    textBox1.Focus();
                }
            }
            catch (Exception ex2)
            {
                MessageBox.Show("连接远程SQL数据库发生错误:" + ex2.ToString(), "错误!");
            }
    
            }
        }
    
    
    
        }
    

    配置字符串是数据源生成的:
    namespace _日记本.Properties {

    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
    
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
    
        public static Settings Default {
            get {
                return defaultInstance;
            }
        }
    
        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database1.mdf" +
            ";Integrated Security=True")]
        public string Database1ConnectionString {
            get {
                return ((string)(this["Database1ConnectionString"]));
            }
        }
    }
    

    }
    错误提示

    打赏 评论
  • Table是关键字,要用中括号括起

    
     string sqlstring = "select * from dbo.[Table] where Id ='" + name + "'and Password ='" + pass + "'";
    
    打赏 评论

相关推荐 更多相似问题