C#数据库操作

在原先登录程序上更改数据存储到数据库中

登录窗口代码
using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Login
{
    public partial class SignInForm : Form
    {
        public SignInForm()
        {
            InitializeComponent();
        }

        private void 登录_Load(object sender, EventArgs e) { }

        private void textBox1_TextChanged(object sender, EventArgs e) { }

        private void Password_TextChanged(object sender, EventArgs e)
        {
            if (!checkBox1.Checked)
            {
                Password.PasswordChar = '*';
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form f1 = new SignUpForm();
            f1.Show();
        }        

        private void button1_Click(object sender, EventArgs e)
        {
            int Flag = 0;
            String strUser, strPassword;
            sqlConnection1.Open();
            SqlDataReader dataReader = sqlCommand1.ExecuteReader();
            while(dataReader.Read())
            {
                Flag = 1;
                strUser = dataReader["用户名"].ToString();
                strPassword = dataReader["密码"].ToString();
                if (strUser == this.UseName.Text && strPassword == this.Password.Text)
                {
                    MessageBox.Show("Hello world!");
                    Flag = 2;
                    break;
                }
            }
            if (Flag == 0)
                MessageBox.Show("当前无用户,请前往注册!");
            if(dataReader.Read()==false && Flag==1)
            {
                MessageBox.Show("用户名或密码错误!");
                this.UseName.Clear();
                this.Password.Clear();
                checkBox1.Checked = false;
                this.UseName.Focus();
            }
            dataReader.Close();
            sqlConnection1.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                Password.PasswordChar = new char(); //显示密码
            else
            {
                Password.PasswordChar = '*';
            }
        }
    }
}
注册窗口代码
using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Login
{
    public partial class SignUpForm : Form
    {
        public SignUpForm()
        {
            InitializeComponent();
        }

        public void SignUp_Load(object sender, EventArgs e){}

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            textBox3.PasswordChar = '*';
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            textBox4.PasswordChar = '*';
        }

        public void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == textBox4.Text)
            {
                String bent = "", sex = "";
                if (radioButton1.Checked)
                {
                    sex = "男";
                }
                else if (radioButton2.Checked)
                {
                    sex = "女";
                }
                foreach (Control c in groupBox2.Controls)
                {   //获取爱好所选选项
                    if (c is CheckBox && ((CheckBox)c).Checked == true)
                    {
                        bent += c.Text + " ";
                    }
                }
                sqlConnection1.Open();
                string sqlstr = "insert into 数据表 VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + dateTimePicker1.Value + "','" + sex + "','" + bent + "')";
                SqlCommand sqlCommand = new SqlCommand(sqlstr, sqlConnection1);
                sqlCommand.ExecuteNonQuery();
                sqlConnection1.Close();
                MessageBox.Show("注册成功,请返回登录。");
                this.Close();
            }
            else
            {
                MessageBox.Show("两次密码不同!");
                this.textBox4.Focus();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {   //点击取消清除数据
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.textBox4.Clear();
            this.textBox1.Focus();
            this.radioButton1.Checked = false;
            this.radioButton2.Checked = false;
            this.checkBox3.Checked = false;
            this.checkBox4.Checked = false;
            this.checkBox5.Checked = false;
            this.checkBox6.Checked = false;
            this.checkBox7.Checked = false;
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值