C#面向对象的注册登录winform窗口程序什么意思?
领导要求用VS和c#设计一个注册登录窗口程序,我完成了。
然后说这次依然是注册登录winform窗口程序,“把用户提出来作为一个类”“面向对象”
这句话是什么意思?以下是我最先设计的代码
namespace APP
{
public partial class Form1 : Form//form1是继承form的一个类
{
public Form1() //构造函数不能私有
{
InitializeComponent(); //初始化窗体
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)//触发事件为button1_click
{
if (string.IsNullOrEmpty(textBox1.Text+textBox2.Text))//判断是否为空值
MessageBox.Show("输入用户名和密码", "警告");
else
{
SqlConnection conn = new SqlConnection("Data source=(local);Initial Catalog=log;Integrated Security=True"); //实例化conn变量,指定连接的数据库参数,不需要用户名和密码,即windows身份验证模式
conn.Open(); //打开conn连接的数据库
SqlCommand cmd = new SqlCommand("select * from log where 用户名='" + textBox1.Text.Trim() + "'and 密码='" + textBox2.Text.Trim() + "'", conn);//sqlcommand对象允许指定在数据库上执行增删改查,数据库连接对象,执行数据库语句
SqlDataReader sdr = cmd.ExecuteReader();//使用ExecuteReader方法创建SqlDataReader类型对象
sdr.Read();//从数据库里读数据
if (sdr.HasRows)//使用HasRows属性判断结果中是否有数据
MessageBox.Show("登陆成功", "提示");
else
MessageBox.Show("用户名或密码有误", "警告");
conn.Close();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)//默认为true
{
textBox2.PasswordChar = '\0';
}
else
{
textBox2.PasswordChar = '*';
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string str1 = textBox1.Text; //读取用户名和密码
string str2 = textBox2.Text;
int a = 0; //检验用户名、密码复杂度
int b = 0;
int c = 0;
int x= 0;
int y = 0;
int z = 0;
int i = textBox1.Text.Length;
int j = textBox2.Text.Length;
for (int m = 0; m < str1.Length; m++)
{
char cha1=str1[m];
if (char.IsLetter(cha1)) //是否为字母
{
a++;
}
else
{
if (char.IsDigit(cha1)) //是否为数字
{
b++;
}
else
if(char.IsPunctuation(cha1))//是否为标点符号
{
c++;
}
}
}
for (int n = 0; n < str2.Length;n++ )
{
char cha2 = str2[n];
if (char.IsLetter(cha2))
{
x++;
}
else
if(char.IsDigit(cha2))
{
y++;
}
else
if (char.IsPunctuation(cha2))
{
z++;
}
}
if (a==0 || b==0 || c==0 || x==0 || y==0 || z==0 ||i < 6 || i > 15 || j < 10 || j > 15 || Regex.IsMatch(str1 + str2, "[^a-zA-Z0-9_]")) //正则表达式用来检查字符串是否和指定的表达式匹配
{
MessageBox.Show("用户名长度在6-15位,密码长度在10-15位。\n仅限数字、字母、下划线。\n用户名和密码必须包含数字、字母、下划线。", "警告");
}
else
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=log;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from log where 用户名='" + textBox1.Text.Trim() + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
{
MessageBox.Show("该用户名已存在", "警告");
}
else
{
sdr.Close();
string myInsert = "insert into log(用户名,密码) values('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand myCom = new SqlCommand(myInsert, conn);//创建一个SqlCommand类型对象
myCom.ExecuteNonQuery();
conn.Close();
conn.Dispose(); //关闭并释放对象,下次调用时重新创建conn
MessageBox.Show("注册成功");
}
}
}
private void button3_Click(object sender, EventArgs e) //关闭程序
{
Close();
}
}
}
还请大佬详细解答一下,我对c#并不熟,最好代码说明,谢谢