您的位置:首页 > 职场人生

黑马程序员-简单的登录练习

2014-05-05 19:10 417 查看
----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
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;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private bool isLogined = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string namme = textBox1.Text;
            string mima = textBox2.Text;
            string connString = @"Data Source=.;Intial Catalog=MySchool;Integated Security=true";
            string sql = string.Format("select count (*)from [User] where UserName='{0}'and Passname='{1}'",namme ,mima );
            SqlConnection conn = new SqlConnection(connString );
            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                int n = (int)comm.ExecuteScalar();
                if (n == 1)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Tag = true;
                }
                else
                {
                    MessageBox.Show("你输入的密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.Tag = false;

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误了", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Tag = false;

            }
            finally
            {
                conn.Close();
            
            }
            Login 
        }
    }
}
这种简单的登录已经可以简单的防止注入了。。。但是用的方法还是很老土的,在看视频的时候知道了其实我们在连接数据库的时候可以使用using的,这样我们就不用想着关闭连接了。。。加油吧,看看能否进去学习学习

----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------详细请查看:www.itheima.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: