using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Windows.Forms;namespace 成语词典
{
    public partial class loginForm : Form
    {
        public loginForm()
        {
            InitializeComponent();
        }        string str = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = D:\\showay.mdb";
        OleDbConnection conn = new OleDbConnection();
        OleDbDataReader result = null;
        public bool loginus(string un, string pw)
        {
            conn.ConnectionString = str;
            string sql = "select*from 用户表1 where 用户名 = '"+un+" 'and 密码 = '"+pw +"'";
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            cmd.Connection.Open();
            try
            {
                result = cmd.ExecuteReader();            }
            catch(Exception )
            {
                MessageBox.Show("对不起!查询数据失败!","提示");
            }
            if(result !=null)
            {
                if (result.Read())
                { return true; };
            }
            result.Close();
            cmd.Connection.Close();
            return false;
        }        private void button1_Click(object sender, EventArgs e)
        {
            string n = textBox1.Text.Trim();
            string p = textBox2.Text.Trim();
            if ((textBox1.Text != " ") & (textBox2.Text != " "))
            {
                if (loginus(n, p) == false)
                {
                    MessageBox.Show("用户密码错误!");
                    return ;
                }
                DialogResult  = DialogResult .OK ;
                this .Hide ();
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
这是个用于用户登录的,我用的是Access数据库,我做了个以showay命名的数据库,里面放了密码,可执行时老提示找不到数据库文件,好纠结哦希望哪位指点指点。

解决方案 »

  1.   

    string str = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = D:\\showay.mdb";mdb路径对不对?连接字符串对不对? string sql = "select * from 用户表1 where 用户名 = '"+un+" 'and 密码 = '"+pw +"'";
    你的sql语句该有空格的地方注意要有空格
      

  2.   

    楼主别的我不说,你看看这里,你怎么写的,这样岂不是总是返回false,那还执行个毛啊
    if(result !=null)
      {
      if (result.Read())
      { return true; };
      }
      result.Close();
      cmd.Connection.Close();
      return false;
      }

      

  3.   

    楼主好好看看我上面说的,你的这个函数永远返回false
    public bool loginus(string un, string pw)
      

  4.   

    请问最后的红色三条语句试试永远要执行 ,那请问是不是最后永远返回false,难道你还没看出来吗,或者我眼花了
      

  5.   

    楼主的程序没有问题,最后3句只有在result为null的情况下才会被执行到,这样写很正确也很高效
      

  6.   

    http://download.csdn.net/detail/xianfajushi/4028787