c#登录实现到socket通信

本文档展示了如何使用C#实现登录窗体并通过Socket进行通信。主要涉及登录窗体、主窗体的创建,以及Socket服务的启动、查询用户信息、连接数据库和数据查询等功能。
摘要由CSDN通过智能技术生成

登录窗体:

using MyWPFTest.bean;

using MyWPFTest.domain;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Media;


namespace MyWPFTest
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        SqlConnection con = null;
        UserLogin userLogin = null;
        private int errorTime = 3;  //计时


        public LoginWindow()
        {
            InitializeComponent();
            this.WindowStyle = System.Windows.WindowStyle.None;//去掉边框
            this.AllowsTransparency = true;//透明
           // this.Background = Brushes.Transparent;//背景透明5
        }


        public LoginWindow(SqlConnection con)
        {            
            InitializeComponent();
            this.con = con;
        }


        public LoginWindow(UserLogin user)
        {       
            InitializeComponent();
            this.userLogin = user;
        }


        //连接db
        private DataSet getSql(String sql, String table)
        {
            string severAddr = "DESKTOP-LLMFC4D";
            // String userName = "sa";
            //String password = "root";
            try{
                SqlConnection con = MSSQLDAO.MSSQLDAO.getCon(severAddr, "sa", "root", "123");
                if (con != null){
                    DataSet ds = MSSQLDAO.MSSQLDAO.getDataSet(sql, table, con);
                    //con.Close();
                    return ds;
                }
                else
                {                 
                    return null;
                }
            }
            catch (Exception ex) {
                MessageBox.Show("数据库连接失败!"+ex.ToString());
                return null;
            }
        }


        //登录事件处理
        private void login_Click(object sender, RoutedEventArgs e)
        {              
            String name = this.user.Text.Trim();
            String pwd = this.password.Password.Trim();
           
            try {
                if ((name != "") && (pwd != ""))
                {
                    String table = "t_user";  //所要操作的表
                    string sql = "select userName,userPassword from t_user where userName='" + name + "' and userPassword='" + pwd + "'";
                    DataSet ds = getSql(sql, table);


                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        Object o = MessageBox.Show("登录成功!");             //登录成功
                                                                         //this.Close();                  
                        new MainWindow().Show();
                        this.Close();
                    }
                    else {
                        MessageBox.Show("登录失败,用户名或密码不正确!");
                        this.password.Password = null;
                    }
                }
                else if (name.Equals("admin") || name.Equals("1"))   //测试
                {
                    new MainWindow().Show();
                    this.Close(); ;
                }
                else
                {
                    MessageBox.Show("登录失败,用户名或者密码不能为空!");
                }            
            }  catch (Exception ex) {
                MessageBox.Show("登录失败....!"+ex.Message);
            }
        }


        //取消事件处理 ,退出程序
        private void cancel_Click(object sender, RoutedEventArgs e)
        {         
            this.Hide();
            this.Close();
            Application.Current.Shutdown();
           // Application.Current.Exit;
        }

//登录db
        private void admin_Click(object sender, RoutedEventArgs e)
        {
            User user = new User();
            user.SeverAddr = "";
            user.UserName = "";
            user.Password = "";
            user.Database = "123";
            this.Hide();
            new MainWindow(user).Show();
        }         
    }

}

登录成功窗体--主窗体:

using MyWPFTest.bean;
using MyWPFTest.model;
using MyWPFTest.socket;
using MyWPFTest.view;
using System;


using System.Data;
using System.Data.SqlClient;


using System.Windows;
using System.Windows.Controls;


namespace MyWPFTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Server server = null;      //当前socket服务
        SqlConnection con = null;
        User user=null;
        Customer customer = null;
      
        public MainWindow()
        {
            InitializeComponent();
        }


        public delegate void ServerPrint(String info);


        public MainWindow(SqlConnection con)
        {
            this.con = con;
            InitializeComponent();
        }


        public MainWindow(User user)
        {
            this.user = user;
            InitializeComponent();
            //this.user = user;
        }


        //连接db
        private DataSet getSql(String sql, String table)
        {
            string severAddr = "WIN-01801291735\\JUNSQLSERVER";
            SqlConnection con = MSSQLDAO.MSSQLDAO.getCon(severAddr, this.user.UserName, this.user.Password, this.user.Database);
            DataSet ds = MSSQLDAO.MSSQLDAO.getDataSet(sql, table, con);
            con.Close();
            return ds;
        }


        //查询处理
        private void query_Click(object sender, RoutedEventArgs e)
        {
            //String table = "t_user";  //所要操作的表
            //String sql = "select userName,userPassword from t_user order by id"; 
                                            
        }


        /*查询用户缴费信息事件处理---打开查询窗口*/
        private void query_exchang_Click(object sender, RoutedEventArgs e)
        {
            //假如没有开启服务,则开启,否则就可以查询信息
            new QueryByInfo().Show();
            //把查询结果经过委托事件显示在主窗体
            QueryByInfo qbi = new QueryByInfo();
            //rtbox_msg.ClearValue();   //清空面板中的数据
            qbi.queryPostMsg += Ss_postMsg;


           // qbi.Show();
            this.customer =qbi.Customer;
            if (customer != null)
            {
                qbi.Close();
            }
            else {
                //qbi.Show();
            }


        }


        //启动service服务--打开socket连接窗口
        private void startService_Click(object sender, RoutedEventArgs e)
        {
            
            StartService ss = new StartService();
            ss.postMsg += Ss_postMsg;
            //当当前服务已经启动的时候,显示为停止
            if (this.server == null)
            {
                ss.button_QueryInfo.Content = "启动服务";
            }
            else {
                ss.Server = this.server;
                ss.b

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值