Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

visual đăng nhập

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;

namespace WindowsFormsApplication1

{

    public partial class DangNhap : Form

    {

        public DangNhap()

        {

            InitializeComponent();

        }

        #region "Khai báo biến sử dụng cho việc truy cập cơ sở dữ liệu"

        //đối tượng kết nối

        private SqlConnection cn;

        // đối tượng thực hiện lệnh

        private SqlCommand cmd;

        //đối tượng lọc dữ liệu

        private SqlDataAdapter da;

        //đối tượng chứa dữ liệu tạm thời.

        private DataSet ds;

        #endregion

        #region"viết hàm đăng nhập "

        public bool Dangnhaphethong()

        {

            bool kq = false;

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (hiển thị <xuất dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd = cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "Dangnhap";

            SqlParameter pNguoidung = new SqlParameter("@Nguoidung", SqlDbType.NVarChar);

            pNguoidung.Value = txtUser.Text;

            cmd.Parameters.Add(pNguoidung);

            SqlParameter pMatkhau = new SqlParameter("@Matkhau", SqlDbType.NVarChar);

            pMatkhau.Value = txtPass.Text;

            cmd.Parameters.Add(pMatkhau);

            //b3: đổ dữ liệu ra dataset thông qua cầu nối DataAdapter

            da = new SqlDataAdapter(cmd);

            ds = new DataSet("bảng chứa dữ liệu");

            //sử dụng phương thức Fill() của dataAdapter

            da.Fill(ds);

            //b4: Đóng kết nối lại

            cn.Close();

            //kiểm tra dữ liệu 

            if (ds.Tables[0].Rows.Count>0)

            {

                MessageBox.Show("Đăng nhập thành công");

                this.Close();

                tienich.username = ds.Tables[0].Rows[0]["Nguoidung"].ToString();

                kq = true;

                // enable =true cho các chức năng của phần

                MDIForm.ActiveForm.Controls["label1"].Text = "Người dùng: " + tienich.username;

            }

            else

            {

                MessageBox.Show("Đăng nhập không thành công");

                kq = false;

            }

            return kq;  

        }

        #endregion

        private void button1_Click(object sender, EventArgs e)

        {

            tienich.trangthai = Dangnhaphethong();

            if (tienich.trangthai==true)

            {

                //MDIForm.ActiveForm.Controls["menuStrip1"].Visible = false;

               // MDIForm.ActiveForm.Controls["menuStrip1"].Visible = false;

                //MDIForm.ActiveForm.Controls["menuStrip2"].Visible = true;

                //MDIForm.ActiveForm.Controls["menuStrip1"].Enabled = false;

               // MDIForm.ActiveForm.Controls["trộnĐềThiToolStripMenuItem"].Enabled = true;

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }  

        private void DangNhap_KeyDown(object sender, KeyEventArgs e)

        {

            if (e.KeyValue == 13)

            {

                Dangnhaphethong();

            }

        }

    }

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: