ado.net实现一个简单的登陆

9 篇文章 0 订阅
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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            string pwd = txtPwd.Text;

            string connStr = @"Data Source=.\sqlexpress;Initial Catalog=MySchool;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(connStr)) //创建一个数据库连接
            {
                string sql = string.Format("select count(*) from [user] where uUserName=@name and uPwd=@pwd", name, pwd); 
                using (SqlCommand cmd = new SqlCommand(sql, conn))   
                {
                    conn.Open();
                    //通过sqlParameter类给sql变量赋值
                    SqlParameter sp = new SqlParameter("@name", name);
                    cmd.Parameters.Add(sp);
                    cmd.Parameters.Add(new SqlParameter("@pwd", pwd));

                    int n = Convert.ToInt32(cmd.ExecuteScalar());
                    if (n > 0)
                    {
                        MessageBox.Show("登录成功");
                    }
                    else
                    {
                        MessageBox.Show("登录失败");
                    }
                }
            }
        }
    }
}


Data Source=.\sqlexpress;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=sa


ado.net组成
 数据提供程序
  connection  //连接对象
   ConnectionString
   Open()
   Close()
  command
   CommandText
   Connection
   executeNonQuery() //执行增删改
   executeScalar() //执行查询返回首行首列
    insert into class() output inserted.列明 values()
    insert into class() values();select @@identity
   executeReader() //执行查询返回只读只进的结果集datareader
  datareader 只读只进 一次只读一条记录
      必须独享一个Connection
   hasRow  //是否有行
   fieldCount      //字段的个数
   read() 
   dr["列名"] //返回object  对的是select语句后的列明
   dr.GetString(序号) dr.GetOrdinal("列名")
   close()
  SqlParameter


  dataadapter
 数据集
  dataset

1、登陆程序
2、读取所有学生
3、数据导入导出
4、数据的增删改

 

 

实现检测用户名是否存在

string connStr = @"Data Source=.\sqlexpress;Initial Catalog=MySchool;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sql = "select uPwd from [user] where uUserName=@name";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.Add(new SqlParameter("@name", txtName.Text));
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        //读取DataReader中的行
                        //如果dataReader返回行,用户名存在,检查密码
                        //如果不存在用户名账号不存在
                        if (dr.Read())
                        {
                            string pwd = dr["uPwd"].ToString();
                            if (pwd == txtPwd.Text)
                            {
                                MessageBox.Show("登录成功");
                            }
                            else
                            {
                                MessageBox.Show("登录失败");
                            }
                        }
                        else
                        {
                            MessageBox.Show("用户名不存在");
                        }
                    }
                }
            }


 

实现三次错误冻结账号

string connStr = @"Data Source=.\sqlexpress;Initial Catalog=MySchool;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sql = "select uPwd,uTimes from[user] where uUserName=@name";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@name",txtName.Text);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        //判断用户是否真的存在
                        if (dr.Read())
                        {
                            string pwd = dr["uPwd"].ToString();
                            //错误次数
                            int times =Convert.ToInt32( dr["uTimes"]);

                            //关闭datareader,因为他独享conn
                            dr.Close();

                            if (times == 3)
                            {
                                MessageBox.Show("今天登录超过三次请改天登录");

                            }

                            else
                            {
                                string update = "";
                                if (pwd == txtPwd.Text)
                                {
                                    MessageBox.Show("登录成功");
                                    //错误次数清零
                                    update = "update [user] set uTimes = 0 where uUserName='" + txtName.Text + "'";
                                    
                                }
                                else
                                {
                                    MessageBox.Show("还剩"+( 2-times)+"次");
                                    //错误次数+1
                                    update = "update [user] set uTimes = uTimes+1 where uUserName='" + txtName.Text + "'";
                                    
                                }
                                
                                cmd.CommandText = update;
                                cmd.ExecuteNonQuery();
                            }
                        }
                        else
                        {
                            MessageBox.Show("用户名不存在");
                        }
                    }
                }
            }


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值