有关c#的一些

首先对登录界面进行关键代码分析:

 String username, password;
           username = textBox1.Text;
           password = textBox2.Text;
                   
           string myconn = @"Data Source=DESKTOP-BICE0VG;Initial Catalog=yh;Integrated Security=True";
           
           SqlConnection sqlConnection = new SqlConnection(myconn);//新建数据库连接实例
           sqlConnection.Open();//打开数据库连接
           String sql = "select SID,Skey from NAME where SID='" + username + "'and Skey='" + password + "'";//SQL语句实现表数据的读取
           SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
           SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
           if (sqlDataReader.HasRows)//满足用户名与密码一致,进入下一个界面
           {
               Form4 form = new Form4();
               form.Show();
               this.Hide();
           }
           else//如果登录失败,询问是否注册新用户
           {
               DialogResult dr = MessageBox.Show("是否注册新用户?", "登录失败", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
               if (dr == DialogResult.Yes)//打开注册界面
               {
                   Form3 form = new Form3();
                   form.Show();
                   this.Hide();
               }
               else
               {
                   this.Show();
               }
           }

修改密码和注册进行分析
修改

 conn = new SqlConnection("Data Source=DESKTOP-BICE0VG;Initial Catalog=yh;Integrated Security=True");
            try
            {
                conn.Open();
            }
            catch
            {
                MessageBox.Show("连接数据库失败!");
            }
            string strQuery = "SELECT COUNT (*) FROM NAME Where SID='" + textBox1.Text + "' AND Skey='" + textBox2.Text + "'";
            
            comm = new SqlCommand(strQuery, conn);
            if ((int)comm.ExecuteScalar() > 0)//修改密码之前先查询原始密码是否正确                   //
            {
                
                if (textBox3.Text == textBox4.Text)//两次密码是否想等
                {
                    string strUpdPwd = "UPDATE NAME set Skey='" + textBox3.Text + "'Where SID='" + textBox1.Text + "'";
                    comm = new SqlCommand(strUpdPwd, conn);
                    int result = comm.ExecuteNonQuery();
                    if (result > 0)
                    {
                        MessageBox.Show("密码修改成功");
                    }
                    else
                    {
                        MessageBox.Show("密码修改失败!");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("两次密码输入不一致!");
                    textBox1.Focus();
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox4.Clear();
                    return;
                }
            }
            else
            {
                MessageBox.Show("原密码错误");
            }

注册

            String username, password, repassword;
                username = textBox1.Text;
                password = textBox2.Text;
                repassword = textBox3.Text;
                if (password == repassword)//两次输入的密码一致
                {
                    string myConn = @"Data Source=DESKTOP-BICE0VG;Initial Catalog=yh;Integrated Security=True";
                    SqlConnection sqlConnection = new SqlConnection(myConn);  //实例化连接对象
                    sqlConnection.Open();
                    String sql = "INSERT INTO Name(SID,Skey) VALUES('" + username + "','" + password + "')";//SQL语句向表中写入数据
                    SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                    sqlCommand.ExecuteNonQuery();
                    MessageBox.Show("注册成功");

                }
                else
                {
                    MessageBox.Show("密码不一致");
                }

对主界面分析:

Form5 frm5 = new Form5();
          frm5.TopLevel = false;

          frm5.WindowState = FormWindowState.Maximized;
          frm5.FormBorderStyle = FormBorderStyle.None;
          frm5.Dock = DockStyle.Fill;
          frm5.MdiParent = this;
          frm5.Show();
      }

主要是将其他的form界面显示到主界面而不是去跳到别的界面去

对聊天界面进行分析

 public partial class Form11 : Form
    {
        public Form11()
        {
            InitializeComponent();
        }
        private Thread td;
        private TcpListener tcpListener;
        private static string message = "";

        private void button1_Click(object sender, EventArgs e)
        {
           richTextBox1.Clear();
        }

        private void Form11_Load(object sender, EventArgs e)
        {
            td = new Thread(new ThreadStart(this.StartListen));
            td.Start();
            timer1.Start();
        }
        private void StartListen()
        {
            message = "";
            tcpListener = new TcpListener(888);
            tcpListener.Start();
            while (true)
            {
                TcpClient tClient = tcpListener.AcceptTcpClient();
                NetworkStream nstream = tClient.GetStream();
                byte[] mbyte = new byte[1024];
                int i = nstream.Read(mbyte, 0, mbyte.Length);
                message = Encoding.Default.GetString(mbyte, 0, i);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
                string strmsg = "  " + textBox2.Text + "(" + ip[1].ToString() + ")" + DateTime.Now.ToLongDateString() + "\n" + "  " + this.richTextBox2.Text + "\n";
                TcpClient client = new TcpClient(textBox1.Text, 888);
                NetworkStream netstream = client.GetStream();
                StreamWriter wstream = new StreamWriter(netstream, Encoding.Default);
                wstream.Write(strmsg);
                wstream.Flush();
                wstream.Close();
                client.Close();
                richTextBox1.AppendText(strmsg);
                richTextBox1.ScrollToCaret();
                richTextBox2.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void Form11_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.tcpListener != null)
            {
                tcpListener.Stop();
            }
            if (td != null)
            {
                if (td.ThreadState == ThreadState.Running)
                {
                    td.Abort();
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (message != "")
            {
                richTextBox1.AppendText(message);
                richTextBox1.ScrollToCaret();
                message = "";
            }
        }

        private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                button2_Click(sender, e);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值