ADO.NET_2

今天和昨天一样也学了些语句,还有一些概念性的问题内容如下:

代码方面:感觉没学什么新的内容,只是在昨天的基础上弄明白了昨天那几句话的确切含义,今天用起来也是得心应手了些,不像昨天那样亦步亦趋了,代码是以制作登录这样的小程序作依托的,也见识到了注册漏洞的危险,理解了用参数传递值的重要意义,虽然参数用处远不止减少漏洞这一块。

今天的练习内容是个登录三次错误随即不能继续登录的小程序,看了一两遍视频便写出来了,代码如下:

 private void Times()

        {

            using (SqlConnection conn = new SqlConnection("Data Source=EAVE;Initial Catalog=firstdata;Integrated Security=True"))

            {

                conn.Open();

                using (SqlCommand cmd = conn.CreateCommand())

                {

                    cmd.CommandText = "updata lianxi set times=times+1 where username=@n";

                    cmd.Parameters.Add(new SqlParameter("name", textBox1.Text));

                }

            }

        }

 

        private void TimesO()

        {

            using (SqlConnection conn = new SqlConnection("Data Source=EAVE;Initial Catalog=firstdata;Integrated Security=True"))

            {

                conn.Open();

                using (SqlCommand cmd = conn.CreateCommand())

                {

                    cmd.CommandText = "updata lianxi set times=0 where username=@n";

                    cmd.Parameters.Add(new SqlParameter("username", textBox1.Text));

                }

            }

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            using (SqlConnection conn = new SqlConnection("Data Source=EAVE;Initial Catalog=firstdata;Integrated Security=True"))

            {

                conn.Open();

                using (SqlCommand cmd = conn.CreateCommand())  

                {

                    cmd.CommandText = "select * from lianxi where username=@n";

                    cmd.Parameters.Add(new SqlParameter("n", textBox1.Text));

                    using (SqlDataReader reader = cmd.ExecuteReader())

                    {

                        if (reader.Read())

                        {

                            int time = reader.GetInt32(reader.GetOrdinal("times"));

                            if (time > 3)

                            {

                                MessageBox.Show("Times of longining was too often");

                                return;

                            }

                            string pass = reader.GetString(reader.GetOrdinal("id"));

                            if (pass == textBox2.Text)

                            {

                                TimesO();

                                MessageBox.Show("login succeed");

                            }

                            else

                            {

                                Times();

                                MessageBox.Show("Login fail!");

                            }

                        }

                    }

用的窗体应用,开始程序出错,弄明白不能同时存在两个sqldatareader连接,于是把密码验证正误后的次数累计方法封装起来,语法是没什么错误了,但还是不能判定密码正误,逐步运行之后发现if(pass==textBox2.text)判定无误,但是不接着执行TimesO()方法而直接跳到下面的Times()去了,百思不解。望大家留言帮帮忙。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值