回答:这里就不给菜单栏的设置了,简单说明一下功能,数据库设计截图如下:
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类型,会造成登陆出问题,可以试试