ADO.NET_2

来源:互联网 发布:马尔可夫 知乎 编辑:程序博客网 时间:2024/05/18 09:50

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

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

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

 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()去了,百思不解。望大家留言帮帮忙。

原创粉丝点击