能帮我看看这个完整的程序代码和窗体的工具箱name text写成什么 吗?实在不行帮我看看(6)(7)吧!实在弄好久都搞不懂 帮帮我吧!

img

img

回答:这里就不给菜单栏的设置了,简单说明一下功能,数据库设计截图如下:

img

img

img

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = textBox1.Text;
            string pwd = textBox2.Text;
            //bool x = false;

            SqlConnection conn = new SqlConnection
            {
                ConnectionString = "Data Source=WIN-8SJRS31FO8P;Initial Catalog=冰冰一号;Integrated Security=True"
            };
            conn.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM [聊天]", conn);
            SqlDataReader Reader = command.ExecuteReader();
            while (Reader.Read())
            {
                if (Reader["用户名"].ToString() == id && Reader["密码"].ToString() == pwd)
                {
                    MessageBox.Show("登陆成功");
                    //x = true;
                    主页 form = new 主页();
                    form.ShowDialog();
                    break;
                }               
            }
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string id = textBox1.Text;
            string pwd = textBox2.Text;
            bool x = false;

            SqlConnection conn = new SqlConnection
            {
                ConnectionString = "Data Source=WIN-8SJRS31FO8P;Initial Catalog=冰冰一号;Integrated Security=True"
            };
            conn.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM [聊天]", conn);
            SqlDataReader Reader = command.ExecuteReader();
            while (Reader.Read())
            {
                if (Reader["用户名"].ToString() != id)
                {
                    SqlConnection conn1 = new SqlConnection
                    {
                        ConnectionString = "Data Source=WIN-8SJRS31FO8P;Initial Catalog=冰冰一号;Integrated Security=True"
                    };
                    conn1.Open();
                    SqlCommand command1 = new SqlCommand("INSERT INTO [聊天] (用户名,密码) VALUES ('" + id + "','" + pwd + "')", conn1);
                    command1.ExecuteReader();

                    MessageBox.Show("注册成功");
                    x = true;
                    conn1.Close();
                    break;
                }
                else
                {
                    x = false;
                }
            }
            if (!x)
            {
                MessageBox.Show("注册失败");
            }
            conn.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {          

        }
    }
}


这里需要修改数据库数据类型,不然默认的text类型,会造成登陆出问题,可以试试