用C#制作的登录界面如何连接到数据库进行账号,密码的认证???????

解决方案 »

  1.   

    汗! 你新手? 还是不回C#?
    新建一个页面 拖几个控件,再在CS文件写操作,连接数据库方法不就好了?
      

  2.   

    如果你的DLL给其他NET平台上的托管代码用,就如二楼的那样写就行了。如果你要给其他非托管代码调用,只能写成COM组件
      

  3.   

    你先先看看C#的基础,研究下ADO.NET
      

  4.   

    要看你是winfrom,还是web页面,都需要跟数据库打交道。
    总体思路就是从界面上的用户名+密码,作为数据库的查询条件,如果在数据库中找到相应记录,说明用户名+密码正确,就可以登录了。如果没有找到相应记录,就给出提示,不让登录就行了!
      

  5.   

      先去看看书,ado。net方面的。
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Data.SqlClient;
    using System.Text;
    using System.Windows.Forms;namespace ERP_FZ
    {
        public partial class Form1 : Form
        {
            DataClass.Class1 ID_Name = new ERP_FZ.DataClass.Class1();
                    public Form1()
            {
                InitializeComponent();
            }
            SqlConnection  conn;
            
            
            private void button3_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Trim () == "" || textBox2.Text.Trim () == "")
                {
                    MessageBox.Show("绋娣彐桢犱稿负绌猴?);
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox1.Focus();
                }
                else
                {
                    try
                    {
                        if (conn.State == ConnectionState.Open)   //澶姝姣濡驽兼版?
                        {
                            SqlCommand cmd = new SqlCommand();
                            cmd.Connection = conn;
                            cmd.CommandText = "select count(*) from data001 where id='" + textBox1.Text.Trim() + "'and  pw='" + textBox2.Text.Trim() + "' ";
                            cmd.CommandType = CommandType.Text;
                            int i = Convert.ToInt32(cmd.ExecuteScalar());
                            if (i > 0)
                            {
                                DataClass.Class1.ID_Name = textBox1.Text.Trim();
                                conn.Close();
                                this.Hide();
                                Form2 frm2 = new Form2();
                                frm2.Show();
                               
                            }
                            else
                            {
                                textBox1.Text = "";
                                textBox2.Text = "";
                                textBox1.Focus();
                                MessageBox.Show("绋娣彐桢犳璇艰殒拌惧ワ?);
                            }
                        }
                        else
                        {
                            MessageBox.Show("缃缁楫棰瀵艰存版鸿挎ヤ告甯革?);
                        }                }                catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    }
                }
                    private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    textBox1.Focus();
                    conn = new SqlConnection("server=168.1.1.242;database=ERP2010;uid=sa;pwd=123456");
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }        }        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == '\r')
                    textBox2.Focus();
            }        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == '\r')
                    button3.Focus();
            }        private void button2_Click(object sender, EventArgs e)
            {
               conn.Close();
               conn.Dispose();
               Application.Exit();
                
              
               
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
              
                   conn.Close();
                   Application.Exit();
              
                        }
        }
    }
      

  7.   

    建议网上搜搜例子(就在csdn找就好),这样的例子很多的,里面有很详细的方法
      

  8.   

    晕死 拖几个控件。然后写SQL语句进行判断就OK了
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Data.SqlClient;
    using System.Text;
    using System.Windows.Forms;namespace ERP_FZ
    {
      public partial class Form1 : Form
      {
      DataClass.Class1 ID_Name = new ERP_FZ.DataClass.Class1();
         public Form1()
      {
      InitializeComponent();
      }
      SqlConnection conn;
       
       
      private void button3_Click(object sender, EventArgs e)
      {
      if (textBox1.Text.Trim () == "" || textBox2.Text.Trim () == "")
      {
      MessageBox.Show("绋娣彐桢犱稿负绌猴?);
      textBox1.Text = "";
      textBox2.Text = "";
      textBox1.Focus();
      }
      else
      {
      try
      {
      if (conn.State == ConnectionState.Open) //澶姝姣濡驽兼版?
      {
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = conn;
      cmd.CommandText = "select count(*) from data001 where id='" + textBox1.Text.Trim() + "'and pw='" + textBox2.Text.Trim() + "' ";
      cmd.CommandType = CommandType.Text;
      int i = Convert.ToInt32(cmd.ExecuteScalar());
      if (i > 0)
      {
      DataClass.Class1.ID_Name = textBox1.Text.Trim();
      conn.Close();
      this.Hide();
      Form2 frm2 = new Form2();
      frm2.Show();
       
      }
      else
      {
      textBox1.Text = "";
      textBox2.Text = "";
      textBox1.Focus();
      MessageBox.Show("绋娣彐桢犳璇艰殒拌惧ワ?);
      }
      }
      else
      {
      MessageBox.Show("缃缁楫棰瀵艰存版鸿挎ヤ告甯革?);
      }  }  catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      }
      }
         private void Form1_Load(object sender, EventArgs e)
      {
      try
      {
      textBox1.Focus();
      conn = new SqlConnection("server=168.1.1.242;database=ERP2010;uid=sa;pwd=123456");
      conn.Open();
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }  }  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
      if (e.KeyChar == '\r')
      textBox2.Focus();
      }  private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
      {
      if (e.KeyChar == '\r')
      button3.Focus();
      }  private void button2_Click(object sender, EventArgs e)
      {
      conn.Close();
      conn.Dispose();
      Application.Exit();
       
       
       
      }  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
       
      conn.Close();
      Application.Exit();
       
         }
      }
    }
    这可以
      

  10.   

    MessageBox.Show("绋娣彐桢犳璇艰殒拌惧ワ?);---这是哪国文字啊?
      

  11.   

    MessageBox.Show("文字说明 ","提示名称"?);---这是哪国文字啊?中国的文字 不过是乱码的中国的文字
      

  12.   

    看看下面这个例子里的登录界面是你所需要的吗?
    http://download.csdn.net/source/2418601
      

  13.   

    楼主,如果是web的在51aspx.com里找点例子看看。什么都是不会到会的,没事,有钻研精神就好。嘿嘿。加油
      

  14.   

    运行结果怎么是这样,求解释,先谢谢了.
    找不到类型或命名空间名称“DataClass”(是否缺少 using 指令或程序集引用?)
      

  15.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace Login
    {
        public partial class Login : Form
        {
            public Login()
            {
                InitializeComponent();
            }
            //退出按钮消息提示,是否确认退出
            private void button2_Click(object sender, EventArgs e)
            {
                DialogResult result;
                result = MessageBox.Show("你确定要退出?", "退出提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    result = MessageBox.Show("你真的确定要退出?", "退出提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        this.Close();
                    }
                }        }
            //判断用户是否输入用户名密码,选择用户类型        private bool ValidateInput()
            {
                if (txtLoinId.Text.Trim() == "")
                {
                    MessageBox.Show("请输入用户名");
                    txtLoinId.Focus();
                    return false;
                }
                else if (txtPassword.Text.Trim() == "")
                {
                    MessageBox.Show("请输入密码");
                    txtPassword.Focus();
                    return false;
                }
                else if (cboType.Text.Trim() == "")
                {
                    MessageBox.Show("请选择用户类型");
                    cboType.Focus();
                    return false;
                }
                else
                {
                    return true;
                }        }        //验证用户是否有效,--------------分方法做各个功能----------
            //private bool ValidateUser()
            //{
            //    //如果监控没有异常,则ValidateUser()的值为true;如果监控出现异常,那么ValidateUser()的值为false        //    bool isfind = false; //默认未找到用户
            //    try//只是执行一个监控的任务
            //    {
            //        //打开连接,链接数据库,好接收从数据库中传来的信息
            //        DBHelper.connection.Open();        //        //SQL语句。将我们要对数据库操作的语句赋给一个字符串        //        string sql = "";
            //        switch (cboType.Text)
            //        {
            //            case "学员":
            //                sql = string.Format("select count(*) from student where loginid ='{0}'and loginpwd ='{1}' ", txtLoinId.Text, txtPassword.Text);
            //                break;
            //            case "管理员":
            //                sql = string.Format("select count(*) from admin where loginid='{0}' and loginpwd ='{1}'", txtLoinId.Text, txtPassword.Text);
            //                break;
            //            default:
            //                sql = string.Format("select count(*) from teacher where loginid='{0}' and loginpwd ='{1}'", txtLoinId.Text, txtPassword.Text);
            //                break;
            //        }
            //        //命令执行。使用connection的对象和sql字符串
            //        SqlCommand cmd = new SqlCommand(sql, DBHelper.connection);        //        //执行操作
            //        int count = Convert.ToInt32(cmd.ExecuteScalar());
            //        if (count > 0)
            //        {
            //            isfind = true;
            //            string ff = cboType.Text + "数据库中有" + count + "条数据";
            //            MessageBox.Show(ff, "显示提示");
            //        }
            //        else
            //        {
            //            //MessageBox.Show("用户名和密码不存在,请重新输入!", "输入提示",MessageBoxButtons .OKCancel ,MessageBoxIcon.Information );
            //            // return false ;
            //            //消息框的返回值是一个dialogresult类型
            //            DialogResult result;
            //            result = MessageBox.Show("用户名和密码不存在,请重新输入!", "输入提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);        //            if (result == DialogResult.OK)
            //            {        //                result = MessageBox.Show("你选择了确定按钮,确定要离开吗!", "输入提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            //                if (result == DialogResult.OK)
            //                {        //                    DialogResult result1 = MessageBox.Show("你真的要离开吗?考虑一下吧", "");
            //                    if (result == DialogResult.OK)
            //                    {        //                        MessageBox.Show("滚吧!");
            //                    }
            //                    else
            //                    {        //                        MessageBox.Show("你觉悟了!还不算晚!");
            //                    }
            //                }
            //                else
            //                {        //                    MessageBox.Show("真好,你觉悟了!");        //                }
            //            }
            //            else
            //            {        //                MessageBox.Show("你选择了取消按钮", "输入提示");
            //            }
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        //提示错误
            //        MessageBox.Show(ex.Message);
            //    }
            //    finally
            //    {
            //        //关闭连接
            //        DBHelper.connection.Close();
            //    }
            //    return isfind;
            //}        private void type()
            {            switch (cboType.Text)
                {
                    case "管理员":
                        AdminForm Admin = new AdminForm();
                        Admin.Show();
                        this.Visible = false;
                        break;
                    case "教员":
                        jiayuan jy = new jiayuan();
                        jy.Show();
                        break;
                    case "学员":
                        TianJiaXY xy = new TianJiaXY();
                        xy.Show();
                        break;
                    default:
                        MessageBox.Show("请检查输入类型", "输入提示");
                        break;
                }        }        private void button1_Click(object sender, EventArgs e)
            {
                if (ValidateInput())
                {
                    //先保存,后打开窗体
                    //if (ValidateUser())
                    //{
                        //将输入的用户名保存到静态变量中
                        Userhelper.loginId = txtLoinId.Text;
                        //将选择的登录类型保存到静态变量中
                        Userhelper.loginType = cboType.Text;
                        type();                //}
                    //else
                    //{
                    //    return;                //}            }
            }    }
    }
      

  16.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using FileOpeat.DataSetLoginTableAdapters;namespace FileOpeat
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.skinEngine1.SkinFile = "MSN.ssk";
            }        private void button2_Click(object sender, EventArgs e)
            {
                this.txtname.Clear();
                this.txtpwd.Clear();
            }        /// <summary>
            /// 主要进行登录操作;
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                if(txtname.Text==""||txtpwd.Text==""){                MessageBox.Show("请输入数据 ","提示",MessageBoxButtons.OK,MessageBoxIcon.Question);                return;
                }
                tb_userinfoTableAdapter userinfo = new tb_userinfoTableAdapter();
                    FileOpeat.DataSetLogin.tb_userinfoDataTable usertalbe=userinfo.GetDataByGetName(txtname.Text.Trim());
                        if(usertalbe.Count<=0){                        MessageBox.Show("用户名错误","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);                        return;
                        }
                    ///用户名存在,进行下面的操作;获取行
                        for (int i = 0; i < usertalbe.Rows.Count;i++ )
                        {/// 获取行数                        FileOpeat.DataSetLogin.tb_userinfoRow rowname = usertalbe[i];
                            string username = rowname.username;
                            string userpwd = rowname.userpwd;
                            if(username!=txtname.Text||userpwd!=txtpwd.Text){                            MessageBox.Show("输入的数据有误","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                                return;
                            }
                            ///数据正确,登录成功!
                            MainShow mainshow = new MainShow();
                            this.Hide();
                            mainshow.Show();                    }
            
              
            }        private void txtname_TextChanged(object sender, EventArgs e)
            {        }
        }
    }