!!LoginWindow.cs

LoginWindow   

UI层

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LoginWindow.cs
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
                        if(txt1.Text.Length<=0)
                         {
                                MessageBox.Show("请输入用户名");
                                return;
                            }
                        if(pwd.Password.Length<=0)
                           {
                                MessageBox.Show("密码不为空");
                               return;
                            }
                           string user=txt1.Text;
                            User  users= UserDAL.GetUser(user);
                   if (users==null)
                           {
                                      MessageBox.Show("用户名不存,请先注册");
                                     return; 
                           }
                    else
                            { 
                                if(user.errotimes>=3)
                                  {
                                         Messagebox.Show("输入查过三次,输入停止五分钟")
                                        return;
                                   }  
                               string pwd=GetMD5("pwd.password"+hao@?Hiha);
                         
                                  if ( pwd !==user.PassWord)
                                      {
                                         UserDAL.Upedate(user.id);
                                      }
                                else
                                      {
                                        Messagebox.show("登录成功");
                                      }
                     }
                   
        }
    }
}

 

  User类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoginWindow.cs
{
    class User
    {
        public long ID { get; set; }
        public string UserName { get; set; }
        public string PassWord { get; set; }
        public string  ErroTimes { get; set; }

    }
}

  

 

 

数据库访问层

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace LoginWindow.cs
{
    class UserDAL
    {

 public static User GetRow(DataRow row )
      {         User user=new user();
                user.ID=(long)row["id"];
                user.UserName=(string)row["UserName"];
                user.PassWord=(string)row["Password"];
                user.Errotimes=(string)row["errotimes"];
             return   user;
 }

        public static User GetUser(string user) 

        {

           DataTable table=SqlHelper.ExecuteDataTable("select*from T_Users where username=@username", new SqlParameter("@username", user));

       
        if (table.rows.count<=0)
          {
            return  null;
           }
      else{  
              if(table.rows.Count>1)
               {
                throw new Exception("用户名重复");
                }
           else{
                DataRow row=table.rows[0];
                return GetRow(row);
             }
          }     

public static void Upedate(long id) { SqlHelper.ExecuteNonQuery("Update t_Users set ErroTimes=Errotimes+1 where id=@id", new SqlParameter("@id", id)); } } }

 

  

 

Config 应用程序配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="dbconnStr" connectionString="server=PC-20150619EMUE\ROCKYOU;database=test;user=jasvdj;pwd=csad"/>
  </connectionStrings>
</configuration>

  

 

SqlHelper

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


namespace LoginWindow.cs
{
    class SqlHelper
    {
        public static string connStr =ConfigurationManager.ConnectionStrings["dbconnstr"].ConnectionString;
        public static void ExecuteNonQuery(string sql ,params SqlParameter[] parameters) 
        {using(SqlConnection conn=new SqlConnection(@connStr)           )
        {
            conn.Open();
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = sql;
                cmd.Parameters.AddRange(parameters);
                 cmd.ExecuteNonQuery();
            }        
        }}
        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(@connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }
   
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);
                    return dataset.Tables[0];
                }
            }



        }
    }
}

  

posted on 2015-12-10 11:48  闫科达  阅读(246)  评论(0编辑  收藏  举报

导航