SqlConnection conn = new SqlConnection(连接数据库字符串);
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
SqlDataReader reader; comm.CommandText = strsql;//sql人员密码信息表
reader = comm.ExecuteReader ();
reader.Read();
if (!reader.HasRows )
{
reader.Close();
conn.Close();
MessageBox.Show("操作员登陆编码错误或者密码错误","注意",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
else
{reader[”用户名字段“].ToString() == “输入的用户名”
reader[”密码字段名“].ToString() == 输入的密码

解决方案 »

  1.   

    SqlConnection
    SqlCommand
    全报错..是不是前面要加载什么类文件?
      

  2.   

    sqlconnection con=new sqlconnection("server=.;uid=sa;pwd=;database=数据库名");
    sqldataadapter da =new sqldataadapter("select * from 表名" where name='"+name+"' and pwd='"+pwd+"',con);
    dataset ds =new dataset();
    da.fill(ds);if(ds.tables[0].rows.count>0)
    {
    messagebox.show(" 登陆成功!");}
    else{
    messagebox.show("帐号密码错!");
    }
      

  3.   

    看到十四楼的见解,真的有一种涣然一新的感觉,因为我以前都会另建一个类,来获取一个动态数组的。然后再在登录窗体把输入的信息与动态数组中的想对比。相比十四的方法,自己OUT了。但我想问一14楼的朋友,如果我想把登录的信信传到主窗体中去,该怎么传呢?因为我想在登录窗体中搞一个登录类型,然后在主窗体中以登录类型来给相应的权限。
    还有一个要请教的是,如何才能让登录窗体中的用户名comboBOX显示曾经登录过的用户呢?
      

  4.   

    还是建一个类来专门验证比较好,把界面和功能代码分开。这样才符合面向对象和维护。建一个类来处理验证,可移植性高,你其他程序要写登陆嗲吗,直接使用这个类一样的,换界面也不影响类的的使用。
    这个是登陆类,新建一个类放代码:
    class LonIn
        {        private string _UserID;//ID
            private string _PassWord;//密码
            public static string Group;//用户权限组,暂时你用不到
            private DataRow userRow;//数据行,存用户表中的一行
            public string UserID//ID属性
            {
                get
                {
                    return _UserID;
                }
                set
                {
                    _UserID = value;
                }
            }
            public string PassWord//PassWord属性
            {
                get
                {
                    return _PassWord;
                }
                set
                {
                    _PassWord = value;
                }
            }
            public LonIn(string UserID, string Password)
            {
                this.UserID = UserID;
                this.PassWord = PassWord;
              
            }
            public LonIn()
            {
            }
            public int isUser()
            {
                SqlConnection SqlCon = new SqlConnection();
                SqlCommand LogInCommand = new SqlCommand();
                SqlDataAdapter userAdapter = new SqlDataAdapter();
                DataSet UserInfoSet = new DataSet();
                try
                {
                    SqlCon.ConnectionString = "server=192.168.121.7;user=zhoukp;pwd=pop989;database=FSOFT;";//连接字段,自己根据情况修改
                    LogInCommand.Connection = SqlCon;
                    LogInCommand.CommandText = "SELECT UID,name,password From users";//从表里读取字段,自己修改对应的
                    userAdapter.SelectCommand = LogInCommand;
                    userAdapter.SelectCommand.Connection = SqlCon;
                    SqlCon.Open();
                    userAdapter.Fill(UserInfoSet, "UserInfo");//填充数据集
                    for (int i = 0; i < UserInfoSet.Tables["UserInfo"].Rows.Count; i++)
                    {
                        this.userRow = UserInfoSet.Tables["UserInfo"].Rows[i];
                        //只有当输入的用户名和密码同时对应上数据库中记录时,才能通过校验
                        if (userRow[1].ToString().Trim() == this.UserID.Trim() 
                            && userRow[2].ToString().Trim() == this.PassWord.Trim())
                        {
                            Group = "0";
                            return 1;                    }
                    }
                    return 0;
                }            catch (Exception ex)
                {
                    throw new ApplicationException("SysTem Err");
                }
            }
        }--------------------------------
    然后是登陆按钮//自己注意修改下控件名称
            private void sure_button_Click(object sender, EventArgs e)
            {
                if (this.textID.Text.Trim() == string.Empty || this.textPwd.Text.Trim() == string.Empty)        //没有输入用户ID和密码
                {
                   MessageBox.Show("请输入用户ID,密码!");
                   return;
                }
                LonIn User = new LonIn();
                User.UserID = this.textID.Text.Trim();
                User.PassWord = this.textPwd.Text.Trim();
                            
             
                   int intResult = User.isUser();//得到返回值
                    if (intResult == 1)         //ID和密码完全正确
                    {                    lfstate = true;
                        this.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("您输入的密码不正确!");
                        return;
                    }
                }
            }
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Collections;namespace 登陆窗体
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string sql = "select count(*) from 表名 where passuser='"+this.textBox1.Text+"' and password='"+this.textBox2.Text+"'";            SqlConnection conn = new SqlConnection("server=SQL server服务器名称;database=数据库名;uid=登录名(如 sa);pwd=密码(如:123);");
                try
                {
                    SqlCommand sqlcmd = new SqlCommand(sql,conn);
                    conn.Open();
                    int count = (int)sqlcmd.ExecuteScalar();
                    
                    if (count > 0)
                    {
                        MessageBox.Show("连接成功");                }
                    else
                    {
                        MessageBox.Show("连接失败");
                    }            }
                catch (SqlException ex)
                {                MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                }        }
      

  6.   

    你设计的程序里面 肯定有一个sql server帐号 帐号名称会很复杂 用于程序连接sql 只用然后让用户在窗体上输入帐号密码  
    这个帐号密码 只是sql server 里面某个表的数据 表示用户是否有权限登录系统